Meilenstein 2 – Sensordaten im Intervall an eine HTTP Adresse senden
US 2.1 – Aufrufen einer HTTP Adresse vom Arduino
Aufgabe: Es soll in einem Intervall eine Adresse aufgerufen werden können.
Lösung:
Auch hier kann ich auf ein bereits bestehendes Tutorial verweisen WEMOS D1 – WLAN Thermometer – Teil2 Datenbank.
Quellcode
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include "DHT.h" #define DHT11PIN 0 #define DHT11TYPE DHT11 DHT dht11(DHT11PIN, DHT11TYPE); const char* ssid = "xyz"; const char* password = "xyz"; struct Entry { unsigned int temperatur; unsigned int luftfeuchtigkeit; String timestamp; }; const int PAUSE = 5000; unsigned long lastAction = -1L; ESP8266WebServer server(80); const int led = 13; String startTime = "1521139420740"; Entry getCurrentDht11EntryData(){ unsigned int humidy = dht11.readHumidity(); //relative Luftfeuchtigkeit vom Sensor DHT11 lesen unsigned int temp = dht11.readTemperature(); //Temperatur vom Sensor DHT11 lesen String currentTime = startTime; //Hier sollte auf eine RTC zurückgegriffen werden. return {temp, humidy, currentTime}; } void handleNotFound(){ digitalWrite(led, 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(led, 0); } void setup(){ Serial.begin(115200); delay(25); dht11.begin(); pinMode(led, OUTPUT); digitalWrite(led, 0); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } //server.on("/", handleRoot); server.on("/inline", [](){ server.send(200, "text/plain", "this works as well"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void storeData(){ Entry entry = getCurrentDht11EntryData(); callServer("Temperatur", entry.temperatur, entry.timestamp); callServer("Luftfeuchtigkeit", entry.luftfeuchtigkeit, entry.timestamp); } void callServer(String desc, unsigned int value, String timestamp){ WiFiClient client; const char* host = "sensor.draeger-it.blog"; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } String url = "/sensor.php?"; url = url + "sensor_desc0="+desc; url = url + "&sensor_value0="+value; url = url + "&sensor_timestamp="+timestamp; client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); } void loop(void){ server.handleClient(); unsigned long currentTimeMillis = millis(); if((lastAction+PAUSE)<currentTimeMillis){ lastAction = currentTimeMillis; storeData(); } }