Go Down

Topic: Problem reading XBee API packet (Read 1 time) previous topic - next topic

Dan_Lovell

Hi, I have 2 series 1 xbees, 1 setup as a remote sensor connected to an ADXL335. The other as a Coordinator connected to an arduino uno via a funduino XBee shield.

I have setup the XBees and am receiving data to my serial port using code uploaded to my arduino. The trouble is I am expecting consistent packets and i seem to be getting random packets interspersed with what i believe are correct packets from which I can retrieve my analogue data (I have attached a screen grab of my serial monitor with some annotation).

Below is the code uploaded to the arduino uno. At the top i have written my XBee settings.

Any help in trying to understand why i'm getting this seemingly inconsistent output would be greatly appreciated. I have tried to give all the information I have about my setup, but if you feel I have missed something please let me know and I will provide anything you require.

Code: [Select]


/*XBee Setups
 *** Configuration ***

SENDER: (REMOTE SENSOR RADIO)
 ATCH -> C
 ATID -> 3332
 ATDL -> 1
 ATMY -> 2
 ATNI -> SENSOR
 ATBD -> 3 (9600)
 ATAP -> 2 (API enabled w/PPP
 ATD0 -> 2 (ADC)
 ATD1 -> 2 (ADC)
 ATD2 -> 2  (ADC)
 ATIR -> 14 (20ms)

CORDINATOR:
 ATCH -> C
 ATID -> 3332
 ATDL -> 2
 ATMY -> 1
 ATNI -> COORDINATOR
 ATBD -> 3 (9600)
 ATAP -> 2 (API enabled w/PPP
 
*/


void setup()
{
  Serial.begin(9600); //Begin serial connection
}
void loop()
{
 
  if (Serial.available()>=17){ //Check for a full API packet
    if (Serial.read() == 0x7E){//If start delimiter is read 
      for(int i = 0; i < 17; i++){ //For the rest of the frame
        Serial.print(Serial.read(),DEC); //Print each Byte read
        Serial.print(","); //Print a comma between each
        }
        Serial.println(); //Println so the next packet starts on the next line
    }
  }
}


PaulS

