summaryrefslogtreecommitdiff
path: root/devices/SensorServer/SensorServer.ino
blob: e9f702d5f38c4f5a6b66604d4617909e3a470a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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();
}