Showing posts with label internet of things. Show all posts
Showing posts with label internet of things. Show all posts

06 June 2015

The Double-A XBee Sensor Node

This is my design for an Arduino-compatible (ATmega328P), low-power sensor node using an XBee ZB module. This node can operate as part of the GroveStreams Wireless Sensor Network that I described in my previous post. I call it the Double-A XBee Sensor Node since it runs on a pair of AA cells.

Although I haven't done a lot of testing, I believe it should run for over a year on a pair of AA alkaline cells. This will be affected by the power requirements of additional sensors, supply voltage (either 3.3V or 5V can be selected) and data transmission frequency. The basic board with no additional sensors draws less than 5µA while sleeping.

The AA XBee Node includes an accurate real-time clock (DS3231, ±2ppm from 0°C to +40°C) for precise timing of data transmissions and an accurate on-board I2C temperature sensor (MCP9808, ±0.25°C typical). A prototyping area is provided for connecting additional sensors. Twelve microcontroller GPIO pins are available to interface with sensors, including the I2C and SPI buses and analog inputs. A pushbutton switch and an LED are available for application use if needed.

For more information

The Double-A XBee Sensor Node is an open-source project. For complete information on the circuit design, including Eagle files, configuration options, programming requirements, etc. see the GitHub repository.

For an example sketch, see the aaXBee sketch which is part of my GroveStreams library. Using this sketch, the AA XBee Node sends data to an Arduino running the gsGateway sketch, which forwards it to the GroveStreams web site.

19 May 2015

A GroveStreams Wireless Sensor Network

In this post I describe how to build an XBee-based wireless sensor network that feeds data to the GroveStreams Data Analytics Platform. I've been using GroveStreams for a couple years now and I am very impressed. The GroveStreams platform has features and power far beyond other "IoT" data services that I have tried. Perhaps even more impressive is how eager the GroveStreams team is to work with their users. User input frequently results in improvements to the platform, and usually quite rapidly at that. GroveStreams delivers a great service, great performance, and they are intensely focused on their customers.

GroveStreams accounts are free, up to certain levels of utilization; above this, rates seem quite reasonable. See the site for details.

This project consists of two network nodes, connected by a ZigBee mesh network implemented with XBee ZB modules (f.k.a. Series 2). One node is the web gateway. Its XBee is configured as the network coordinator. The gateway forwards data sent to it by other nodes to the GroveStreams service via an Ethernet shield.

The other node is a sensor node; its XBee is configured as a router. This node uses a simple analog temperature sensor and sends the data to GroveStreams via the gateway node. Multiple copies of the sensor node are easily added to the network. These can have the same sensor, or with additional programming, other sensors could be added.

Hardware required for this project

(2) Arduino Uno boards
(1) Arduino Ethernet shield
(2) XBee ZB modules (I use part no. XB24-Z7WIT-004 with the wire antenna)
(2) Adafruit XBee Adapter kits
(1) Adafruit FTDI Friend (used to configure the XBees with XCTU)
(1) USB A Male to Mini B cable to connect the FTDI friend to a computer
(2) Yellow LEDs (use any common 3mm or 5mm LED)
(2) Red LEDs (ditto)
(4) 330-ohm resistors (anything between 220 ohms and 1000 ohms should be OK)
(1) 100nF ceramic capacitor
(1) Analog Devices TMP36 temperature sensor
Breadboards, jumper wires, USB and Ethernet cables, power supplies, etc.

Step-by-step instructions

  1. Build the Gateway and Sensor nodes per the schematic diagram.

Gateway Node

Sensor Node
  1. Download and install the XBee Configuration and Test Utility software (XCTU) from digi.com (version 6.2.0 is current as of this writing).
  2. If you have not used FTDI on your computer before, install the FTDI drivers.
  3. Install one XBee in an Adafruit XBee adapter. Using the FTDI Friend, connect it to the computer with a USB A-to-Mini-B cable.
