#include #include #include "ESPAsyncWebServer.h" #include "ESPAsyncTCP.h" #include // Replace with your network credentials (STATION) const char* ssid = "HD24-neu"; //FRITZ!Box 6591 Cable MB const char* password = "Dossenheim"; //03234937612844141212 // Structure example to receive data // Must match the sender structure typedef struct struct_message { int id; float temp; float hum; float temp1; float temp2; float temp3; float temp4; float volt; unsigned int readingId; } struct_message; struct_message incomingReadings; JSONVar board; AsyncWebServer server(80); AsyncEventSource events("/events"); // callback function that will be executed when data is received void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) { // Copies the sender mac address to a string 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(&incomingReadings, incomingData, sizeof(incomingReadings)); board["id"] = incomingReadings.id; board["temperature"] = incomingReadings.temp; board["humidity"] = incomingReadings.hum; board["temp1"] = incomingReadings.temp1; board["temp2"] = incomingReadings.temp2; board["temp3"] = incomingReadings.temp3; board["temp4"] = incomingReadings.temp4; board["voltage"] = incomingReadings.volt; board["readingId"] = String(incomingReadings.readingId); String jsonString = JSON.stringify(board); events.send(jsonString.c_str(), "new_readings", millis()); Serial.printf("Board ID %u: %u bytes\n", incomingReadings.id, len); Serial.printf("t value: %4.2f \n", incomingReadings.temp); Serial.printf("h value: %4.2f \n", incomingReadings.hum); Serial.printf("temp1 value: %4.2f \n", incomingReadings.temp1); Serial.printf("temp2 value: %4.2f \n", incomingReadings.temp2); Serial.printf("temp3 value: %4.2f \n", incomingReadings.temp3); Serial.printf("temp4 value: %4.2f \n", incomingReadings.temp4); Serial.printf("v value: %4.2f \n", incomingReadings.volt); Serial.printf("readingID value: %d \n", incomingReadings.readingId); Serial.println(); } const char index_html[] PROGMEM = R"rawliteral( ESP-NOW DASHBOARD

Meine Wetterstation

Keller - Temperatur

°C

letzte Lesung:

Keller - Feuchtigkeit

%

letzte Lesung:

Sensor 1 - Temperatur

°C

letzte Lesung:

Sensor 2 - Temperatur

°C

letzte Lesung:

Sensor 3 - Temperatur

°C

letzte Lesung:

Sensor 4 - Temperatur

°C

letzte Lesung:

Keller - Akku

V

letzte Lesung:

)rawliteral"; void setup() { // Initialize Serial Monitor Serial.begin(115200); // Set the device as a Station and Soft Access Point simultaneously WiFi.mode(WIFI_AP_STA); // Set device as a Wi-Fi Station WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Setting as a Wi-Fi Station.."); } Serial.print("Station IP Address: "); Serial.println(WiFi.localIP()); Serial.print("Wi-Fi Channel: "); Serial.println(WiFi.channel()); // Init ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } // Once ESPNow is successfully Init, we will register for recv CB to // get recv packer info esp_now_register_recv_cb(OnDataRecv); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html); }); events.onConnect([](AsyncEventSourceClient *client){ if(client->lastId()){ Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId()); } // send event with message "hello!", id current millis // and set reconnect delay to 1 second client->send("hello!", NULL, millis(), 10000); }); server.addHandler(&events); server.begin(); } void loop() { static unsigned long lastEventTime = millis(); static const unsigned long EVENT_INTERVAL_MS = 5000; if ((millis() - lastEventTime) > EVENT_INTERVAL_MS) { events.send("ping",NULL,millis()); lastEventTime = millis(); } }