Arduino to Arduino comunication using Xbees

  arduino, c++, xbee

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. The idea is that the main code from the arduino prints the menu over serial while the secundary arduino sends the options displayed there and then recieves the data from that request

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 the current code

Source: Windows Questions C++

LEAVE A COMMENT