====== Außenthermometer - Programmversion 1.0 ====== Nachdem die [[iot:aussenthermometer:programmversion_0.2|Programmversion 0.2]] über ein halbes Jahr fehlerfrei lief, wurde für die Programmversion 1.0 nur noch die Ausgabe an die serielle Schnittstelle auskommentiert. // Außenthermometer // Läuft auf einem Lolin D32 // Misst Temperatur, Luftfeuchtigkeit und Luftdruck // Berechnet den Taupunkt und den Hitzeindex // Sendet die Daten iot.frickelpiet.de // Bibliotheken #include #include #include #include #include // Debug on/off // #define DEBUG // Sensor Objekt Adafruit_BME280 bme; // I2C // WLAN SSDI und Passwort const char* ssid = "FRITZ!Box 7490"; // Name des WLAN const char* password = "xxxxxxxxxxxxxxxxxxxx"; // Passwort des WLAN // Webserver iot.frickelpiet.de char server[] = "iot.frickelpiet.de"; const int port = 80; WiFiClient client; const int sensorbox = 1; // Eindeutige ID des Außenthermometers. Wird von adddata.php ausgewertet float temperature; // Temperatur float humidity; // Luftfeuchtigkeit float pressure; // Luftdruck float dewpoint; // Taupunkt float heatindex; // Hitzeindex float voltage; // Spannung der Batterie int rssi; // Signalqualität des WiFi int wifiCounter = 0; // Zählt die Verbindungsversuche zum WiFi String data; // String, der an den Webserver übertragen wird #define uS_TO_S_FACTOR 1000000 // Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 600 // Time ESP32 will go to sleep (in seconds) */ void setup () { // Serielle Schnittstelle #ifdef DEBUG Serial.begin(115200); delay(1000); //Take some time to open up the Serial Monitor #endif esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); #ifdef DEBUG Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds"); #endif // Initialisiert Wire Wire.begin(25, 26); // SDA, SCL // Verbindung zum WLAN Router aufbauen #ifdef DEBUG Serial.printf("Connecting to %s ", ssid); #endif WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { // Es wird 5 Sekunden eine Verbingung gesucht, danach wird abgebrochen if (wifiCounter <= 19) { wifiCounter ++; delay(500); #ifdef DEBUG Serial.print("."); #endif } else { #ifdef DEBUG Serial.println("Connection to wifi failed!"); Serial.println("Going to sleep now"); delay(100); #endif esp_deep_sleep_start(); // Wenn keine Verbindung zum WLAN hergestellt werden kann, geht der ESP32 in den Tiefschlaf } } #ifdef DEBUG Serial.println(" Connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); #endif // BME280 initialisieren bool status; status = bme.begin(); if (!status) { #ifdef DEBUG Serial.println("Could not find a valid BME280 sensor, check wiring!"); #endif while (1); } bme.setSampling(Adafruit_BME280::MODE_FORCED, Adafruit_BME280::SAMPLING_X1, // temperature Adafruit_BME280::SAMPLING_X1, // pressure Adafruit_BME280::SAMPLING_X1, // humidity Adafruit_BME280::FILTER_OFF ); // BME 280 abfragen temperature = bme.readTemperature(); humidity = bme.readHumidity(); pressure = bme.readPressure() / 100.0F; // Taupunkt berechnen float a = 17.271; float b = 237.7; float dewpointTmp = (a * temperature) / (b + temperature) + log(humidity/100); dewpoint = (b * dewpointTmp) / (a - dewpointTmp); #ifdef DEBUG Serial.print("Dewpoint: "); Serial.print(dewpoint); Serial.println(" °C"); #endif // Hitzeindex berechnen float c1 = -8.784695; float c2 = 1.61139411; float c3 = 2.338549; float c4 = -0.14611605; float c5 = -1.2308094e-2; float c6 = -1.6424828e-2; float c7 = 2.211732e-3; float c8 = 7.2546e-4; float c9 = -3.582e-6; heatindex = c1 + c2 * temperature + c3 * humidity + c4 * temperature * humidity + c5 * sq(temperature) + c6 * sq(humidity) + c7 * sq(temperature) * humidity + c8 * temperature * sq(humidity) + c9 * sq(temperature) * sq(humidity); #ifdef DEBUG Serial.print("Heatindex: "); Serial.print(heatindex); Serial.println(" °C"); #endif // Batteriespannung lesen voltage = analogRead(35) / 4096.0 * 7.445; #ifdef DEBUG Serial.print("Battery voltage: "); Serial.print(voltage); Serial.println(" volts"); #endif // Signalqualität WiFi rssi = (WiFi.RSSI() + 100) * 2; #ifdef DEBUG Serial.print("WiFi signal: "); Serial.print(rssi); Serial.println(" %"); #endif // String erzeugen data = "sensorbox=" + String(sensorbox) + "&temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&pressure=" + String(pressure) + "&dewpoint=" + String(dewpoint) + "&heatindex=" + String(heatindex) + "&rssi=" + String(rssi) + "&voltage=" + String(voltage); #ifdef DEBUG Serial.print("Data String: "); Serial.println(data); #endif // Daten an den Webserver iot.frickelpiet.de senden #ifdef DEBUG Serial.print("Connecting to: "); Serial.println(server); #endif if (client.connect(server, port)) { #ifdef DEBUG Serial.print("Connected to: "); Serial.println(server); #endif client.print("GET /adddata.php?"); client.print(data); client.println(" HTTP/1.1"); client.print("Host: "); client.println(server); client.println("Connection: close"); client.println(); client.stop(); } else { #ifdef DEBUG Serial.println("connection failed"); #endif } // Schickt den ESP32 in den Tiefschlaf #ifdef DEBUG Serial.println("Going to sleep now"); delay(100); #endif esp_deep_sleep_start(); } void loop() { }