Pin Sleep Xbee with Arduino Host

Making it work.

I got everything to function with a rather messy board setup as shown below.

The output from the Arduino shows the delta T between messages received from the end-devices.  It is pretty close to the calculated ones. I will change the duration to be 15 minutes later on but for debugging purposes 10s intervals for pin sleep is tolerable.


The following are the libraries were referenced.

Arduino library for communicating with XBees in API mode – I used both Java and C++ versions for this.

Standard Template library (STL) for AVR – I wanted some maps and other stuff in the dev environment.

Streaming C++ style Output – print and println gets to be a pain after a while

Ardunio Modbus Slave - I used this for my energy monitoring project as well and hacked in a modbus function 6. Since then, a new library that includes function 6 has been deployed, but I still used the older version as I wrapped the C++ class – SerialModbusSlave and did not feel like manually merging code.


I leveraged the serial ports available in the Mega2560 and created global variables to reference instances of the serial port as follows:

Which allowed me to do things like the following:

I can change ports around in one spot in the code rather than find an replace things.

I opted to over design things a bit as I wanted to avoid changing the code later on as tacit information tends to be forgotten over time. What I wanted to do is be able to provision new end devices without changing the arduino code and follow a simple 4 step process:

  1. update to appropriate firmware in XBee, set Pan ID, and API mode 2
  2. utilize the home grown Java app to set up the details such as IO types and lines  ( I am thinking of adding a clone function to make this even simpler)
  3. wire up xbee related circuitry
  4. provision the end device in the Arduino via Mango HMI over Modbus

The following illustrates the concept in a non-formal way of describing software. When a ioresponse is received from an end device I look up the corresponding meshdevice entry in map using the string representation of the address64. I was thinking of using int64_t  type rather than a string as the string takes more space but that is not at a premium at this point.

The address64 string for managed end devices are stored in eeprom and loaded on setup(). Provisioning of end devices are sent over modbus from the HMI host and written to eeprom as well. Yes overkill, but t I want to evolve the system and focus on some abstraction to allow the evolution the meshdevice representation. All the smarts to evolve will be confined in one class. The tacit information like how to provision and communicate with the HMI, etc. will hopefilly remain static.

Map of Meshdevices

A map of using the STL for AVR to reference the MeshDevice is as follows:

And updating meshdevices is coded as follows and will change later on as the error handling is uber weak.

Modbus Register Buffer

Lastly the mapping to modbus style registers are set up as clusters for each device and terminated with a set of register to accept commands and address64 strings from the host. The code for it is rather simple and I will shoehorn this later on. Up to 13 discrete (digital) and 5 analog I/O are reserved for each device. What is used depends on how the XBee device is configured which is explained in the XBee datasheet 90000976_D Datasheet
















The prototype basically implemented the following data flow model. The issue for me is battery powered end device is not green enough of a solution for me.  I’m thinking of exploring energy harvesting to help things along. The plan was to also send battery voltage level on the end device to the SCADA host to generate low battery alarms. Still, it would be nicer to tap into ambient environment (vibration, etc) to generate energy to help power the end device. This will be explored later.

Sleep Mode

I opted for hardware driven sleep mode (SM=1) in the XBee. I felt that that cyclic sleep mode (SM=4) to be a pain to setup. I felt that that keeping the XBee asleep for extended durations, the device would be MIA from host. With a hardware based sleep, I could use a swith to to force  it awake, configure remotely with the software tool I wrote, flick the switch back to the timer based sleep. Simple in my mind.

The tested scenario is illustrated below and it worked as planned. The XBee would be taken out of sleep for 30 ms and back to sleep for 100 seconds. As stated earlier, the power consumption does not lend itself to using batteries. xbee visio

I found this link for an alternative to the 555 and will look into it.  I would also not require a level shifter and can operate from a single 3.3 battery. This would surely extend the battery life as it would remove the dependency on the 78L33 and 555.  Now I just need to deal with the LM35 and other sensors and I think I have a way to deal with that and that is to keep them “offline” when sleeping. More experimentation.

New Project – Xbee Mesh Network and Data Collection

I needed a project to dabble and distract me in a constructive manner.  The goal for this project is to build a low power data collection network to sample  slow changing analog data such as temperature, ambient lighting, among other things. The constraint given was to use XBee Series 2,  run in API mode, and operate on batteries.

I purchased 5 XBee Series 2s to experiment with. I used X-CTU software to configure the Xbee chips as per the last project I did. The difference this time is I did not want to use AT mode but API mode to give me more control over XBee interactions from the Arduino host software. The  project context is as shown below.

Arduino to-be

The XBee USB Explorer was used and I soldered break away pins and a push button switch between ground and reset.   When I first load an XBee, I select the proper port with default settings. Query the device and you should get a dialog box pop up as shown below.

xctu read new

My XBees don’t come with the latest firmware and perusing the web about which is the latest for the Series 2 indicated that that the ZB supersedes the ZNet stuff.   This is where the push button approach comes in and described here. I picked the XB24ZB firmware with the correct device type. I included 1 Coord, 1 Router, and 2 End Devices. I used a PAN ID of 47 to setup the devices and ensured that I included that in my firmware configuration.

