Nanode Gateway/Remote Tutorial 1

Remote Sensing , pardon me, what was that, what’s the temperature over there?

Note: This is part of series of tutorials on the Nanode

The Arduino has been a transformational device. It is being used everywhere from art installations to the manufacturing floor. A whole new class of makers have gotten their first taste of micro-controllers using the Arduino. An area of particular interest to me is using Arduinos in distributed computing and more specifically remote sensing and the “Internet of Things.” This series of tutorials will walk you through the basics of connecting one or more remote devices to a central gateway and managing the interactions between those devices. These tutorials will cover everything from collecting remote data, storing collected data in a central repository for online viewing, remote activation of devices, and well who knows…..

Let’s get started

Building Blocks:

Nanode Gateway: The original Nanode was designed by London Hackerspace. The Nanode Gateway is an Arduino-like micro-controller with both a standard network connector and an onboard RFM12B radio used to communicate with the remote sensors

Nanode Gateway (click for larger)

Nanode Gateway (click for larger)

Nanode Remote: The Nanode Remote, also a Nanode derivative, differs from the Nanode Gateway in that it only contains the RFM12B radio and does not contain the network connector.

Nanode Remote (click to enlarge)

Nanode Remote (click to enlarge)

Check out Jeelabs.org for tons of useful information on the RFM12B radio.

One difference between a Nanode and a standard Arduino is the programming cable. A standard Arduino usually communicates with a host computer via USB, the Nanode uses an FTDI Cable (see photo of Nanode Gateway to see FTDI cable connected). An FTDI cable can be ordered through the Wicked Device Store or several other suppliers.

In addition to the Nanode GatewayNanode Remote and FTDI cable, a few other parts are required to complete this first project. A complete kit including a Nanode Gateway and NanodeRemote is available from Wicked Device,  or if you prefer to buy the parts separately, here is a list of what you’ll need for tutorial 1

  1. TMP36 Temperature Sensor, available here
  2. Standard LED – Radio Shack part 276-330
  3. 220 ohm resistor (red-red-brown code) – Radio Shack part 271-1313
  4. Prototyping breadboard
  5. 9 Volt battery
  6. 9V battery clip Radio Shack part 270-324
  7. Hookup wire

Circuit

The circuit used on the Nanode Remote has two subsections; a temperature monitor and a status LED. No additional circuit is required for the Nanode Gateway. The two circuits on the Nanode Remote look like this.

Tutorial 1 Circuit (click to enlarge)

Tutorial 1 Circuit (click to enlarge)

Circuit Description

  1. Connect the positive (anode) side of the status LED (longer lead) to Nanode digital pin 5. Using a 270 ohm resistor connect the negative (cathode) LED lead to Nanode ground.
  2. The TMP36 has three leads (image link). The 5 Volt lead connects to the Nanode 5 volt pin, the Ground lead connects to the Nanode ground pin, and the signal pin  connects to Nanode analog pin A0.

Here are photos of the circuits built on both a standalone breadboard and on an Arduino Proto-Shield

Breadboard circuit Tutorial 1 (click for larger)

Breadboard circuit Tutorial 1 (click for larger)

Proto-shield circuit tutorial 1 (click for larger)

Proto-shield circuit tutorial 1 (click for larger)

Software

The software is divided into two Nanode sketches. “Sketch” is the name used for Nanode/Arduino programs. One sketch is for the Nanode Remote and another sketch for the Nanode Gateway. You should have Arduino IDE 1.0.5 setup on your computer. Check  http://arduino.cc/en/Main/Software to get the latest software.

The sketches for both the Nanode Gateway and the Nanode Remote requires JeeLib created by Jean-Claude Wippler. The latest version of JeeLib can be found at https://github.com/jcw/jeelib. Download the zip file and rename the unzipped directory from jeelib-master to jeelib then copy the renamed directory to your Arduino/libraries directory. If you are unfamiliar with how to add a library to your Arduino IDE, checkout the Arduino documentation http://arduino.cc/en/Guide/Libraries, Adafruit also has an excellent tutorial on installing libraries http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use/

Let’s Start with the Nanode Remote. At this point you should have the Nanode Remote hardware hooked up as described in the “Circuit” section above.

