Hello,
I want to use EspNow to send my data. I have two Esp8266 (Wemos) and an Esp32. Both Esp8266 are transmitters. The first must send the soil humidity percentage and the second send the air temperature and humidity. The receiver must receive these three values on an Esp32. I use the program from two Esp8266 to an Esp32 which comes from
https://randomnerdtutorials.com/esp-now-many-to-one-esp32/.
On the other hand, of course, I modified the Esp8266 and the Esp32.
My question. Is it possible to have different structs or I have to put them identical and take the values that I need and I give fictitious values to the data that I do not use.
Ex. Board1= floor humidity, the other air temp and humidity at zero
Board2= air temperature and humidity and soil humidity = zero
typedef struct struct_message
{ Int id;
int soil_humidity;
int temp;
int air_humidity; }
struct_message;
Sending from transmitter no.1 one is well received by the Esp32.
Sending from transmitter no.2 is well received by the Esp32.
On the other hand, when the two transmitters send at the same time, there are errors.
If we cannot send different data from two Esp8266 to an Esp32, then how can we do it. Webpage?…MQTT?….???? I need help
Michel Tremblay
Hi.
The structure of sender and receiver must be the same because the receiver needs to know how much memory to allocate to receive and save the data. Our examples only deal with the same structure. I’m not sure if there is a way to use different structures.
Other alternative methods to send data:
- MQTT: https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/
- Wi-Fi: https://randomnerdtutorials.com/esp8266-nodemcu-client-server-wi-fi/
- https://randomnerdtutorials.com/esp32-client-server-wi-fi/
- Database with Firebase: https://randomnerdtutorials.com/esp32-firebase-realtime-database/
- ESP-MESH (for ESP32 boards): https://randomnerdtutorials.com/esp-mesh-esp32-esp8266-painlessmesh/
I hope this helps.
Regards,
Sara
Hello,
Ok, first is it possible to have two structures. Example if my shipment comes from board1 ( mac address no 1 ) then structure 1 and otherwise structure 2.
Hi.
I’m not sure. But if you check the mac address of the board and then use the corresponding structure maybe it will work. See the onDataRecv example function below:
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
memcpy(&myData, incomingData, sizeof(myData));
Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
// Update the structures with the new incoming data
boardsStruct[myData.id-1].x = myData.x;
boardsStruct[myData.id-1].y = myData.y;
Serial.printf("x value: %d \n", boardsStruct[myData.id-1].x);
Serial.printf("y value: %d \n", boardsStruct[myData.id-1].y);
Serial.println();
}
If after getting the MAC address, you check to which board it corresponds, then add an if statement to check which structure to use (in this case, the structure is myData, but you would use the one that corresponds to the MAC address:
memcpy(&myData, incomingData, sizeof(myData));
I’m not sure if this will work because I didn’t try it. But, you can try to implement it and then tell us how it went.
Regards,
Sara