Configuring an XBee with Adafruit's FTDI Friend
  1. Download the latest Coordinator API firmware (21A7 as of this writing) to the XBee. Then, configure the ID, NI, BD, and AP parameters as described in the gsGateway example sketch. (The PAN ID can be any number you like; all XBees on the same network must have the same PAN ID. However, I do recommend a non-zero value.)
  2. Repeat with the other XBee, except download the latest Router API firmware (23A7 as of this writing) and set the four parameters as described in the gsSensor example sketch.
  3. Install the Coordinator XBee in the Gateway node and the Router XBee in the Sensor node.
  4. On both the Gateway and Sensor nodes, temporarily disconnect Arduino Pin 0 from the XBee to allow programming. (Since the XBee connects to the Arduino's serial port, it must be disconnected from the RXD pin to allow the Arduino to be programmed via the bootloader.)
  5. Now go to the GroveStreams web site and create an account. This involves a verification email which should be a familiar process. Once the account is verified, log in and you should see the following message that offers to create an Organization.
After initial GroveStreams Sign-In

Let's digress here for a very quick and incomplete primer on some GroveStreams terminology. An Organization is a container for one or more Components. A Component can represent a physical object in the real world. For this project, the Gateway node will be one Component and the Sensor node will be another. Components in turn contain one or more Datastreams, which contain the actual data sent to the GroveStreams site, for example by our sensor. Keep these concepts in mind for now and we'll see how it all fits together shortly.
  1. Continuing on, respond to the message by clicking Yes to create an Organization. Give it any name you like, then click the Create Organization button.
Creating an Organization
  1. Now we see the GroveStreams start page which lists our Organizations, of which there should be only one, namely the one we just created.
GroveStreams Start Page
  1. Click on the Organization name to open the GroveStreams Observation Studio, which is the main interface to configuring and observing everything in GroveStreams. On the left, note there is a folder for Components, but if the little triangle is clicked, the folder doesn't expand, since no components have been created yet. We can create Components from Observation Studio, but GroveStreams also has a handy feature to create Components automatically when data is first received. Being mostly lazy, we'll use that method for this project.
GroveStreams Observation Studio
  1. Next click on the yellow lock "API Keys" icon near the top right. This will open a dialog that allows keys to be displayed. Our Gateway node needs a key so that GroveStreams can validate it and associate the incoming data with our Organization. Check the box, "Feed Put API Key (with auto-registration and request stream rights)", then click View Secret Key.
API Keys Dialog
  1. In the dialog box that appears, use the mouse to select the key. Press Ctrl-C to copy it to the clipboard, then open Notepad or other convenient application and press Ctrl-V to paste the key. We'll use it shortly, but for now just close the API Secret Key dialog box and the API Keys dialog box.
  2. We are almost ready to program the Gateway and Sensor nodes. But first we need to install the prerequisite libraries. If you are not familiar with installing libraries, please click this link.

    Install each of these libraries:

    http://github.com/JChristensen/GroveStreams
    This library manages the connection to the GroveStreams web site, allows data to be sent, etc.

    http://github.com/JChristensen/gsXBee
    This library handles interface to the XBees.

    http://github.com/andrewrapp/xbee-arduino
    An essential library for working with XBees in API mode.

    http://arduiniana.org/libraries/streaming/
    A very convenient alternative to Serial.print(). (I don't know why it's not part of the Arduino core.)
  3. Start the Arduino IDE and open the gsGateway example sketch from the GroveStreams library (File > Examples > GroveStreams > gsGateway). Change this line of code by pasting in the API key that was copied earlier. (You may also want to change the MAC address on the following line, but chances are that the existing one will work fine.)
  4. Upload the modified gsGateway sketch to the Gateway node. Disconnect the Gateway node. Reconnect Pin 0 to the XBee.
  5. Open the gsSensor example sketch (File > Examples > GroveStreams > gsSensor) and upload it to the Sensor node. No code changes are needed. Disconnect the Sensor node. Reconnect Pin 0 to the XBee.
  6. Connect the Gateway node to your LAN with an Ethernet cable. Apply power to the Arduino. After several seconds, the red wait LED should blink once and the yellow heartbeat LED should begin blinking on and off. Upon startup or reset, the Gateway sends a single message to GroveStreams. Go to the GroveStreams Observation Studio and there should now be a Component named "Coord" created for the Gateway/Coordinator node. This may take several seconds; if you don't see it, try refreshing the web page. Expand the Coord Component and one datastream named "msg" should be visible. This is the startup message sent by the Gateway.
Coord Component created, Startup message sent
  1.  Connect power to the Sensor node. The first data will be sent in one minute, then at one minute intervals thereafter. Wait at least a minute, then refresh the Observation Studio web page and the Sensor node should now be visible as Component "TMP36a". It has three datastreams: "s" which is a sequence number, "C" which is the temperature in Celsius, and "rss" which is the received signal strength at the Gateway XBee. ("rss" is added by the Gateway node. Note that in larger XBee networks, several hops may occur between source and destination nodes. The RSS value only represents that of the last hop.)
TMP36a Sensor Component created

Both nodes write diagnostic information to the serial port; open the Arduino serial monitor or other terminal program at 115,200 baud to observe this information.

Concluding remarks

This has been a long post but it's basically a cookbook approach and only scratches the surface. I hope it will be a starting point for further exploration into both GroveStreams and wireless sensor networks. GroveStreams has much more functionality than I have mentioned so far. Dashboards can display and aggregate data in many different ways, events can be defined to key off certain data values and cause various alerts to be sent, and derived streams can perform various calculations on data. For instance, GroveStreams can easily convert the temperature data sent by the Sensor node from Celsius to Fahrenheit; it would not be necessary to do this in the microcontroller firmware.

I also have not elaborated on the XBee node naming convention. The XBee Node Identifiers (NI) need to start with a one- to eight-character name. This name is used as the GroveStreams Component ID. The name is followed by an underscore character, then eight digits. The eight digits comprise four numbers that control the timing and frequency of data transmission. In this relatively simple project, even though the code for the Gateway node does not use any of these numbers, and the Sensor node only uses the interval, the naming convention still needs to be followed.

One advantage of the naming convention is that it allows identical sensor nodes to be deployed without having to change the microcontroller code. Only the XBee NI parameter needs to be changed. See the comments in the gsXBee.h file for a more complete description of the naming convention.

More to come

I'll be following up with a future post about another project that builds on this one: A battery-powered sensor node. Using two AA cells, it should run for at least a year, taking advantage of the low-power sleep modes of both the ATmega328P microcontroller and the XBee. So stay tuned for that.


28 April 2011

A "Minimal" Arduino/XBee/Pachube Sensor Network

One or two people have asked in online forums for code from my sensor network, and while I'm usually happy to share, it's full of a lot of extraneous stuff (RTCs, NTP, displays, thermocouples) that might not be of interest to everyone, but more importantly, that probably only obscures the fundamentals of XBee networking and communicating with Pachube.

So with that in mind, I cut out the extraneous stuff, and just left the bare essentials: A remote sensor unit which transmits a single reading from a photocell once per minute to a base unit which connects to the internet and forwards the data on to Pachube.

Bill of materials -- Base unit:
(1) Arduino Uno, Through-hole or SMD edition
(1) Arduino Ethernet shield
(1) XBee ZB low power Zigbee Module, Digi Product ID XB24-Z7CIT-004, XB24-Z7WIT-004, XB24-Z7SIT-004, or XB24-Z7UIT-004 (these differ only in the antenna, they are functionally equivalent)
(1) Adafruit Industries XBee Adapter
(1) LED for heartbeat (optional)
(1) 330-ohm resistor for heartbeat LED (optional)

XBee configuration:
  • Load the Zigbee Coordinator API firmware (I'm using Version 2170).
  • Set the PAN ID as desired, to match that of the sensor unit(s).
  • Set API mode 2 (AP=2).
  • Set baud rate to 9600.

Bill of materials -- Remote unit:
(1) Arduino Uno or similar (Through-hole, SMD edition, Boarduino, etc.)
(1) XBee ZB low power Zigbee Module, Digi Product ID XB24-Z7CIT-004, XB24-Z7WIT-004, XB24-Z7SIT-004, or XB24-Z7UIT-004.
(1) Adafruit Industries XBee Adapter
(1) CdS photocell
(1) 10K resistor

XBee configuration is same as above, except:
  • Load the Zigbee Router API firmware (I'm using Version 2370).

Here are the sketches.  Unzip into the Arduino Sketchbook folder.  Before uploading, be sure to disconnect the XBee from Arduino pin 0.  Also be sure to enter your MAC, IP, and Pachube API key in the base station main module, and your Pachube feed number and the address of your network coordinator XBee in the main module for the remote unit.

Here is the Pachube feed. Pretty boring, but at least you can see that it does work. Not sure how long I'll leave it running, probably until I need the parts for something else!  Feedback and questions welcome!

Note that in the following wiring diagrams, the XBees plug into the Adafruit adapter, which in turn plugs into the breadboard. The XBee pins do not (in fact, cannot, spacing is different) connect to the breadboard.  The adapter plugs into the breadboard, this is represented by the 10-pin header below the XBee.

Wiring the base unit



Wiring the remote unit

My base unit

My remote unit (using a Boarduino)

25 January 2011

A simple wireless sensor network

A lot of pieces come together in this project.  It seems to be working well; I'm pretty happy with it.  I'll jump right in and give a high level description of the pieces.

I started this blog with digital clocks and this project starts with one as the sensor unit.  It consists of a Arduino Pro Mini 328 (check the size of that guy!), a "Chronodot" Real-Time Clock (RTC) module from Macetech based on the Maxim DS3231 chip, and an XBee ZigBee module from Digi International (part no. XB24-Z7WIT-004).  Just for fun and to generate a little more data to collect, I connected a simple Cadmium-Sulfide (CdS) photocell to one of the Arduino's A/D inputs to measure light intensity wherever the sensor unit happens to be.  The readout is a 16x2 LCD from Sparkfun.  See the picture below.

Sensor Unit.  L to R: Arduino, RTC, XBee, Photocell and XBee status LEDs, power supply.

A few words on the parts.  The Arduino Pro Mini has the same ATmega328 microprocessor and basic specs as the Arduino Uno, but is much smaller!  I soldered on headers so it would plug into a breadboard.  What it doesn't have is a power supply or a USB interface.  Both Sparkfun and Adafruit make nice little breadboard power supplies that work great and accept the same wall wart input as the Uno.  As for the USB connection, you'll need an FTDI breakout board (about the size of a small postage stamp) and FTDI drivers to do the interface.  Once you have that (and it's pretty easy), the USB interface works pretty much the same as the Uno.

The DS3231 RTC is pretty cool too.  If you keep it between freezing and about 100°F, it will keep time within two parts per million, which is about a minute per year.  It accomplishes this by monitoring its own temperature (a major cause of oscillator drift) and switching capacitors in and out of the crystal oscillator circuit to trim the frequency.  The crystal itself is on-chip which also helps accuracy.  A very sweet piece of engineering.  In addition to the date and time, we can also read the temperature from it (in °C to the nearest quarter degree).

Last we have the XBee module, which uses the ZigBee specification, based on the IEEE 802.15.4 standard.  These modules are low power and don't offer terribly high bandwidth, which is just fine for sensor networks which tend to send very small amounts of data at any one time.  The really neat thing is that XBee modules form themselves (with just about zero help from you!) into self-healing mesh networks.  They can be configured either as network coordinators, routers, or end devices.  Every network must have exactly one coordinator.  Routers can do anything an end device can, plus forward messages between devices.  End devices cannot route traffic, and must do all their business through a parent node which is either a coordinator or a router.  Additionally, end devices can sleep in a low-power mode which would be very good for battery-powered remote sensors.  If an end device is sleeping, its parent node will store traffic for it.  The XBee in my sensor module is configured as a router.

Now we come to what I call the "base" unit.  This consists of an Arduino Uno, with an Ethernet shield added on top and connected to my internet router.  Also we have an XBee module, configured as the network coordinator, and a seven-segment LED display.

Base unit. Arduino and Ethernet shield at top. XBee, status LEDs, temperature display on breadboard.


The whole thing works like this.  Once a minute, the sensor unit sends the temperature and light intensity data to the base unit.  The base unit sends the data to Pachube (click here to go to the Pachube feed for this project) and displays the temperature on the LED display.  We can then download the data from Pachube, or have it create charts like below.  These are live charts and will refresh every few minutes (not sure how often exactly, haven't found the spec on Pachube yet).  So if you refresh this page, you will see current data, assuming that a cat hasn't chewed through any wires on my end in the meantime.





DS18B20 Temperature Sensor


It would be straightforward to add additional sensor units to this project.  One idea I have in mind is to measure the temperature of my wood stove with a thermocouple.  Maybe send a text message to my mobile phone if it got over a certain temperature.