Here is the sketch, cut and paste it into a new window on your Arduino IDE

// Start of Nanode Remote code

// Based on the pollee code from <jc@wippler.nl>
// 2011-11-23 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
//
#include <JeeLib.h>

/*
  Status LED
*/
#define BLINK_DELAY 1000 // milliseconds
#define LED_PIN 5  // Status LED

/*
  RF12 Communications
*/
#define RF12_GROUPID 212    // all nodes must be a member of the same group
                                                   // to communicate with each other
#define RF12_NODEID  2         // Each node within a group must have a unique ID

/*
  Sensor Specific Code
*/

#define TEMPERATURE_PIN A0

// Definition of Data Structure used to send information from Remote to Gateway

typedef struct {
  byte node;
  long time;
  float temp;
} Payload;

// Create an instance of this data structure
Payload mypayload;

//Arduino Setup code, run once after reset
void setup () {
  int i;
  float temperature;
  float volts;
  int analogData;

  Serial.begin(57600);
  Serial.print("Remote Sensorn");

  // Initialize RF12 Radio
  mypayload.node = rf12_initialize(RF12_NODEID, RF12_433MHZ, RF12_GROUPID);

  // Set status LED pin as output
  pinMode(LED_PIN,OUTPUT);

  // Print temperature to monitor
  /* LM34 CODE */
  //temperature = analogRead(TEMPERATURE_PIN);
  //temperature = (5.0 * temperature * 100.0) / 1024.0;

  /* TMP36 CODE */
  analogData = analogRead(TEMPERATURE_PIN);
  volts = analogData * (5.0 / 1024.0);
  temperature = (volts - 0.5) * 100;  //Celsius
  temperature = (temperature * 9.0 / 5.0) + 32.0;
  Serial.println(temperature);

  // Blink LED 3 times at end of initialize sequence
  for(i = 0; i < 6; i ++){
    toggleLed();
    delay(BLINK_DELAY);
  }
}

// Each call to toggleLed, switches the state of Led from on to off or off to on
int ledState = LOW;

void toggleLed() {

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(LED_PIN,ledState);
}

// main loop
void loop () {
  float temperature;
  float volts;
  int analogData;

  // wait to be polled by Gateway
  if (rf12_recvDone() && rf12_crc == 0 && rf12_len == 0 && RF12_WANTS_ACK) {

    // Fill payload with current time in milliseconds
    mypayload.time = millis();

    /* LM34 CODE */
    //temperature = analogRead(TEMPERATURE_PIN);
    //temperature = (5.0 * temperature * 100.0) / 1024.0;

    /* TMP36 CODE */
    analogData = analogRead(TEMPERATURE_PIN);
    volts = analogData * (5.0 / 1024.0);
    temperature = (volts - 0.5) * 100;  //Celsius
    temperature = (temperature * 9.0 / 5.0) + 32.0;

    // Fill payload with temperature info
    mypayload.temp = temperature;

    Serial.println(temperature);

    // start transmission
    rf12_sendStart(RF12_ACK_REPLY, &mypayload, sizeof mypayload);

    toggleLed();
  }
}
// End of Nanode Remote Code

Once this code is cut into your Arduino IDE your screen should look like this

Remote sketch (click for larger)

Remote sketch (click for larger)

Try verifying that the code compiles correctly by clicking the check mark at the upper left of the Arduino IDE

Arduino IDE verify (click to enlarge)

Arduino IDE verify (click to enlarge)

If you are getting errors there is a good chance that the JeeLib library was not installed correctly.

Once the code compiles correctly connect an FDTI cable to the USB port of your computer and connect the other end to the Nanode Remote. Pay close attention to the orientation of the FTDI cable when connecting to the Nanode. In particular, make sure the Nanode FTDI pin marked black is lined up with the FTDI cable pin marked GND.

FTDI with Nanode FTDI cable (click to enlarge)

FTDI with Nanode FTDI cable (click to enlarge)

FDTI - Standard FTDI cable (click to enlarge)

FDTI – Standard FTDI cable (click to enlarge)

Now download the code to your Nanode Sensor. You will need to set the board type on your Arduino IDE to UNO (Tools->Board Type->Arduino UNO)

