*/ /************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: Temperatur und Lufdruckanzeige mit einem BME180 am WEB-Server ************************************************************************************************** Version: 30.06.2022 ************************************************************************************************** Board: ESP32vn IoT UNO ************************************************************************************************** Libraries: https://github.com/espressif/arduino-esp32/tree/master/libraries C:\Users\User\Documents\Arduino D:\gittemp\Arduino II\A156_Wetterdaten_V3 ************************************************************************************************** C++ Arduino IDE V1.8.19 ************************************************************************************************** Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json http://arduino.esp8266.com/stable/package_esp8266com_index.json **************************************************************************************************/ #include #include #include #include #include #include // Replace with your network credentials const char* ssid = "xxx"; const char* password = "xxx"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); // Create an Event Source on /events AsyncEventSource events("/events"); // Timer variables unsigned long lastTime = 0; unsigned long timerDelay = 30000; // Create a sensor object BMP180 bmp180; // BME280 connect to ESP32 I2C (GPIO 21 = SDA, GPIO 22 = SCL) float temperature; float humidity; float pressure; void getSensorReadings(){ temperature = (bmp180.getTemperature()); pressure = (bmp180.getPressure());// / 100.0F; } // Initialize WiFi void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } String processor(const String& var){ getSensorReadings(); //Serial.println(var); if(var == "TEMPERATURE"){ return String(temperature); } else if(var == "PRESSURE"){ return String(pressure); } return String(); } const char index_html[] PROGMEM = R"rawliteral( ESP Web Server

KHF WEB SERVER

Temperatur

%TEMPERATURE% °C

Luftdruck

%PRESSURE% hPa

)rawliteral"; void setup() { Serial.begin(115200); initWiFi(); bmp180.init(); // Handle Web Server server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Handle Web Server Events 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() { if ((millis() - lastTime) > timerDelay) { getSensorReadings(); Serial.printf("Temperature = %.2f ºC \n", temperature); Serial.printf("Pressure = %.2f hPa \n", pressure); Serial.println(); // Send Events to the Web Server with the Sensor Readings events.send("ping",NULL,millis()); events.send(String(temperature).c_str(),"temperature",millis()); events.send(String(pressure).c_str(),"pressure",millis()); lastTime = millis(); } }