ESP Now - Wrong data in structure

Dominik95
Posts: 2
Joined: Wed Mar 20, 2024 7:51 am

ESP Now - Wrong data in structure

Postby Dominik95 » Wed Mar 20, 2024 8:08 am

Hi there,

I'm about to build my own whether station and so on and multiple controllers should communicate with each other. So I'm now starting to use the ESP-NOW protocol.
To my problem the sender (Wemos D1 Mini) sends the data which is received in a "normal" ESP8266.
The data is packed into a structure, which is declared in both CPUs

Code: Select all

typedef struct dht_message{
  uint8_t id;
  float temp;
  float humi;
} dht_message;
Here some code snippets from sender and receiver and the corresponding Serial Monitor output
The sender (D1 Mini)

Code: Select all

	dht_message dhtData;
	dhtData.id = 1;
	dhtData.temp = DhtValues::temp;
	dhtData.humi = DhtValues::humi;
	
	uint8_t debugData[sizeof(dht_message)+1];
	memcpy(&debugData, &dhtData, sizeof(dht_message));
	for (int i=0; i< sizeof(dht_message); i++) {Serial.print(debugData[i]);} Serial.println();
		
	Serial.println(String(dhtData.id) + " ; " + String(dhtData.temp) + " ; " + String(dhtData.humi) );
	
	esp_now_send(loggerAddress, (uint8_t *) &dhtData, sizeof(dht_message));	
Following code is in the Monitor:
08:43:01.068 -> 10001541531696551518366
08:43:01.068 -> 1 ; 21.20 ; 52.80
On the receiver side (ESP8266)

Code: Select all

void espnowReceived(uint8_t * mac, uint8_t *incomingData, uint8_t len){
  dht_message recData;
  Serial.println("Message length:" + String(len));
  for (int i=0; i<len; i++){ Serial.print(incomingData[i]);}
  Serial.println();
  memcpy(&recData, incomingData, sizeof(incomingData));
  if (recData.id <2){
    SensorValues::temp[recData.id]= recData.temp;
    SensorValues::humi[recData.id]= recData.humi;
    Serial.println ("ID: " + String(recData.id) + " | Temp: "+ String(recData.temp) + " | Humi: " + String(recData.humi) );
08:43:01.075 -> Message length:12
08:43:01.075 -> 10001541531696551518366
08:43:01.149 -> ID: 1 | Temp: 2.00 | Humi: 2.00
As far as I can see the communication is running as it should also the raw data is transfered successful.
Nevertheless the same data is interpreted on receiver side in a different way.
Is there any different memory setting between Wemos D1 and "normal" ESP8266 or can something be configured in Arduino IDE?
I'm running out of ideas and assume there is any byteswap, memory gap or similar.

ESP_Sprite
Posts: 9772
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP Now - Wrong data in structure

Postby ESP_Sprite » Thu Mar 21, 2024 3:30 am

Maybe a data packing issue? You can try aligning it manually:

Code: Select all

typedef struct dht_message{
  uint8_t id;
  uint8_t dummy[3];
  float temp;
  float humi;
} dht_message;

boarchuz
Posts: 611
Joined: Tue Aug 21, 2018 5:28 am

Re: ESP Now - Wrong data in structure

Postby boarchuz » Thu Mar 21, 2024 4:04 am

memcpy(&recData, incomingData, sizeof(incomingData));
You're taking the sizeof a pointer, so only the first 4 received bytes are being copied into your recData struct.

Dominik95
Posts: 2
Joined: Wed Mar 20, 2024 7:51 am

Re: ESP Now - Wrong data in structure

Postby Dominik95 » Thu Mar 21, 2024 6:34 am

Ouh, sometimes it is so simple. I checked everything but never thought about using the size of a pointer instead of size of the structure. :o Thanks for the hint now everything is running as it should and I can make it nice looking in the coming days. This Topic can be closed.

Who is online

Users browsing this forum: No registered users and 15 guests