LoRa connected Arduino UNO and ESP8266 received packets does not match

I am trying to use Arduino uno as microcontroller in the sender side and ESP8266 on the receiver side.

I am sending struct data and here's the code I am using for this project.

//Sender Arduino Uno
#include <SPI.h>
#include <LoRa.h>

struct Data {
  byte a;
  float b;
  float c;
  float d;
  float e;
  float f;
  float g;
  float h;
  float i;
};

Data data = {0xBB, 0, 0, 0, 0, 0, 0, 0, 0};

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial.println("LoRa Sender");
  LoRa.setPins(10, 9, 2); // 10, 9 for UNO, 53, 9 for Mega, 15, 16 for ESP
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if (runEvery(4000)) { // repeat every 1000 millis

    sendMessage(); // send a message

  }

}

void sendMessage() {
  LoRa.beginPacket();                   // start packet
  for (unsigned int i = 0; i < sizeof(Data);i++) {
    // Serial.print(' ');
    LoRa.write(((byte *) &data)[i]);
    // Serial.print(((byte *) &data)[i]);
  }
  LoRa.endPacket(true);                 // finish packet and send it

  data.b += 1;
  data.c += 2;
  data.d += 3;
  data.e += 4;
  data.f += 5;
  data.g += 6;
  data.h += 7;
  data.i += 8;

  Serial.print("Node"); 
  Serial.print(data.a, HEX); Serial.print(",");
  Serial.print(data.b); Serial.print(",");
  Serial.print(data.c); Serial.print(",");
  Serial.print(data.d); Serial.print(",");
  Serial.print(data.e); Serial.print(",");
  Serial.print(data.f); Serial.print(",");
  Serial.print(data.g); Serial.print(",");
  Serial.print(data.h); Serial.print(",");
  Serial.print(data.i); Serial.print(",");
  Serial.println(LoRa.packetRssi());
}


boolean runEvery(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;
}

//Receiver ESP8266
#include <SPI.h>
#include <LoRa.h>

struct Data {
  byte a;
  float b;
  float c;
  float d;
  float e;
  float f;
  float g;
  float h;
  float i;
} data;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial.println("LoRa Receiver"); 
  LoRa.setPins(15, 16, 2); // 10 for UNO, 53 for Mega, 15, 16 for ESP
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

    // read packet
    while (LoRa.available()) {
      for (int i = 0; i < packetSize; i++) {
        ((byte *) &data)[i] = LoRa.read();
        // Serial.print(' ');
        // Serial.print(((byte *) &data)[i]);
      }
    }
    // Serial.flush();
    Serial.print("Node"); 
    Serial.print(data.a, HEX); Serial.print(",");
    Serial.print(data.b); Serial.print(",");
    Serial.print(data.c); Serial.print(",");
    Serial.print(data.d); Serial.print(",");
    Serial.print(data.e); Serial.print(",");
    Serial.print(data.f); Serial.print(",");
    Serial.print(data.g); Serial.print(",");
    Serial.print(data.h); Serial.print(",");
    Serial.print(data.i); Serial.print(",");
    Serial.println(LoRa.packetRssi());
  }
}

I printed what the sender side sents and here's the output

NodeBB,1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00,-80
NodeBB,2.00,4.00,6.00,8.00,10.00,12.00,14.00,16.00,-80
NodeBB,3.00,6.00,9.00,12.00,15.00,18.00,21.00,24.00,-80
NodeBB,4.00,8.00,12.00,16.00,20.00,24.00,28.00,32.00,-80
NodeBB,5.00,10.00,15.00,20.00,25.00,30.00,35.00,40.00,-80
NodeBB,6.00,12.00,18.00,24.00,30.00,36.00,42.00,48.00,-80
NodeBB,7.00,14.00,21.00,28.00,35.00,42.00,49.00,56.00,-80
NodeBB,8.00,16.00,24.00,32.00,40.00,48.00,56.00,64.00,-80
NodeBB,9.00,18.00,27.00,36.00,45.00,54.00,63.00,72.00,-80
NodeBB,10.00,20.00,30.00,40.00,50.00,60.00,70.00,80.00,-80
NodeBB,11.00,22.00,33.00,44.00,55.00,66.00,77.00,88.00,-80
NodeBB,12.00,24.00,36.00,48.00,60.00,72.00,84.00,96.00,-80

But I always received something like this.

