Send data with XBee and Arduino

Hello!

I have an Arduino Micro, a RGB sensor (TCS34725) and a XBee SX868 (https://www.digi.com/resources/documentation/digidocs/pdfs/90001538.pdf) with his development board.

I send the data via UART to the XBee with the following code (if you detect some fail let me know, please):

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "ColorConverterLib.h"
 
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X);
 
void setup()
{
  Serial.begin(9600);
 
 
  if (!tcs.begin())
  {
    Serial.println("Error TCS34725");
    while (1) delay(1000);
  }
}
 
void loop()
{
  uint16_t clear, red, green, blue;
 
  tcs.setInterrupt(false);
  delay(1000); // Cuesta 50ms capturar el color . ANTES 60 en vez de el valor actual del delay
  tcs.getRawData(&red, &green, &blue, &clear);
  tcs.setInterrupt(true);
 
  // Hacer rgb medición relativa
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
 
  // Escalar rgb a bytes
  r *= 256; g *= 256; b *= 256;
 
  // Convert to hue, saturation, value
  double hue, saturation, value;
  ColorConverter::RgbToHsv(static_cast<uint8_t>(r), static_cast<uint8_t>(g), static_cast<uint8_t>(b), hue, saturation, value);
 
  // Show color name
  printColorName(hue * 360);
 
  delay(1000);
}
 
void printColorName(double hue)
{
  if (hue < 15)
  {
    //Serial.println("Rojo");
    Serial.write("Rojo");
  }
  else if (hue < 45)
  {
    //Serial.println("Naranja");
    Serial.write("Naranja");
  }
  else if (hue < 90)
  {
    //Serial.println("Amarillo");
    Serial.write("Amarillo");
  }
  else if (hue < 150)
  {
    //Serial.println("Verde");
    Serial.write("Verde");
  }
  else if (hue < 210)
  {
    //Serial.println("Cyan");
    Serial.write("Cyan");
  }
  else if (hue < 270)
  {
    //Serial.println("Azul");
    Serial.write("Azul");
  }
  else if (hue < 330)
  {
    //Serial.println("Magenta");
    Serial.write("Magenta");
  }
  else
  {
    //Serial.println("Rojo");
    Serial.write("Rojo");
  }
}

The wiring between the Arduino and the XBee are: common GND, TX to DIN and RX to DOUT.

My XBee is working in API mode 1 and I believe that it is correctly configured because of with XCTU software I can send text (‘RF data’ field in ‘Transmist Request’ frame) and it is received perfectly in another XBee.

The point is that I don’t know how I can put the data that comes from the Arduino (via UART) into a frame and send it. I don’t know if I can do that with XCTU or I should use another program.

If someone can provide me some information of how to achieve this goal it would be great!

Thanks!

My XBee is working in API mode

But you are not sending API packets AND you are not expecting the XBees to send data independently of the Arduinos. So, put the XBees in AT mode.

It would be useful for you to understand the difference between print() and write() and when each is normally used.

The point is that I don't know how I can put the data that comes from the Arduino (via UART) into a frame and send it.

If you used AT mode, you wouldn't need to. If you insist on doing things the hard way, use Andrew Rapp's XBee library that does the packeting and checksum calculations for you.

First of all, thanks for your reply. Ok I will try the AT Mode and if I get success I will follow with the API Mode.

But I have two questions:

  1. How can I send the AT frames to another specific XBee? Because in the XCTU I can not find the option and in the arduino I do not know how it would be specified

  2. What do I have to do in the AT mode so that the frames coming through the DIN UART pin of the XBee are sent?

  1. How can I send the AT frames to another specific XBee?

The XBees I'm familiar with have a SH/SL (source) or MY (point to point models) address and a DH/DL (destination) address. The destination address is the device that they talk to. Set the DH/DL address of one to the SH/SL address of the other, or set the DH to 0 and the DL value of one to MY of the other. Then, each radio can talk to exactly one other radio.

  1. What do I have to do in the AT mode so that the frames coming through the DIN UART pin of the XBee are sent?
  1. There are NO frames in AT mode.
  2. Data coming into the XBee, over the air, goes out the DOUT pin. Data that comes into the DIN pin goes out over the air, without you needing to do a thing.

Ok, then I am doing something wrong because in the monitor serie of the Arduino Software I can see the colour that the sensor is reading but in the XCTU I can't see the data that comes through the UART pin.

So, or the Arduino code that I attached has some mistakes (maybe I should use another way to send the data) or the wiring is wrong( as I wrote yesterday TX to DIN, RX to DOUT and common GND).

Thanks!

A schematic of both ends of your system is going to be necessary.

How is the data on the receiving end supposed to get to X-CTU? Why on earth are you using THAT software to test communications involving Arduinos?