diff options
| author | steckbrief <steckbrief@chefmail.de> | 2017-08-08 23:55:48 +0200 |
|---|---|---|
| committer | steckbrief <steckbrief@chefmail.de> | 2017-08-08 23:55:48 +0200 |
| commit | ea4aa7b2f6cd74161bb516378cea0724c7197b7c (patch) | |
| tree | b8556a5dda62d9c01b8de19038c5050492c04920 | |
| parent | 3df152f8ec9ced7a81bb3039d713176b9923a659 (diff) | |
Example SensorServer added - reads humidity, temperature, vcc - can change builtin led stateHEADmaster
| -rw-r--r-- | devices/SensorServer/SensorServer.ino | 127 |
1 files changed, 127 insertions, 0 deletions
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 <ESP8266WiFi.h> +#include <WiFiClient.h> +#include <ESP8266WebServer.h> +// 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", "<h1>Failed to read from DHT sensor!</h1>"); + } 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 ="<html>\ + <head>\ + <meta http-equiv='refresh' content='5'/>\ + <meta name='viewport' content='width=device-width, initial-scale=1.0'>\ + <title>ESP8266 Demo</title>\ + <style>\ + body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; color: #000088; }\ + table, th, td { border: 1px solid black; }\ + th { font-weight: bold; }\ + table { width: 300px; }\ + </style>\ + </head>\ + <body>\ + <h1>Node MCU Sensor Data</h1>\ + <p>Uptime: " + String(uptime) + "</p>\ + <table>\ + <tr><th>field</th><th>value</th></tr>\ + <tr><td>Chip ID</td><td>" + String(ESP.getChipId()) + "</td></tr>\ + <tr><td>VCC</td><td>" + String(ESP.getVcc()) + "V</td></tr>\ + <tr><td>Humidity</td><td>" + String(h) + "%</td></tr>\ + <tr><td>Temperature</td><td>" + String(t) + "°C</td></tr>\ + <tr><td>Built in LED state</td><td><a href='" + ((!ledState) ? "on" : "off") + "'>" + ((ledState) ? "on" : "off") + "</a></td></tr>\ + </table>\ + </body>\ +</html>"; + + 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(); +} |