Code: [Select]
  if (Serial.available()>=17){ //Check for a full API packet
    if (Serial.read() == 0x7E){//If start delimiter is read
      for(int i = 0; i < 17; i++){ //For the rest of the frame
        Serial.print(Serial.read(),DEC); //Print each Byte read
        Serial.print(","); //Print a comma between each
        }
        Serial.println(); //Println so the next packet starts on the next line
    }

If there are 17 bytes available, it is NOT OK to read 18 bytes.

Howaboutprintingsomespacesbetweenvalues?

Let's see what happens when you read data properly.
The art of getting good answers lies in asking good questions.

Dan_Lovell

Paul, thank you for your response.

I was under the impression that there are 18 bytes available in my frame, 1 start delimiter (which is read but not printed) then the LSB & MSB which inform me that there are 14 bytes following before the checksum. I total that at 18 :/.

Do you mean I should print spaces between my values instead of commas?

Many thanks Dan

PaulS

Quote
I total that at 18
I do to. So, why are you reading 18 when there are 17 available?

Serial data arrives slowly. The Arduino will execute loop(), doing nothing, thousands of times between there being 16 bytes to read and there being 17 bytes to read. Make it run a few more thousands of iterations doing nothing until all 18 bytes have arrived.
The art of getting good answers lies in asking good questions.

Dan_Lovell

Quote
why are you reading 18 when there are 17 available?
Are you referring to
Code: [Select]
if (Serial.available()>=17)
I was under the impression that the count started from 0, so with this i am waiting until there are 18 or more Bytes available. Then I use
Code: [Select]
for(int i = 0; i < 17; i++) 
to read 17 Bytes (less the start delimiter which has already been read) and subsequently print them. So i thought this code waited until there were 18 Bytes available then read 18.

Can you please identify which part of this I have misunderstood.

Many thanks Dan

PaulS

Quote
I was under the impression that the count started from 0
Serial.available()>=17 will evaluate to true as soon as there are 17 bytes to read. 17 is STILL less than 18. Serial.available() returns 0 when there is nothing to read.

Quote
So i thought this code waited until there were 18 Bytes available then read 18.
No. It waits for 17 bytes to be available.
The art of getting good answers lies in asking good questions.

Dan_Lovell

I have changed the code to read
Code: [Select]
if (Serial.available()>=18)
But I get the same result in the serial monitor  :smiley-confuse:

Many thanks Dan

PaulS

Quote
But I get the same result in the serial monitor
I can't believe how many times I looked at that code and missed the fact that you are reading from the XBee on the hardware serial port, and then writing to the XBee on the hardware serial port, as well as the PC.

You need to use two different serial ports - one to talk to the PC and one to talk to the XBee. You can use SoftwareSerial to talk to the XBee, after you connect it to different pins.
The art of getting good answers lies in asking good questions.

Dariostesh

Since I had the same problems on my <Arduino One - XBee shield - XBee> composition, it's interesting to understand this argument.
Paul, you say:

I can't believe how many times I looked at that code and missed the fact that you are reading from the XBee on the hardware serial port, and then writing to the XBee on the hardware serial port, as well as the PC.

You need to use two different serial ports - one to talk to the PC and one to talk to the XBee. You can use SoftwareSerial to talk to the XBee, after you connect it to different pins.
But on this page https://www.arduino.cc/en/Main/ArduinoXbeeShield, there is written that when using the XBee shield with jumpers in the XBee position, the Arduino microcontroller can send to both the XBee and the PC, while receiving only from XBee.
Does it mean you anyway have to use two different serial ports? I thought that using the hardware serial port (pin 0 and 1), I would have had no problems in getting the message sent to XBee.
If I power Arduino on with the external jack and not the USB, what will happen? I think this way you only will have the communication between Arduino and XBee, but I'm having problems in communication also in this case.

Thanks,

Regards.
Dario Parastesh

PaulS

Quote
But on this page
This would be the first time you've mentioned how the XBee is connected to the Arduino. Though you didn't actually say that you are using that shield...

So, lets make it very clear. Just how is the XBee connected to the Arduino?
The art of getting good answers lies in asking good questions.

Dariostesh

This would be the first time you've mentioned how the XBee is connected to the Arduino. Though you didn't actually say that you are using that shield...

So, lets make it very clear. Just how is the XBee connected to the Arduino?
Sorry, you're right. Yes, I am using an Arduino One - XBee shield - XBee composition. I have initially removed the ATMEGA328p and put the jumpers on the USB position to use the XBee shield and XBee coupled with Arduino One with XCTU software of Digi. The radio module was working regularly.
Then I used the API mode library by andrewrapp, here https://github.com/andrewrapp/xbee-arduino, to load a sketch on Arduino One which send a frame packet to a specific destination address, which is not an XBee, but a Telegesis radio module, in particular the ETRX357 radio module.

Here the code:

Code: [Select]
#include <XBee.h>

// Create an XBee object at the top of your sketch
XBee xbee = XBee();

void setup() {
  // put your setup code here, to run once:

  // Start the serial port
Serial.begin(9600);
// Tell XBee to use Hardware Serial. It's also possible to use SoftwareSerial
xbee.setSerial(Serial);

pinMode(13,OUTPUT);
digitalWrite(13,LOW);

}

void loop() {
  // put your main code here, to run repeatedly:

  // Create an array for holding the data you want to send.
uint8_t payload[] = { '3', ',', 'A', ',', '5', '0', '0', ',', '0' };

delay(500);

// Specify the address of the remote XBee (this is the SH + SL)
XBeeAddress64 addr64 = XBeeAddress64(0x000D6F00, 0x0AB9FA93);

delay(500);

  // Create an explicit TX Request
ZBExplicitTxRequest zbTx = ZBExplicitTxRequest(addr64, 0xfffe, 0, 0, payload, sizeof(payload), 0, 0xE8, 0x01, 0x0002, 0xC091);

delay(500);

// Send your request
xbee.send(zbTx);

  delay(1000);
  digitalWrite
  (13,HIGH);
  delay(1000);
  digitalWrite(13,LOW);

delay(5000);

}


When loading the sketch on Arduino One, I tried the code by itself observing on the serial monitor if it was successful in transmitting. And it was, since the TX LED blinked when a message was sent to the serial monitor. I also put the pin 13 ("L" LED) as a controller.
Then I put the jumpers on the XBee shield in XBee position, I tried to couple Arduino, the XBee shield and XBee itself. When powering Arduino on with the USB cable, I  couldn't see any TX LED blinking, and the L LED on Arduino was green LED fixed for all the time. This gave me the impression Arduino was not able to serially talk with XBee, but I'm not sure of that.
Why is not transmitting if so?

Thanks a lot,

Regards.
Dario Parastesh

Dan_Lovell

Paul - as stated in my first post my Xbee is connected to arduino uno using a funduino Xbee shield

Dariostesh

Dan,

your problem is similar to mine, but I'm using XBee Series 2 Pro.
When I connect Arduino One, the shield and XBee, it seems that nothing happens and the "L" LED become fixed green.
I also tried to reset XBee to default values and connect it to Arduino One again but it seems that no transmission is going on.

Thanks,

Regards.
Dario Parastesh

Dan_Lovell

Dariostesh,

No you have misunderstood my problem entirely! Please read carefully my first post. I am transmitting data perfectly well and receiving it!

My problem is that I am not able to print the serial packets consistently to the arduino serial monitor.

I get the output that I screen grabbed and posted when i expect to see something like

0,14,131,0,2,51,0,1,14,0,1,243,1,252,2,123,202
0,14,131,0,2,51,0,1,14,0,1,243,1,252,2,123,202

...repeating consistently, however I get some lines where this in not the case. The problem is in my code somewhere. The hardware is set up and working perfectly.

Dariostesh

Ok, it's probably not the same problem.
I will try opening a new issue.

Thanks,

Regards.
Dario Parastesh

Go Up