Hello,
I have attached a color sensor on my Lego Mindstorm EV3 Robot from which I'm sending values to Arduino UNO continuously.
After having that I attached an XBee-PRO 900 HP DigiMesh mounted on the Arduino so that it can transmit the color sensor values to second Lego with the help of the Previous Arduino and XBee setup.
Here's the code of what I've written:
#include <XBee.h>
#include <time.h>
#include <stdlib.h>
#include <Wire.h>
#include <Servo.h>
XBee xbee = XBee();
XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
uint8_t data[4];
int instruction[4] = {1,0,0,0};
byte read_byte = 0x00;
int byte_count = 0;
byte* dataPointer;
void setup()
{
Serial.begin(9600); // We will spit it back out on the serial line.
Wire.begin(0x0A); // Start I2C on Address 0x0A
xbee.setSerial(Serial);
Serial.println("StartingUp!");
}
void loop()
{
delay(500);
Wire.onReceive(receiveI2C); // Receive Event from Master
}
// When data is received, this function is called.
void receiveI2C(int bytesIn)
{
while(0 < Wire.available()) // loop through all but the last
{
read_byte = Wire.read(); // Receive the incoming byte
instruction[byte_count] = read_byte;
byte_count++;
Serial.print(read_byte); // print the incoming byte as a character on the Serial line.
}
dataPointer = reinterpret_cast<byte *>(data);
for(int n =0; n < byte_count; n++)
{
*dataPointer = instruction[n];
dataPointer += 1;
}
Serial.println(); // print the incoming byte
ZBTxRequest zbTx1 = ZBTxRequest(ROUTER1, data, sizeof(data));
Serial.print("Data from I2C: "); // print the incoming byte
for (byte n = 0; n < byte_count; n++)
{
Serial.print(data[n]);
}
Serial.println();
xbee.send(zbTx1);
Serial.println("To Router 1");
//transmitXBee();
byte_count = '\0';
}
Now, when I used Serial in the above code:
xbee.setSerial(Serial);
I'm getting values only once. But, as soon as I change my code to Wire:
xbee.setSerial(Wire);
I'm getting values continuously.
Now, my question is that, if I want to transmit to second XBee, I have to make a serial communication. So, I have to keep my code to Serial. But, doing that i'm not able to receive the output on the second XBee.
Here's the code for the receiver part:
#include <XBee.h>
#include <time.h>
#include <stdlib.h>
#include <Wire.h>
#include <Servo.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
//XBeeAddress64 ROUTER1 = XBeeAddress64(0x0013A200,0x4147FE2A);
XBeeAddress64 ROUTER5 = XBeeAddress64(0x0013A200,0x414E65AE);
uint8_t instruction[4] = {1,0,0,0};
uint8_t count = 0;
uint8_t data[4] = {1,0,0,0};
uint8_t receivedData[4];
byte* dataPointer;
uint8_t counter[6][4];
uint8_t dist[6] = {0,0,0,0,0,0};
uint8_t n = 0;
byte read_byte = 0x00;
uint8_t byte_count = 0;
void setup()
{
Serial.begin(9600);
Wire.begin(0x0A); // Start I2C on Address 0x0A
xbee.setSerial(Serial);
Serial.println("Starting Up!");
}
void loop()
{
receiveXBee();
delay(1000);
}
void receiveXBee()
{
xbee.readPacket(500);
Serial.println("Reading for Packet");
if(xbee.getResponse().isAvailable())
{
Serial.print("Packet Available: ");
Serial.println(count);
//delay(500);
if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
{
xbee.getResponse().getZBRxResponse(rx);
//delay(500);
dataPointer = reinterpret_cast<byte *>(receivedData);
for(int n =0; n < rx.getDataLength(); n++)
{
*dataPointer = rx.getData(n);
dataPointer += 1;
}
Serial.print("Packet Received: ");
//for(uint8_t j = 0; j<6; j++)
//{
for (byte n = 0; n < 4; n++)
{
Serial.print(receivedData[n]);
delay(500);
Serial.println();
Serial.print("count: ");
count++;
Serial.println(count);
//delay(500);
}
}
else if(xbee.getResponse().isError())
{
Serial.print("Error Reading Packet. Error Code: ");
Serial.println(xbee.getResponse().getErrorCode());
delay(500);
}
else
Serial.println("No data received");
}
Can anyone suggest any way of how to receive data continuously on the second Router, because I shall be extracting values from the receiver end of the Arduino and feed it into the second Lego Robot to do the further actions.