Arduino Receiver Code when Transferring Data via Xbee

Hello!

I am working on a project at the moment where I have a remote Arduino collecting input from 4 sensors, 2 thermocouple and 2 accelerometer.

I have a functioning code and physical connection set up for the sensors which operates well when plugged straight from the Arduino to the PC however I want to send this data wireless via Xbee to another Arduino.

I have the Xbee’s programmed and they are communicating with each other through typed messages.

Basically my question is, what should the code look like on the Arduino receiving this data in order to display it on the serial monitor in the same way it appeared when plugged straight from the Arduino to computer?

This is the code from the Arduino collecting the data from the sensors.

Many Thanks.

Complete_Code.ino (2.41 KB)

1.png

1 Like

Complete TX Code (please post this in your initial post next time):

// Defining which junction on the first thermocouple board connects with which port on the Arduino
#define CS1 11
#define MISO1 12
#define SCLK1 13

// Defining which junction on the second thermocouple board connects with which port on the Arduino
#define CS2 5
#define MISO2 6
#define SCLK2 7

// Requesting Downloaded Libraries
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_MAX31855.h"
#include "SparkFun_MMA8452Q.h"

MMA8452Q accel1; // Creation of the first accelerometer
MMA8452Q accel2; // Creation of the second accelerometer

Adafruit_MAX31855 Temp1(SCLK1, CS1, MISO1); // Creation of the first Thermocouple
Adafruit_MAX31855 Temp2(SCLK2, CS2, MISO2); // Creation of the second Thermocouple


void setup() {
  Serial.begin(9600); // Starting serial communication
  Wire.begin();

  if (accel1.begin() == false) {
    Serial.println("First Accelerometer Not Connected. Please Check Connections");
    while (1);
  }

  if (accel2.begin(Wire, 0x1c) == false) { // The second accelerometer uses the altered unique I2C address of the board.
    // Since default settings have changed both the wire port and new I2C address are indicated within accel2.begin().
    Serial.println("Second Accelerometer Not Connected. Please Check Connections");
    while (1);
  }
}

void loop() {
  if (accel1.available() && accel2.available()) {  // Waiting for new data from both accelerometers
    // Acceleration of x, y, and z directions in g units for both sensors

    float x1 = accel1.getCalculatedX();
    float x2 = accel2.getCalculatedX();

    float y1 = accel1.getCalculatedY();
    float y2 = accel2.getCalculatedY();

    float z1 = accel1.getCalculatedZ();
    float z2 = accel2.getCalculatedZ();

    Serial.print("x: ");
    Serial.print(x1, 3);
    Serial.print(" ");
    Serial.print(x2, 3);

    Serial.print("\t");
    Serial.print("\t");

    Serial.print("y: ");
    Serial.print(y1, 3);
    Serial.print(" ");
    Serial.print(y2, 3);

    Serial.print("\t");
    Serial.print("\t");

    Serial.print("z: ");
    Serial.print(z1, 3);
    Serial.print(" ");
    Serial.print(z2, 3);

    Serial.println();
  }

  Serial.print("Temperature in C = ");
  Serial.println(Temp1.readCelsius()); // Thermocouple 1 temperature in degrees Celsius
  Serial.print(" ");
  Serial.println(Temp2.readCelsius()); // Thermocouple 2 temperature in degrees Celsius
  delay(1000);
}

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

Thanks very much for the reply and the advice, appreciate it a lot. :slight_smile: