Communication between arduinos using Xbees

Hello

I'm using 2 arduinos and 2 xbees trying to communicate and send commands to one of the xbees, currently both of the programs can read eachother but there are issues where i can't send data through the Serial Monitor which is the idea we have.

if you guys could help me figure this out, here are both codes:

Main Arduino

#include <SoftwareSerial.h>
#include <XBee.h>

SoftwareSerial serialXbee(2, 3);
XBee xbee = XBee();

XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();

int option;
String msg;

void setup()
{
    Serial.begin(9600);
    serialXbee.begin(9600);
    xbee.setSerial(serialXbee);
    while (!Serial)
    {
    };
    printMenu();
}
void printMenu()
{
    Serial.println("===========OPCIONES===============");
    Serial.println("1) Enviar Informacion");
    Serial.println("...");
}

String sendInfo(String info){
    Serial.print("Enviando la siguiente informcion: ");
    Serial.println(info);   
    serialXbee.println(info);
    return "";
}

void recieveData()
{
    xbee.readPacket();
    if (xbee.getResponse().isAvailable())
    {
        if (xbee.getResponse().getApiId() == RX_16_RESPONSE)
        {
            delay(80);
            xbee.getResponse().getRx16Response(rx16);
            delay(80);
            for (int i = 0; i < rx16.getDataLength(); i++)
            {
                Serial.println((char)rx16.getData()[i]);
            }
        }
        else if (xbee.getResponse().isError())
        {
            Serial.print("ERROR CODIGO: ");
            Serial.println(xbee.getResponse().getErrorCode(), DEC);
        }
    }
}

void rutinaEnvio(){
    msg = "";
    Serial.println("Ingrese la informacion a enviar(5 segundos para escribir)");
    delay(5000);
    while(Serial.available()> 0){      
      char message  = Serial.read();
        msg += message;               
    }
    sendInfo(msg);
}

void loop()
{
    while (Serial.available())
    {
        option = Serial.read();

        switch (option)
        {
        case '1':
            Serial.println("Iniciando Rutina de envio de informacion");
            delay(2000);
            rutinaEnvio();
            break;

        default:
            break;
        }
    }
}

Secundary Arduino

#include <SoftwareSerial.h>
#include <XBee.h>

SoftwareSerial serialXbee(0, 1);
XBee xbee = XBee();

XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();

int option;
String msg;

void setup()
{
    Serial.begin(9600);
    serialXbee.begin(9600);
    xbee.setSerial(serialXbee);
}
void loop()
{
    while (serialXbee.available() > 0)
    {
        char option = serialXbee.read();
        msg = option;
    }
    if (msg == "")
    {
    }
    else
    {
        Serial.println(msg);
    }
}

OUTPUT of both codes

this is not really a great way to handle the Serial input. I would suggest to study Serial Input Basics to handle this

Also in sendInfo, why do you print directly to the xBee Serial port (serialXbee.println(info);) instead of building an XBeeRequest and using your xbee instance to send the request out?

I'll take a look at that documentation thanks you very much.

I'll see hot to implement this in the code that i'm working on. Anyhow i will remove the second arduino, which is supposed to be connected to the computer at all times, and just use the Xbee and a python program to read and write through the COM port, adding the other arduino meant another point of potential failure in the communications

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.