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)

35 comments:

  1. It was pointed out to me that the remote sensor unit could be implemented with only an XBee, i.e. without a microcontroller. That is certainly true. The XBee has the ability to take an analog signal, such as from the photocell, and transmit it periodically. However, my intent was to show the more general case where the sensor may require a more intelligent interface. I only chose a photocell because it was simple and common, and because folks could easily replace it with a potentiometer or even with a switch if they wanted to implement this simple demonstration.

    ReplyDelete
  2. Great post and very helpful.
    Could you provide more details around the xbee setup:
    - which tool did you use. I am using X-CTU
    - did you only configure the 4 paramaters ( PAN ID, firmware, API mode and baud rate )?

    and more importantly, thanks for this procedure.

    ReplyDelete
  3. Hi, thanks for your comment! I do use X-CTU to configure the XBees, and an Adafruit "FTDI Friend" to connect to them. Those are the only parameters that should be needed. Many times I'll also set the Node Identifier (NI) but it's not required by this example. I've just recently been using the newer 2x8C firmware, so far it seems to work fine. Hope this helps!

    ReplyDelete
  4. for whatever reason, the xbees would not talk in API mode...but only with the AT firmware...any clue or idea as to how troubleshoot welcome :)

    ReplyDelete
  5. That seems odd, not sure off the top what would cause that. Glad to help troubleshoot, would you mind posting an item on the Arduino forum? http://arduino.cc/forum/ ... It's a better place than here to troubleshoot, there's a networking section with a lot of chat about XBees, suggest posting there. If it turns out that I can't help you there's lots of other folks there too. Please include all the details that you can.

    ReplyDelete
  6. In case, anyone has the problem, I had to tweak the remote arduino code so that the coordinator address should be set to all 0's. I think hat should be the default value unless you configure specifically the addressing in X-CTU.

    ReplyDelete
  7. It will work with zeros, or the coordinator's actual address can be used. The latter requires less network discovery overhead, but I haven't noticed that using all zeroes slows anything down. I have some newer versions of the code that I need to finalize and git up to github, and I've been using all zeroes lately as well.

    ReplyDelete
  8. Oh, and configuring the destination address (DH, DL) only applies to AT mode. With API mode, the destination address is part of the packet.

    ReplyDelete
  9. hi, thanks for the post.

    could you talk us about how would have to be if we would want to make a XBee only remote sensor, as an end device? i've been surfing internet and i can´t find a clear example for non-electronic people.

    thanks in advance

    ReplyDelete
  10. I haven't actually played with end devices yet, but I do have a couple ideas for using them. Good topic for a future post! In the meantime, I recommend Robert Faludi's book, http://www.amazon.com/Building-Wireless-Sensor-Networks-Processing/dp/0596807732/

    ReplyDelete
  11. The arduino is beneficial for those user who working on projects because it is less expensive chip system and easy to made device board.
    Jaycon

    ReplyDelete
  12. Hi, do you have the code for the Arduino available? Or send it to my mail.

    ReplyDelete
    Replies
    1. The code is on github, there is a link at the beginning of the third to last paragraph.

      Delete
  13. Hi, I have some problem compiling your files in Arduino. First it do not find Xbee.h. And a whole lot of other xbee variables and parameters.

    Do you have some helping tips. If there is something that I am doing wrong...

    thanks

    ReplyDelete
  14. Sounds like you need to install the XBee library. Follow the link in the comment on the #include statement:

    #include //http://code.google.com/p/xbee-arduino/

    ReplyDelete
  15. Can you use Series 1 Xbees in this configurations? Are the Zigbee firmware configs required?

    Thanks in advance!

    ReplyDelete
    Replies
    1. Not as it stands. It's probably possible to adapt it though.

      Delete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. I'm using some power saving options, the power hibernation of the xBee pin and the power_done mode. The problem is that I need to wait for a really long time (about 10 seconds) before I can send a packet. Where am i wrong? This is the sketch: pastebin.com/tFt2vJRm

    ReplyDelete
    Replies
    1. Using end devices and XBee sleep modes is something that I haven't worked much with yet. It's on my list of things to do, but not a high priority at this time. I regret that I not available to debug code and circuits.

      Delete
  18. Hello,
    I wanna ask how to get API Keys. I dont understand. can you give me the steps?
    How to configure Xbee or program arduino when i use multipoint to point? i mean i add 3 Remote Unit. How to configure??

    Thank you in advance.

    ReplyDelete
    Replies
    1. The API keys came from Pachube (since renamed Cosm, since renamed Xively). I haven't used it since it changed to Xively so I don't know how the API key business may have changed.

      I use XBee ZB modules (f.k.a. Series 2). I would configure one as coordinator and the other three as routers. The modules do the rest and organize themselves into a mesh network.

      Delete
    2. me too, i use xbee pro s2b. 1 as Coordinator and 3 other as routers. i mean how arduino's code the routers? are the same?? just like you posted?

      Thanks

      Delete
    3. Yes the routers can all be configured the same.

      Delete
    4. Whats the use of Patchtube feed number ? I just need the data to my coordinator then why use Xively or such service providers?

      Delete
  19. Hello Jack,
    I wanna ask what if float data? how about the code for arduino.

    Thanks

    ReplyDelete
    Replies
    1. Float data can be handled easily enough. If you're having a specific problem, I'd encourage you to post a question on the Arduino forum, forum.arduino.cc.

      Delete
  20. hey jack,
    ive try your code. i just change little.
    i try to send data integer, for example i send 25 from remote to base then upload. but i failed. in xively.com "waiting for request" . what's wrong?
    i've change code = brightness = 25;
    that's it.
    im confused. can you explain.

    Thanks

    ReplyDelete
    Replies
    1. I regret that I cannot debug code and circuits via this blog. Try posting your code and question on the Arduino forum, forum.arduino.cc.

      Delete
  21. Hi friends please help me.....am trying to communicate two arduino uno board via Xbee module without shield is this possible....i need any tips for communicate xbee without shield...

    ReplyDelete
    Replies
    1. I regret that I cannot debug code and circuits via this blog. Try posting your code and question on the Arduino forum, forum.arduino.cc.

      Delete
  22. Hi, Why should the API be set to 2? thanks in advance

    ReplyDelete
    Replies
    1. See https://code.google.com/p/xbee-api/wiki/WhyApiMode

      Delete
  23. Hi, i tried to have communication between Xbee coordinator serie 2 and Xbee router serie 2. the router send data to coordinator but in the other way the coordinator don't send data to router. i use the API mode but nothing was changed . i put the router in API 2 and the coordinator still in API 1

    ReplyDelete
    Replies
    1. Both must be API mode 2. This post is pretty old, I haven't tried it with Xively (fka Pachube). Have a look at this post instead, which uses a different service:
      http://adventuresinarduinoland.blogspot.com/2015/05/a-grovestreams-wireless-sensor-network.html

      Delete