Hi.
I'm trying to send a simply message by RF using two xbee (one is a coordinator and the other a router) but I'm struggling on it. Both nodes are in the same network since I already checked Operating channel, Operating network and association status is 0. Besides, I already send successfully Node discovery from the router (I know that is useless in this case since there are only two nodes in my network but I want to create in the future a mesh network so started off from the basic) receiving the proper respond.
This is my packet that I send from my router.
byte Request_Frame[] = {0x7E,0x00,0X13,0X10,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X48,0X65,0X6C,0X6C,0X6F,0X92};
As the message is going to be transmitted to my coordinator, my 64bits address, as well as 16bit address should be 0 for what I read in the guide.
This is router's code
#include <Arduino.h>
#define OK "OK"
#define ERROR "ERROR"
#define CR '\r'
#define LR '\n'
unsigned long t = 0;
byte nodeDiscovery[] = {0x7E,0x00,0x04,0x08,0x01,0x4E,0x44,0x64};
byte Request_Frame[] = {0x7E,0x00,0X13,0X10,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X48,0X65,0X6C,0X6C,0X6F,0X92};
int sendATCommands(char AT[], char *expected1, char *expected2, long unsigned int timeout = 1000){
char response[10];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print(AT);
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
else if ((i > 0))
{
if ((memcmp(response, "OK",2) == 0)){
Serial.println("OK");
return 1;
}
else if ((memcmp(response, "ERROR",5) == 0)){
Serial.println("Error");
return 2;
}
}
}
}while (millis() - last < timeout);
Serial.println("NULL");
return 0;
}
char* readPANID(char PANID[]){
char response[10];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print(PANID);
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
}
}while (millis() - last < 1000);
Serial.println((String)response);
return response;
}
void readOperatingPAN(char PANID[]){
char response[20];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print(PANID);
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
}
}while (millis() - last < 2000);
Serial.println((String)response);
}
void readOperatingID_16bits(char ATOI[]){
char response[20];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print(ATOI);
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
}
}while (millis() - last < 2000);
Serial.println((String)response);
}
void readMYID(char MYID[]){
char response[10];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print(MYID);
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
}
}while (millis() - last < 2000);
Serial.println((String)response);
}
void readOperatingChannel(){
Serial.print("Operating Channel:");
char response[10];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print("ATCH\r");
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
}
}while (millis() - last < 1000);
Serial.println((String)response);
}
void Association_Report(){
Serial.print("Association Report:");
char response[10];
memset(response, '\0', sizeof(response));
while (Serial2.available() > 0)
Serial2.read();
Serial2.print("ATAI\r");
delay(300);
int i = 0;
unsigned long last = millis();
do{
while (Serial2.available() > 0){
response[i++] = Serial2.read();
if (((i == 2) && (response[0] == CR) && (response[1] == LR)) ||((i == 1) && (response[0] == LR)))
i = 0;
}
}while (millis() - last < 2000);
Serial.println((String)response);
}
unsigned int GetLength(uint8_t *raw_data){
byte MSB = (byte)raw_data[0];
byte LSB = (byte)raw_data[1];
unsigned int length = (((unsigned int)MSB << 8) | (unsigned int)LSB);
Serial.print("Lenght is:");
Serial.println(length);
return length;
}
void Checksum_Verify(uint8_t *message, unsigned int size){
uint16_t total = 0;
byte LSB;
for (register int i = 0; i <= size; ++i)
total += message[i];
LSB = total & 0xFF;
if (LSB == 255)
Serial.println("Checksum Correct");
}
void NodeDiscovery(){
bool Waiting_Message = true;
unsigned int size_message = 0;
uint8_t length [2];
register int cont = 0;
while (Serial2.available() > 0)
Serial2.read();
for (int c=0; c < 8;c++)
Serial2.write(nodeDiscovery[c]);
unsigned long t = millis();
do{
while (Serial2.available()){
if (Waiting_Message){
char aux = Serial2.read();
Serial.println(aux,HEX);
if (aux == 0x7E ){
Serial.println("Received Started byte");
Waiting_Message = false;
}
}
else{
if (cont == 0){
Serial2.readBytes(length,2);
Serial.print(length[0]);
Serial.print(" ");
Serial.println(length[1]);
size_message = GetLength(length);
++cont;
continue;
}
else if (cont == 1){
uint8_t *p = (uint8_t*)malloc((size_message+1)*sizeof(uint8_t));
Serial2.readBytes(p,size_message+1);
Checksum_Verify(p, size_message);
return;
}
}
}
} while (millis() - t < 10000);
return;
}
void SendATFrame(){
unsigned int size = sizeof(Request_Frame);
Serial.print("Sending ...");
while (Serial2.available() > 0)
Serial2.read();
for (int c=0; c < size;c++){
Serial2.write(Request_Frame[c]);
}
}
void AT_Mode(){
if (sendATCommands("+++",OK,ERROR,10000) != 1){
Serial.println("Error AT Mode");
AT_Mode();
}
return;
}
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
//Enter to AT Mode
AT_Mode();
//Set or read the 64-bit extended PAN ID
char *PANID = readPANID("ATID\r");
//Read the 64-bit extended PAN ID. The OP value reflects the operating extended PAN ID where the
//device is running. If ID > 0, OP equals ID.
readOperatingPAN("ATOP\r");
readMYID("ATMY\r");
readOperatingID_16bits("ATOI\r");
readOperatingChannel();
Association_Report();
if (sendATCommands("ATJV 1\r", OK, ERROR)!= 1)
Serial.println("Error JV");
if (sendATCommands("ATAP 1\r", OK, ERROR) != 1)
Serial.println("Error JV");
if (sendATCommands("ATDH 0\r", OK, ERROR)!= 1)
Serial.println("Error DH");
if (sendATCommands("ATDL 0F\r", OK, ERROR) != 1)
Serial.println("Error DL");
if (sendATCommands("ATDL 0F\r", OK, ERROR) != 1)
Serial.println("Error DL");
Serial.print("ATAC:");
sendATCommands("ATAC\r",OK,ERROR);
Serial.print("ATWR:");
sendATCommands("ATWR\r",OK,ERROR);
Serial.print("ATCN:");
sendATCommands("ATCN\r",OK,ERROR);
NodeDiscovery();
delay(10000);
SendATFrame();
}
void loop() {
static unsigned long t = 0;
if (millis() - t > 60000L){
SendATFrame();
t = millis();
}
while (Serial2.available()){
char c = Serial2.read();
Serial.println(c,HEX);
}
}