x-ctu default

I  get the following error and that is where the push button wiring that connects reset to ground comes in. I just push the switch for a couple of seconds, release, then upgrade continues. I has something to do with the new firmware and default sleep mode and described here.

x-ctu programmingOnce the new firmware is installed, I head back to the PC Setting tab and select API mode as shown below and set the include escape character as well.

x-ctu api

Now I see the the latest firmware upgrade.

xBee, Debugging, et al

Relatively Easy

I’ve been busy with work and never have the time to get back to this project. Anyway, I penciled in a few hours to wire up the xBee transmitter/receiver to the arduino. The xBee explorer is a time saver. What I wanted to do is to program the arduino over wireless as well. Given that time is limited, I opted to just send data over wireless to simplify connectivity.

The xBee explorer had 4 connection points that made it easy to hookup:

  • +5 to arduino +5
  • gnd to arduino ground
  • dout to rx (pin 1 of ardiuino)
  • din to tx (pint 2 of arduino)

That is pretty all that is required to wire up the xbee. The explorer takes care of the 5v to 3.3v level shifting.  Remember I am using the Duemilanove as the reference board. Note that you can’t plug in the USB in the Duemilanove and xBee in the explorer at the same time. The two will conflict with each other. This is rather trivial to hook up so no wiring diagram other than pin connection mapping is provided.

On the host side, I used the USB explorer which is basically leads to a plug and play model which I like.  Rather than regurgitate xBee configuration, I found that it is well explained here. (Xbee Configuration tutorial)  I tweaked the PAN, Baud rate, and type. I made the host a coordinator and the slave device a routing type. Again, explained well in the tutorial.

Just like the other parts of this project, I found soldering and wiring it up the hardest. I used left over wire wrap wires (I think the stuff is 20 years old) to connect things. I don’t wire wrap them and still solder. I like it because it is not tot bulky. It does break easy though.   Anyway, I have a renewed appreciation for those who package hardware. Although mine works, only a mother could love the way it looks. Alas, it is for my own use so prototype mode is good enough.


Current Transformer Revisited

I revisited the current transformer wiring in the context of safety. Rather than use terminal blocks, I should have used simple audio jacks to simplify the wiring. Either way, there was a safety issue that loomed in the back of my head and I wanted to address it before wiring the sensors to the power mains. The secondary in an open circuit mode of a transformer (not connected to anything) can reach a high voltage and should it come in contact to a person, guess who is going to be the resistor for that voltage to close the circuit. You.  It does not take much to kill you.

What I did was add a switch that shunts (shorts) the secondary of the transformer that I flip to “field work mode” before handling the terminals.  This is not like shorting a 120 AC line. The voltage on the secondary is the function of the reflection of the impedance on the primary. This can get mathematical so I will spare the math. Suffice it to say that shunting the secondary won’t hurt the current transformer and will add another level of safety.

current xformer


Measuring Current

There are two ways I am willing to entertain for measuring current. One is via a current transformer and the other vial hall effect.

I combed the web to look for affordable devices in North America. It seems that Europe has access to cheaper current transformers.  I ended purchasing three  (3) current transformers and two(2) hall effect chips.

Two (2) revenue Grade Current Transformer Two from CR Magnetics to install on each side of my 200A pannel per each side  (CR8459-2000-N)

One (1) general purpose 0-20A for experimentation CR Magnetics  (CR8410-1000)

Two (2) Hall effect bases chips from Allegro ACS712ELCTR-20A-T

I ordered on-line via Digi-Key and found to have an excellent customer experience. I opted for one stop shopping for other goodies such as zigbee chips and other odds and ends. I also wanted something Canada friendly so I picked a place that operates in both Canada and US.

I entertained the idea of spit-core transformers to facilitate wrapping around the wire. I could not find some at a cheap rate and figured some higher quality ones could be used for permanent installation. I’ve done enough wiring to feel comfortable inside the panel box. I would not recommend venturing in high voltage areas without a good understanding what can harm you. Get an electrician to wire those if you have any doubts.

Measuring Voltage

I have a bunch of AC transformers lying around so I plan on using those to measure the AC voltage.  120:10 volts p-p will do as that is all I have in stock.

Embedded System
I entertained the idea of using a multi-tasking OS such as The FreeRTOS Project and finding a board to host it. The KISS principle forced me to focus on deploying hardware to perform specific tasks and avoid creeping elegance. I only want a device that at will feed into the mesh network. No sense over engineering things. I can use the FreeRTOS for a data concentrator later on. There were many choices out there e.g.  PIC and Micromint. I opted for simplicity and went with the Arduino Duemilanove.

I purchased my Aduino from spikenzielabs along with the following to help me get ball going.

  • Arduino Duemilanove w/ Atmega328
  • XBee Explorer USB
  • Adafruit Proto Shield for Arduino
  • Basic 16×2 Character LCD – White on Black 5V
  • Breakout Board for XBee Module

power parts

Time to plug things in and see what happens.