-
Notifications
You must be signed in to change notification settings - Fork 1
/
homecontrol-ESP-DHT.ino
185 lines (147 loc) · 4.49 KB
/
homecontrol-ESP-DHT.ino
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include "DHT.h"
#include <ArduinoJson.h>
// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
void LEDflash(int sleepDelay = 300);
// Replace with your network details
const char* ssid = "SSID";
const char* password = "passwordHere";
// Web Server on port 80
WiFiServer server(80);
const int LEDPin = 2;
const int DHTPin = 12;
DHT dht(DHTPin, DHTTYPE);
const char* apiVersion = "v1";
IPAddress localIP;
IPAddress remoteIP;
// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];
void setup() {
pinMode(LEDPin, OUTPUT);
LEDoff();
Serial.begin(115200);
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
delay(1000);
wifiManager.setAPCallback(configModeCallback);
if (!wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
ESP.reset();
delay(1000);
}
Serial.println("connected to ");
server.begin();
Serial.println("Web server running");
LEDon();
dht.begin();
}
void loop() {
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
LEDoff();
delay(500);
}
WiFiClient client = server.available();
if (client) {
Serial.println("New client from ");
//Serial.println(DisplayAddress(client.remoteIP));
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && blank_line) {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
strcpy(celsiusTemp, "Failed");
strcpy(humidityTemp, "Failed");
client.println("HTTP/1.1 503 Service Unavailable");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
const size_t bufferSize = JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5);
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.createObject();
root["version"] = apiVersion;
root["status"] = 503;
root["message"] = "Failed to read from DHT sensor!";
JsonObject& data = root.createNestedObject("data");
root["millisSinceBoot"] = millis();
root.printTo(Serial);
root.printTo(client);
client.stop();
break;
}
else {
// Computes temperature values in Celsius + Fahrenheit and Humidity
float hic = dht.computeHeatIndex(t, h, false);
dtostrf(hic, 6, 2, celsiusTemp);
client.println("HTTP/1.0 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
const size_t bufferSize = JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5);
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.createObject();
root["version"] = apiVersion;
root["status"] = 200;
root["message"] = "";
JsonObject& data = root.createNestedObject("data");
data["temperature"] = t;
data["temperatureUnit"] = "C";
data["humidity"] = h;
data["heatIndex"] = hic;
data["heatIndexUnit"] = "C";
root["millisSinceBoot"] = millis();
root.printTo(Serial);
//root.prettyPrintTo(client);
root.printTo(client);
client.stop();
break;
}
}
if (c == '\n') {
// when starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// when finds a character on the current line
blank_line = false;
}
}
}
// closing the client connection
delay(1);
client.stop();
Serial.println("\nClient disconnected.");
LEDflash(200);
}
}
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void LEDflash(int sleepDelay) {
digitalWrite(LEDPin, !digitalRead(LEDPin));
delay(sleepDelay/2);
digitalWrite(LEDPin, !digitalRead(LEDPin));
delay(sleepDelay/2);
}
void LEDon() {
digitalWrite(LEDPin, LOW);
}
void LEDoff() {
digitalWrite(LEDPin, HIGH);
}