#include <esp_now.h>
#include <WiFi.h>
// Structure to keep the temperature and humidity data
// Is also required in the client to be able to save the data directly
typedef struct temp_humidity {
float temperature;
float humidity;
};
// Create a struct_message called myData
temp_humidity dhtData;
// callback function executed when data is received
void OnRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&dhtData, incomingData, sizeof(dhtData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Temperature: ");
Serial.println(dhtData.temperature);
Serial.print("Humidity: ");
Serial.println(dhtData.humidity);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("There was an error initializing ESP-NOW");
return;
}
// Once the ESP-Now protocol is initialized, we will register the callback function
// to be able to react when a package arrives in near to real time without pooling every loop.
esp_now_register_recv_cb(OnRecv);
}
void loop() {
}