You will also need to set the serial port on your Arduino IDE (Tools->Serial Port->). If you are unsure which serial port corresponds to the FDTI cable, remove the FDTI cable from your computers USB port and look at the serial ports available in the Serial Ports drop down menu of the Arduino IDE (Tools->Serial Port). Now reconnect the FTDI cable to the USB port and again look at the available serial ports in the Arduino IDE, you should see one or more new ports, try selecting one of the new ports and downloading your code to the Nanode Remote.

If you are seeing an error during download try selecting the other new port that became available after reconnecting the FTDI cable to your computer.

Arduino IDE Download (click to enlarge)

Arduino IDE Download (click to enlarge)

Once the code is successfully downloaded, open the serial monitor on your Arduino IDE by clicking the icon in the upper left corner.

Start Serial Monitor (click to enlarge)

Start Serial Monitor (click to enlarge)

Set the serial monitor baud rate to 57600 (bottom right corner). Click the reset button on the Nanode Remote. The status LED you wired to digital pin 5 should flash 3 times, your Arduino Serial Monitor should now look something like this.

Nanode remote monitor screen grab (click to enlarge)

Nanode remote monitor screen grab (click to enlarge)

You are all done setting up the Nanode Remote, disconnect your Nanode Remote from the FTDI cable.

OK, let’s move on and setup the Nanode Gateway. For this first project we won’t be connecting the Nanode Gateway to any networks so don’t worry about the network connector. Paste this code into a new Arduino IDE window.

//Start of Gateway code
/// Based on the polleer code from <jc@wippler.nl>;
// 2011-11-23 <jc@wippler.nl>; http://opensource.org/licenses/mit-license.php
//
//
#include <JeeLib.h>;

#define LOOP_DELAY 5000 // Milliseconds
/*
  RF12 Communications
*/
#define RF12_GROUPID 212    // all nodes must be a member of the same group
                          // to communicate with each other
#define RF12_NODEID_GATEWAY  1      // Each node within a group must have a unique ID
#define RF12_NODEID_REMOTE 2

MilliTimer timer;

typedef struct {
  byte node;
  long time;
  float temp;
} Payload;

void setup () {
  Serial.begin(57600);
  Serial.println("Gateway");

  rf12_initialize(RF12_NODEID_GATEWAY, RF12_433MHZ, RF12_GROUPID);
}

void loop () {
  // send an empty packet to Nanode Remote
  rf12_sendNow(RF12_HDR_ACK | RF12_HDR_DST | RF12_NODEID_REMOTE, 0, 0);

  // wait up to 10 milliseconds for a reply
  timer.set(10);
  while (!timer.poll())
    if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof (Payload)) {
      // got a good ACK packet, print out its contents

      // Data from RFM12B returns in rf12_data
      const Payload* p = (const Payload*) rf12_data;
      Serial.print("Node: ");
      Serial.print((word) p->node);
      Serial.print(": ");
      Serial.println(p->temp);
      break;
    }
  delay(LOOP_DELAY);
}
//End of Gateway code

Now connect your Nanode Gateway to your FTDI cable, again be careful to get pins aligned correctly. Download the gateway code into your Nanode Gateway and start the the Arduino serial monitor. You should see something like this

Gateway monitor - no remote (click to enlarge)

Gateway monitor – no remote (click to enlarge)

Now the big step. Connect the 9 Volt battery clip the the Nanode Sensor ground and Vin pins. Be very careful to get the polarity correct. Of course you could power the Nanode Remote via the micro-usb connector or the FTDI connector.

Battery powered remote (click to enlarge)

Battery powered remote (click to enlarge)

If everything goes according to plan you should now see temperature reports displaying in the Arduino serial monitor connected to your Nanode Gateway. Your screen should look something like this.

Gateway receiving temperature data from remote (click to enlarge)

Gateway receiving temperature data from remote (click to enlarge)

Congratulations, you are now on the IOT superhighway, or at least you have taken the on-ramp.

BTW: Differences between standard Arduino and a Nanode

The onboard Nanode features such as the network connector and RFM12B radio use existing Arduino I/O pins for control. Check out this table for a list of used and available I/O pins. Except for those pins used for specific Nanode functions the Nanode is Arduino compatible in form factor, connectors, and software.

Next

How to get this data into Xively

@ken_rother

Tags: ,