From ea4aa7b2f6cd74161bb516378cea0724c7197b7c Mon Sep 17 00:00:00 2001 From: steckbrief Date: Tue, 8 Aug 2017 23:55:48 +0200 Subject: Example SensorServer added - reads humidity, temperature, vcc - can change builtin led state --- devices/SensorServer/SensorServer.ino | 127 ++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 devices/SensorServer/SensorServer.ino diff --git a/devices/SensorServer/SensorServer.ino b/devices/SensorServer/SensorServer.ino new file mode 100644 index 0000000..e9f702d --- /dev/null +++ b/devices/SensorServer/SensorServer.ino @@ -0,0 +1,127 @@ +#include +#include +#include +// Including the DHT library +#include "DHT.h" + +const char *ssid = "TP8"; +const char *password = "tzspot123"; + +ESP8266WebServer server(80); + +float h; +float t; + +#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 + +// DHT Sensor +const int DHTPin = D4; +// Initialize DHT sensor. +DHT dht(DHTPin, DHTTYPE); + +boolean ledState = false; + +void handleRoot() { + Serial.println("Handle root"); + h = dht.readHumidity(); + Serial.println("humidity read"); + Serial.println(h); + // Read temperature as Celsius (the default) + t = dht.readTemperature(); + Serial.println("temperature read"); + Serial.println(t); + + if (isnan(h) || isnan(t)) { + server.send(500, "text/html", "

Failed to read from DHT sensor!

"); + } else { + int sec = millis() / 1000; + int min = sec / 60; + int hr = min / 60; + + char uptime[9]; + snprintf(uptime, 9, "%02d:%02d:%02d", hr, min % 60, sec % 60); + String temp ="\ + \ + \ + \ + ESP8266 Demo\ + \ + \ + \ +

Node MCU Sensor Data

\ +

Uptime: " + String(uptime) + "

\ + \ + \ + \ + \ + \ + \ + \ +
fieldvalue
Chip ID" + String(ESP.getChipId()) + "
VCC" + String(ESP.getVcc()) + "V
Humidity" + String(h) + "%
Temperature" + String(t) + "°C
Built in LED state" + ((ledState) ? "on" : "off") + "
\ + \ +"; + + server.send(200, "text/html", temp); + + } +} + +void handleOn() { + digitalWrite(BUILTIN_LED, LOW); + ledState = true; + server.sendHeader("Location", "/", true); + server.send ( 302, "text/plain", ""); +} + +void handleOff() { + digitalWrite(BUILTIN_LED, HIGH); + ledState = false; + server.sendHeader("Location", "/", true); + server.send ( 302, "text/plain", ""); +} + +/**********************************************/ +/* WiFi connecting script +/**********************************************/ +void connectWifi() { + WiFi.begin(ssid, password); // Start WiFI + + Serial.print("Connecting "); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println("WiFi connected"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + dht.begin(); +} + +void initServer() { + server.on("/", handleRoot); + server.on("/on", handleOn); + server.on("/off", handleOff); + server.begin(); + Serial.println("HTTP server initialized"); +} + +void setup() { + Serial.begin(9600); //Output to Serial at 9600 baud + connectWifi(); // Start ConnecWifi + Serial.print("\n"); + Serial.println("ChipId: "); + Serial.println(ESP.getChipId()); + initServer(); + pinMode(BUILTIN_LED, OUTPUT); + digitalWrite(BUILTIN_LED, HIGH); +} + +void loop() { + server.handleClient(); +} -- cgit v1.0-28-g1787