Saturday, November 20, 2010

Xbee and newsoftserial

Receiver Xbee/Arduino
very simple newsoftserial
Overview My Solar Battery Monitor will move into the house via a wireless Xbee link. I have been testing how Xbee's work as a stand alone sensor input.  I just transmit the A/D conversions to my Arduino Xbee in the house every few seconds or so. I looked at what others have done and the Xbee libraries available and decided to use only newsoftserial for the Xbee/Arduino link and sort out the A/D data in the Xbee RF packet with the Arduino. What follows is how I did this, it is simple and should work for any simple use of one or more Xbee's transmitting data to a Xbee receiver connected to an Arduino via newsoftserial. This makes it easy to update the Arduino while listening to what your sensors are sending. Once you decide how to configure the transmitter Xbee's ( # of channels to A/D, which ones, etc) the data packets can be grabbed just by counting the input bytes. My test set up follows closely the Tweet-a-Watt setup at Adafruit.  Get the Xbee Manual .

Sensor Xbee test board. Three inputs
for the A/D's selectable.
Detailed example follows:
I used X-CTU to configure all Xbee's .

The receiver Xbee is default (transparent mode)  with a PAN ID that agrees with all of your transmitter Xbee's. Set all the baud rates the same. This is so you only receive data from YOUR transmitters!

I used one Xbee for transmission, it has three AD inputs A0,A1,A3 active. Configured as follows:
MY (16bit source) = 1 the first and only unit transmitting at this PAN ID
SLEEP MODE = 4  cyclic  wakeup every 2 seconds transmit data go to sleep
ST =3  Time after last sample to go back to sleep. (ms)
SP = C8  sleep period  2 seconds (Hex in 10ms steps)
D0, D1, D3 = 2   This enables A/D on these inputs
IT = 4  sample each input 4 times
IR =1  time (ms) between samples
Thats all; just supply this Xbee with power and hook up the three inputs. You will see the power lite blink every 2 seconds and if you look closely the red data lite blinks also for the one RF packet that is sent.

Set up newsoftserial between the receiver Xbee and the Arduino like the Adafruit example

In my example the sampled RF packet bytes = 35 as follows:
7E, 0, 20, 83, 0, 1, 4F, 0, 4, 16, 0, 1, C1, 0, 9C, 3, 64, 1, C1, 0, 9C, 3, 64, 1, C2, 0, 9C, 3, 64, 1, C1, 0, 9C, 3, 64, FD, 
My code to test xbeePacketsize
Quick view of my Headers:
8 bytes of RF header; 3 bytes of Data header; Data values, checksum.

  1. 7E Packet start delimiter
  2. 00 MSB length
  3. 20 LSB length  Length is 32 + 3 bytes already; end is at byte 35
  4. 83 ATI identifer..receive 16 address packet data, the only mode I use.
  5. 00 MSB 16 bit source address
  6. 01 LSB  16 bit source address in my case 1
  7. 4F RSS RF signal strength -75dBm.  I'll plot this vs distance soon. This is typical for wireless.
  8. 00 reserved Options 
  9. 04 Total number of data samples (a/d conversions) 4 in  my case
  10. 16 bitwise indication of channels enabled A/D see manual. This is how you setup the transmitting Xbee. 
  11. 00 bitwise indication of digital enabled pins on the transmitting Xbee. None in my case.
  12. 01 MSB A/D first conversion for pin A0 in my case.  1*256 + LSB
  13.  AB LSB A/D first conversion for pin A0. Value is  MSB + 160 + 11 or 427 (0-1023 possible)
.... to byte 35.
Remember I count from 1's but arrays are indexed from 0. I sometimes mix them up, be careful! Count!
My data starts at byte[11] and ends at byte[34] and  byte[35] is the checksum. My case 3channels*4samples*2bytes/value = 24 more bytes. 11 + 24 inclusive is 34. 34 is the last data byte.

All data in this xbee mode will start at byte[11] and be (num sampled channels) * (num A/D conversions) * 2 bytes of data long.
It is simple to now fill a dataArray[3][4] with the outputs of the xbee A/D values for each conversion
My code xbeeDataArray

Hope this helps.


  1. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you gotta watch this video
    right away...

    (VIDEO) Text Your Ex Back?

  2. If you're trying hard to lose weight then you have to start using this brand new custom keto diet.

    To produce this keto diet, certified nutritionists, fitness trainers, and cooks have united to develop keto meal plans that are powerful, painless, money-efficient, and satisfying.

    Since their first launch in January 2019, hundreds of people have already remodeled their figure and well-being with the benefits a smart keto diet can give.

    Speaking of benefits: clicking this link, you'll discover eight scientifically-proven ones given by the keto diet.