Showing posts with label arduino library. Show all posts
Showing posts with label arduino library. 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.


14 September 2012

Yet Another Real-Time Clock

Real-Time Clocks (RTCs) are popular add-ons to microcontroller projects. I am no exception, I have a lot of them kicking around. The most common RTC seems to be the Maxim DS1307. A lot of my RTCs are DS1307s. It's a real workhorse chip, and easy to use. Still, it has disappointed me in a couple ways. For one, it's not always as accurate as I would like. I've used inexpensive no-name crystals and I've used more expensive ones from the top-shelf distributors. Sometimes they don't even seem to operate within the crystal's specs (typically ±20ppm). Not sure why this is, and it certainly could be my fault, but there it is. Another thing is that sometimes I have a 3.3V microcontroller circuit, but the DS1307 requires 5V.

Another alternative is the Maxim DS3231 (or its SPI relative, the DS3234). I love this chip, it is so cool. The integrated, temperature-compensated crystal makes it very accurate (±2ppm from 0°C to +40°C), and it will operate on 3.3V. This addresses all of my gripes with the DS1307. But it is more expensive, nearly $9 from Mouser in single quantities as I write this, where the DS1307 is about half that. (Yes, I know DS1307s can be had for significantly less from other sources.) But the DS3231 is harder to find at discount prices, and is only available in a surface-mount package if that makes a difference to you.

Enter the Microchip MCP79412. It operates off a crystal similar to that used by the DS1307, so the basic accuracy is about the same. But, it can be calibrated by setting an internal register. It will operate down to 1.8V. And while it does require a few more external passive components than the DS1307, it costs only $1.23 in single quantities. I popped for ten and paid $0.98 per copy. Again, it is only available as a surface-mount component.

The MCP79412 has some other cool tricks up its sleeve as well, including alarms, tracking power outages, and EEPROM in addition to SRAM. I've detailed these in the table below.

To summarize, I've been tinkering with the MCP79412 on and off for the last couple months and have come to like it quite well. I designed a breakout board for it (pictures below) and wrote an Arduino library to support it. I've only used three of the ten chips so far, but they have all operated well within the ±20ppm spec of the crystal I chose. One unit seems to be within 2ppm, and so hardly needs trimming.

If you are also using this chip, or would be interested in it, I'd love to hear from you!

PS: For a comprehensive example using the MCP79412, see my Power Outage Logger project.

Real-Time Clock Comparison
Feature MCP79412 DS1307
On-Chip Calibration ±127 ppm N/A
Alarms Dual alarms (single output) N/A
Power Fail/Restore Timestamps Yes N/A
Unique ID 64-bit ID N/A
EEPROM 128 bytes N/A
Battery-Backed SRAM 64 bytes 56 bytes
Vcc 1.8 - 5.5V 4.5 - 5.5V
I2C Interface Clock Frequency 400 kHz (Vcc ≥ 2.5V) 100 kHz
Square-Wave Output 1, 4096, 8192 or 32,768 Hz 1, 4096, 8192 or 32,768 Hz


MCP79412 RTC Breakout Board, Top
MCP79412 RTC Breakout Board, Bottom

17 March 2012

Arduino Timezone and DST Library

The Timezone library facilitates time zone conversions and automatic daylight saving (summer) time adjustments. This is accomplished by setting a Real Time Clock (RTC) to Universal Coordinated Time (UTC) and then converting UTC to the correct local time, whether it is daylight saving time (a.k.a. summer time) or standard time.

The Timezone library is designed to work in conjunction with the Arduino Time library at http://www.arduino.cc/playground/Code/Time. To download and use the Timezone library, including documentation and example sketches:


  • Go to https://github.com/JChristensen/Timezone/downloads and download the file in the compressed format of your choice (zip or tar.gz) to a convenient location on your PC.
  • Uncompress the downloaded file. This will result in a folder containing all the files for the library, that has a name similar to "JChristensen-Timezone-42e98a7".
  • Rename the folder to just "Timezone".
  • Copy the renamed folder to the Arduino sketchbook\libraries folder.
  • Read the ReadMe.txt file!