Breaker 7-6, This is Pi, Netduino Do You Copy?

June 16, 2014

I recently wrote about getting the a Raspberry Pi connected with Azure storage. This works well for when you have a full network stack to work with but what those scenarios where you don’t have this luxury? Here we’ll walk through one potential solution using the some super inexpensive radios, a Netduino 2 and the Raspberry Pi.

The architecture of this solution is pretty simple. We’re going to turn the Raspberry Pi into a hub for one or more stand-alone microcontrollers. In this example I’m using a Netduino 2 but this model works with just about any microcontroller (Arduino, BeagleBoard, etc.).  The radios are nRF24L01+ 2.5Ghz transceivers.

rpi_nd_nrf

Prepping the Pi

Before we can use the nRF module with the Pi we need to first install a couple of packages. The nRF radio uses a standard SPI (serial to parallel interface) connection plus a couple of additional pins to handle enabling transmission and handling notification of new messages coming in. In order to work support the module we’ll need to enable GPIO (and SPI).

There are a number of ways of getting GPIO and SPI set up on the Pi. My personal preference is a project called WiringPi due to the drop-dead simplicity of the process. There are complete instructions (and lots of other interesting tid-bits) available as the WiringPi site but the bare-bone-basic installation requires just a couple of lines:

```bash sudo apt-get update sudo apt-get upgrade sudo apt-get install git-core git clone git://git.drogon.net/wiringPi cd wiringPi ./build gpio -v ```

Once completed you should see the version number (currently 2.14) displayed on the screen. The only remaining task is to enable SPI. Please note that WiringPi is designed for prototyping which means it doesn’t permanently enable SPI. This means that you’ll have to execute this command again after a restart. It isn’t a big deal but I’ve caught myself running a script and wondering why it failed only to remember I needed to re-enable SPI.

```bash gpio load spi ```

Finally we need to install the Node-NRF package for Node.js. This will provide the interface between Node and our radio (if you get a chance, show your appreciation by giving the author a star over at GitHub https://github.com/natevw/node-nrf. They did an awesome job).

```bash npm install nrf ```

Now we can move on to actually wiring the radio. GPIO on the Pi can be a little difficult thanks to the male pin header. I’ve taken to using a little device from Adafruit called the <a href=”http://www.amazon.com/gp/product/B00JE1T1WY/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B00JE1T1WY&linkCode=as2&tag=soapb30-20”” target=”_blank”>Pi T-Cobbler</a> that makes life a lot easier. It allows you to use a simple breadboard instead of messing around with female to female jumper cables.  The pin configuration is as follows:

pi-nrf

Prepping the Netduino

For the Netduino I started with Jakub Bartkowiak’s nRF24L01p driver on Codeplex. This worked extremely well when both sides were Netduino’ s but when one side was an Arduino or Pi it fell down. Thankfully ShVerni over in the Netduino forums pointed out that the driver wasn’t in compliance with the nRF24L01+ spec. According to the spec all data must be sent Least Significant Byte First (i.e. inverted). With a few changes to the driver (and condensing it into a single .cs file) I had a working driver. I’ve put up my altered version at <a title=”

The wiring for the Netduino is very straightforward:

Netduino to nRF Mappings ####

Turing It On

Since this is a proof of concept the code is extremely simple.  The nRF module can transmit to 1 address and receive on 6. For our example we’re telling the Raspberry Pi to listen for messages at 0xF0F0F0F0E1 and transmit messages to 0xF5F0F0F0F2 (the Netduino is logically the opposite). The goal is to have the Netduino send a message to the Pi and for the Pi to then bounce that message back. This will illustrate how the modules work without any application specific clutter.

The code for the Netduino can be found at https://github.com/mlafleur/Netduino-nRF24L01p. Alternatively you can download Netduino-nRF24L01p as a zip file. Once you have the project loaded in Visual Studio you can simply deploy to your Netduino by pressing F5. The debugger will begin outputting some status information.

The core component of this code can be found in the Start() method of Program.cs. In here we initialize and enable the radio (the rest is fairly common C#).

```csharp // Initialize the radio on our pins _radio.Initialize(SPI_Devices.SPI1, Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D8); _radio.Configure(new byte[] { 0xF5, 0xF0, 0xF0, 0xF0, 0xF2 }, 76, NRFDataRate.DR1Mbps); _radio.OnDataReceived += _radio_OnDataReceived; _radio.Enable(); ```

The first line initializes the correct pins while the second configures the radio to listen at F5F0F0F0F2 on channel 76 with a data rate of 1 Mbps. We then wire up the event for data received and enable the radio.

The code for the Raspberry Pi can be found at <a title=”

Coming Soon…

In another post I will start digging in to this model a little deeper. We will be wiring up some sensors to the Netduino and routing the results to Azure through the Pi Hub. We’ll also investigate using an Arduino along side the Netduino.