Parsing the serial data from Xbee coordinator through Leonardo

Dear All,

I have created a Xbee + Arduino wireless system to remotely control the LED brightness by a potentiometer. The system components are 2 Xbee chips ( series 1) and 1 Xbee shield (from Sparkfun), 1 Arduino Leonardo, a potentiometer (20k), a LED.

Xbee is configured using API 2 mode (Escape mode). Xbee router is configured as end device. Xbee coordinator is as coordinator.

Router ( transimitter): 1 Xbee and 1 pot. The pot connects to DIO0 pin.

Coordinator ( receiver): 1 Xbee, 1 Xbee Shield, 1 Leonardo, 1LED.

The code is running properly. I can see the serial readout from serial monitor. But the parsing part of code doesnt function well or at all (I guess???). Because when I turn the knob of my pot from the xbee transmitter end, the LED brightness doenst change (actually the LED doesnt light on even). Could someone take a look at the code to see where the problem is?

Also since the data type is 16 bit (API ID identifer: 0x83), I can use the last two bits of infomation before the checksum to find the analog value of pot voltage. But from browsing through the serial readout from serial monitor, the serial readout sometimes (not always) displays 03 255 (which are max value of analog voltage 3.3V) even if the actual voltage stays at 1.2v. I dont know where this readout inconsistency comes from? Thank you guys indeed.

The code I use to receiver RX data is

int myData = 0;
int const LEDPIN = 11;

void setup(){
  // Start up our serial port, we configured our XBEE devices for 9600 bps.
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(LEDPIN,OUTPUT);
  // while the serial stream is not open, do nothing:
  while (!Serial) ;
}

void loop(){


  while(Serial1.available()>0){
    Serial.println(Serial1.read()); //input from Serial1 to Serial
  }
  // send data only when you receive data:


  //if(Serial.available() > 0){

  // myData = Serial.read();
  // Serial.println(myData);
  // }

if (Serial.available() >= 20) { // Wait until we have a mouthful of data
if (Serial.read() == 126) { // Start delimiter of a frame
// Skip over the bytes in the API frame we don't care about
for (int i = 0; i < 10; i++) {
Serial.read();
}
// The next two bytes are the high and low bytes of the sensor reading
int analogHigh = Serial.read();
int analogLow = Serial.read();
int analogValue = analogLow + (analogHigh * 256);
// Scale the brightness to the Arduino PWM range
int brightness = map(analogValue, 0, 1023, 0, 255);
// Light the LED
analogWrite(LEDPIN, brightness);


}
}
}

126
0
10
131
18
52
34
0
1
2
0
1
24 (01 24 are values for analog voltage)
248
126
0
10
131
18
52
34
0
1
2
0
3
255 (03 255 which corresponds to 3.3v are way off the actual voltage input)
15
126

Xbee router is configured as end device. Xbee coordinator is as coordinator.

Then they are not configured correctly. These terms make no sense for point to point models.

Router ( transimitter): 1 Xbee and 1 pot. The pot connects to DIO0 pin.

You have an analog device on a digital pin?

I don't think your problem is in the code.

Hi Paul,

The DIO0 pin is configured as ADC. Then I use the pin to receive analog input.

what do u think?

When using the XBee by itself, with sensors attached, I am pretty sure that the data that it sends is in API mode. The XBee library handles accepting API packets and decoding them. Or, you can do it yourself. Rob Faludi's book "Wireless Sensor Networks" has a lot of information on how to decode an API packet.

The stuff you are printing bears a strong resemblance to an API packet.

Hi Paul,

Yes, I am using API 2 mode. The data type in the Xbee S1 is 0x83. I think I almost solve my primary problem here which prints out the analog voltage in serial monitor of Arduino. But the inaccuracy of voltage readout still exists. I should use Serial1() instead of Serial() when I parse the serial data. The code is attached here.

int const LEDPIN = 11;

void setup(){
  // Start up our serial port, we configured our XBEE devices for 9600 bps.
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(LEDPIN,OUTPUT);
  // while the serial stream is not open, do nothing:
  while (!Serial) ;
}

void loop(){


 // while(Serial1.available()>0){
  //  Serial.println(Serial1.read()); //input from Serial1 to Serial
 // }
  // send data only when you receive data:


  //if(Serial.available() > 0){

  // myData = Serial.read();
  // Serial.println(myData);
  // }

if (Serial1.available() >= 20) { // Wait until we have a mouthful of data
if (Serial1.read() == 126) { // Start delimiter of a frame
// Skip over the bytes in the API frame we don't care about
for (int i = 0; i < 10; i++) {
Serial1.read();
}
// The next two bytes are the high and low bytes of the sensor reading
int analogHigh = Serial1.read();
int analogLow = Serial1.read();
float analogValue = (analogLow + (analogHigh * 256))*3.3/1023;
// Scale the brightness to the Arduino PWM range
int brightness = map(analogValue, 0, 1023, 0, 255);
// Light the LED
analogWrite(LEDPIN, brightness);

Serial.println(analogValue);


}
}
}

But as you can see in the readout. I turn the potentiometer signal to 0 volt. But the serial monitor shows as below. Apparently, max value 3.3 voltages in the readout is not correct. I guess the problem with this is coming from my remote end (transmitting end). Because when I have pot signal connects to DIO0 pin ( configured to ADC), the voltage of pot signal is fluctuated on my multimeter which I use to track the voltage change of potentiometer signal line. DO you think this max out of 3.3v in serial monitor could come from the voltage fluctuation in the transimitting Xbee. Thank you.

0.00
3.30 (Wrong)
0.00
0.00
0.00
0.00
3.30 (Wrong)
0.00
0.00
0.00
3.30 (WRong)
3.30
0.00
0.00
0.00
0.00
3.30 (WRONG)
0.00
0.00
0.00
3.30
0.00
0.00
0.00
0.00
3.30

I think I kind of solve the voltage fluctuation issue. I just switch to use a new explorer for my Xbee transmitting module. And to put the concern of the max voltage the ADC input pins can take, I control the potentiometer's maximum output voltage as 1.2v. But my voltage readout from serial monitor ( Arduino leonardo +xbee receiving module) looks strange. It either stays at 1.2v (which is 03 256 decimal or 03 FF Hex), or 0 volt when pot voltage input is 0. It doesnt matter the pot input voltage changes from 0.1 to 1.2v.

Any thoughts on this issue?

could anyone give any thought on my last issue? Thank you.

Do you guys think anything wrong with my Xbee transmitting module itself,rather than connection issue?