Hardware Reference
In-Depth Information
This function
goes through
each complete message packet byte-
by-byte, assembling the relevant values
from the various bytes. When it's
done, it returns the average of the five
samples in the packet.
Parse It
int parseData() {
int adcStart = 11; // ADC reading starts at byte 12
int numSamples = dataPacket[8]; // number of samples in packet
int total = 0; // sum of all the ADC readings
// read the address. It's a two-byte value, so you
// add the two bytes as follows:
int address = dataPacket[5] + dataPacket[4] * 256;
// read <numSamples> 10-bit analog values, two at a time
// because each reading is two bytes long:
for (int thisByte = 0; thisByte < numSamples * 2; thisByte=thisByte+2) {
// 10-bit value = high byte * 256 + low byte:
int thisSample = (dataPacket[thisByte + adcStart] * 256) +
dataPacket[(thisByte + 1) + adcStart];
// add the result to the total for averaging later:
total = total + thisSample;
}
// average the result:
int average = total / numSamples;
return average;
}
When you run this sketch, you should get a
value between 0 and 1023 every time you get
a message from the sensor XBee radio. Make
sure you're getting good values before you move on. If
you're not, use the first version of the sketch that prints
out the value of each byte to help diagnose the problem.
Seeing every byte that you actually receive before you do
anything with those bytes is the best way to get at the
problem. There are a few common problems to look for:
• Are you getting anything at all? If not, is the sensor
XBee transmitting?
• Does each radio have the correct settings? Connect
them to a USB-to-XBee serial adapter and check in a
serial terminal application.
• Is the receiving radio getting adequate power? Check
the voltage between pins 1 and 10 of the receiving XBee.
If you're using an older Arduino, and you're relying on
the 3.3V output without a regulator, you may not be
giving the radio enough power. The earlier Arduinos
(before the Uno) didn't supply much amperage from the
3.3V output.
Figure 7-9
Until you're finished troubleshooting, it's a good idea to label
your XBee radios so you can tell them apart without having
to check their configurations serially.
Also consider the advice in Figure 7-9 to make trouble-
shooting easier. Once you know you're receiving properly,
move on to adding the web server code.
X
 
Search WWH ::




Custom Search