I'm trying to transfer data between a Arduino Uno board to a NodeMCU ESP8266 board, using nRF24L01+ transceivers module and RF24 library on both side. The data I'm transferring are stored in a struct
defined this way:
// Struct declared in both code
struct sensorData {
bool doorOpened;
int light;
int temperature;
int humidity;
float voltage_battery;
};
// Variable in Uno code (source node)
sensorData dataToSend;
// Variable in ESP8266 code (dest node)
sensorData dataToReceive;
But the data are not transferred properly between the two boards and I get random values or 0 when trying to display them from ESP8266 (values are correctly displayed from Uno).
Display in Uno (using signed short
as data type, see following paragraphs):
Display in ESP8266 (using signed short
as data type, see following paragraphs):
After some testing, I found out that memory-allocation on both boards are different, as Uno takes 2 bytes to store int
while ESP8266 takes 4 bytes. Moreover, sensorData takes 11 bytes of memory on Uno (1 for bool
, 2 for int
and 4 for float
, which sums up to 11 bytes), but takes 20 bytes of memory on ESP8266 (1 for bool
, 4 for int
and 4 for float
, which does NOT sum up to 20 !).
Trying to solve this, I tried changing int
for signed int
, byte
, short
, signed short
but the transmission problem remains. When using short
or signed short
(which takes 2 bytes on both boards), my structure sensorData
still takes 11 bytes of memory on UNO while it takes 12 bytes of memory on ESP8266. For some reason, ESP8266 always takes more space than needed to store sensorData
, probably causing bits to be wrongly placed when receiving data.
I'm sure that both boards, radio module and transmission protocol work as I successfully tested the same code with a smaller struct (text
was "Hello World !" and cmp
was increased at each transmission):
struct Package {
char text[16];
long cmp;
};
Does anyone have faced this issue before ? How could I solve that ?