Received packet 'NodeBB,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,-90
Received packet 'NodeBB,0.00,2.00,-0.00,-0.00,-2.00,-36893769622395820029.22,0.00,0.00,-90
Received packet 'NodeBB,-0.00,-2.00,0.00,0.00,2.00,36893774020442333672.06,-0.00,0.00,-90
Received packet 'NodeBB,-2.00,0.00,2.00,158457552843814264420529980270.65,-0.00,-0.00,-2.00,0.00,-90
Received packet 'NodeBB,0.00,2.00,-0.00,-0.00,-2.00,-36893774020442333672.06,0.00,0.00,-90
Received packet 'NodeBB,0.00,158457552843814264420529980270.65,-0.00,-131073.02,-158457552843814264420529980270.65,0.00,0.00,0.00,-90
Received packet 'NodeBB,2.00,-0.00,-2.00,-158457552843814264420529980270.65,0.00,0.00,2.00,0.00,-90
Received packet 'NodeBB,36893769622395820029.22,-0.00,-36893774020442333672.06,0.00,0.00,512.00,36893778418488842874.01,0.00,-90
Received packet 'NodeBB,-0.00,-2.00,0.00,0.00,2.00,36893778418488842874.01,-0.00,0.00,-90
Received packet 'NodeBB,-0.00,-562954315497472.10,0.00,0.00,562954382606336.16,2658476907804736555362978833727538585.66,-0.00,0.00,-90
Received packet 'NodeBB,-0.00,-158457552843814264420529980270.65,0.00,131073.03,158457571733280211390137992566.45,-0.00,-0.00,0.00,-90
Received packet 'NodeBB,-0.00,0.00,0.00,144116321947222036.92,-0.00,-0.00,-0.00,0.00,-90

its does not match with the expected sent data.

When I swap the microcontroller on each side, Arduino output is just 0

The code works fine as long as the microcontroller on each nodes is in the same (ex. sender and receiver microcontrollers are both Arduino (can be nano, uno or mega) or both ESP8266)

I really need ESP8266 on the receiver side because I needed the receiver side connected to the internet while on the sender side, I needed analog pins so I need to use arduino instead of esp

edit:
I tried to print the values in bytes and it is match with the sender. What must be the problem in converting values?

if I run the following code on a UNO

struct Data {
  byte a;
  float b;
  float c;
  float d;
  float e;
  float f;
  float g;
  float h;
  float i;
}data;

void setup() {
 Serial.begin(115200);
 Serial.println(sizeof(data));
}

void loop() {}

the Serial monitor displays 33 on a ESP8266 it displays 36
different processors can use different sizes for variables and different packing of variables in structures, e.g. aligned to a byte boundry or 16bit word boundry or a 32 bit boundry etc
try moving the byte to the end of the structure

struct Data {
  float b;
  float c;
  float d;
  float e;
  float f;
  float g;
  float h;
  float i;
    byte a;
}data;

1 Like

this works thank you so much!

Anotther question thou, is it normal that I received 1line delayed?

this is the first time I encounter something like this

I was incrementing the sequennce number immediatly after transmitting the packet
it should be just before the packet is transmitted, e.g.

void sendMessage() {
  data.seq++;               // increment packet sequence number
  LoRa_txMode();                            // set tx mode
  LoRa.beginPacket();                       // start packet
  LoRa.write((byte *)&data, sizeof(data));  // transmit packet
  // display packet contents
  //for (unsigned int i = 0; i < sizeof(Data);i++) {
  // Serial.print(' ');
  //LoRa.write(((byte *) &data)[i]);
  // Serial.print(((byte *) &data)[i]);
  //}
  LoRa.endPacket(true);  // finish packet and send it
  data.x += 1;
  data.y += 1;
  data.z[0] += 1;
}

avoid uploading screen images etc they tend to be larger than equivalent text

That worked in the case of this simple struct. However it could fail with a more complex structure that has a larger mix of data types. A more general solution is to use the packed attribute:

  struct __attribute__((packed)) Data {
    byte a;
    float b;
    float c;
    float d;
    float e;
    float f;
    float g;
    float h;
    float i;
  } 

can still have problems with structure such as

  struct __attribute__((packed)) Data {
    byte a;
    int k;
    float b;
    float c;

  } data;
void setup() {
 Serial.begin(115200);
 Serial.println();
 Serial.println(sizeof(data));
 Serial.println(sizeof(int));
}

void loop() {}

on a UNO displays (int is two bytes)

11
2

on an ESP8266 (int is four bytes)

13
4

can avoid the problem by using int16_t

 struct __attribute__((packed)) Data {
    byte a;
    int16_t k;
    float b;
    float c;

  } data;

data is 11 bytes on both the UNO and ESP8266

clearly when transmitting data between computers of different architectures care has to be taken

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