-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.ino
executable file
·484 lines (417 loc) · 11.1 KB
/
thermostat.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <Arduino.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <math.h>
#include <RTClib.h>
#include <stdlib.h>
#include <U8g2lib.h>
#include <WiFiClient.h>
#define DHTTYPE DHT22
#define OLED_RESET -1
#ifndef STASSID
#define STASSID "xxxxxxxxxxxxx"
#define STAPSK "xxxxxxxxxxxxx"
#endif
const int POT_PIN = A0;
const int DHTPin = 14;
const int GREEN_PIN = 12;
const int RED_PIN = 13;
const int YELLOW_PIN = 15;
const int MENU_PIN = 0;
const int SELECT_PIN = 2;
const int CONTROL_PIN = 16;
const char* ssid = STASSID;
const char* password = STAPSK;
boolean isTimeForAChange;
boolean hotOn;
boolean fanOn;
boolean fanForcedOn;
boolean fanForcedOff;
boolean hotForcedOn;
boolean hotForcedOff;
boolean coldForcedOn;
boolean coldForcedOff;
double temp = 0.00;
double oldTemp = 0.00;
double valueRead = 0.00;
double t;
double h;
int yyyy, MM, dd, hh, mm, ss;
DateTime rightNow;
DateTime lastChangeTime;
String date;
ESP8266WebServer server(80);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2B(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
DHT dht(DHTPin, DHTTYPE);
RTC_DS3231 rtc;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(POT_PIN, INPUT);
pinMode(MENU_PIN, INPUT);
digitalWrite(MENU_PIN, HIGH);
pinMode(SELECT_PIN, INPUT);
digitalWrite(SELECT_PIN, HIGH);
pinMode(CONTROL_PIN, INPUT);
digitalWrite(CONTROL_PIN, HIGH);
Serial.begin(9600);
startScreenA();
startScreenB();
startDht();
startRtc();
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.onNotFound(handleNotFound);
server.on("/temp", HTTP_GET, getTemp);
server.on("/hot", HTTP_GET, forceHot);
server.on("/cold", HTTP_GET, forceCold);
server.on("/fan", HTTP_GET, forceFan);
server.on("/auto", HTTP_GET, setAuto);
server.begin();
Serial.println("HTTP server started");
lastChangeTime = rtc.now();
}
void loop() {
boolean controlOff = digitalRead(CONTROL_PIN);
Serial.println(digitalRead(CONTROL_PIN));
Serial.println(digitalRead(MENU_PIN));
Serial.println(digitalRead(SELECT_PIN));
if (controlOff) {
u8g2B.clear();
u8g2.clear();
u8g2B.clearBuffer();
u8g2.clearBuffer();
fanForcedOn = false;
fanForcedOff = false;
hotForcedOn = false;
hotForcedOff = false;
coldForcedOn = false;
coldForcedOff = false;
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
} else {
isTimeForAChange = rtc.now().unixtime() - lastChangeTime.unixtime() >= 300;
setTemp();
delay(500);
h = dht.readHumidity();
t = dht.readTemperature();
u8g2B.clearBuffer();
u8g2B.setCursor(5, 1);
u8g2B.print(temp);
u8g2B.sendBuffer();
printTemp(temp);
printT(t);
printH(h);
u8g2.setCursor(0, 40);
if (coldForcedOn || (!hotForcedOn && !coldForcedOff && t - temp > 0)) {
setColdOn();
} else if (hotForcedOn || (!coldForcedOn && !hotForcedOff && t - temp < 0)) {
setHotOn();
}
if (fanForcedOn || ((!fanForcedOn && !fanForcedOff) && (t - temp >= 3 || t - temp <= -3 || ((t - temp >= 3 || t - temp <= -1) && h > 65)))) {
setFanOn();
} else if (fanForcedOff || (!fanForcedOn && !fanForcedOff)) {
setFanOff();
}
printDate();
u8g2.sendBuffer();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
server.handleClient();
MDNS.update();
}
}
void getTemp()
{
oldTemp = temp;
temp = server.arg(String("temp")).toDouble();
printTemp(temp);
server.send(200, "text/plain", String("Temperatura cambiada a " + String(temp)));
}
void forceHot()
{
hotForcedOn = server.arg(String("hot")).equals("true");
if (hotForcedOn) {
setHotOn();
} else {
hotForcedOff = !hotForcedOn;
setHotOff();
}
server.send(200, "text/plain", String("Calor establecido a " + String(hotForcedOn)));
}
void forceCold()
{
coldForcedOn = server.arg(String("cold")).equals("true");
if (coldForcedOn) {
setColdOn();
} else {
coldForcedOff = !coldForcedOn;
setColdOff();
}
server.send(200, "text/plain", String("Frío establecido a " + String(coldForcedOn)));
}
void forceFan()
{
fanForcedOn = server.arg(String("fan")).equals("true");
if (fanForcedOn) {
setFanOn();
} else {
fanForcedOff = !fanForcedOn;
setFanOff();
}
server.send(200, "text/plain", String("Ventilador establecido a " + String(fanForcedOn)));
}
void setAuto()
{
boolean autoMode = server.arg(String("auto")).equals("true");
if (autoMode) {
fanForcedOn = false;
fanForcedOff = false;
hotForcedOn = false;
hotForcedOff = false;
coldForcedOn = false;
coldForcedOff = false;
server.send(200, "text/plain", String("Establecido modo auto"));
}
}
void startScreenA(void) {
u8g2.setI2CAddress(0x7A);
u8g2.begin();
u8g2_prepare();
}
void startScreenB(void) {
u8g2B.setI2CAddress(0x78);
u8g2B.begin();
u8g2B_prepare();
}
void startDht(void) {
dht.begin();
}
void startRtc(void) {
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void u8g2_prepare(void) {
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
}
void u8g2B_prepare(void) {
u8g2B.setFont(u8g2_font_logisoso42_tf);
u8g2B.setFontRefHeightExtendedText();
u8g2B.setDrawColor(1);
u8g2B.setFontPosTop();
u8g2B.setFontDirection(0);
}
void setTemp(void) {
valueRead = (double)analogRead(POT_PIN);
valueRead = valueRead <= 40 ? 0 : valueRead;
double newTemp = round((18.0 + valueRead / 128.00) * 10) / 10;
boolean smallVariation = oldTemp - newTemp <= 0.4 && oldTemp - newTemp >= -0.4;
temp = newTemp == 0.00 ? temp : smallVariation ? temp : newTemp;
}
void printTemp(double temp) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("Aire fijado a ");
u8g2.print(temp);
u8g2.print("C");
Serial.print("FixedTemp: ");
Serial.print(temp);
}
void printT(double t) {
u8g2.setCursor(0, 20);
u8g2.print("Temperatura: ");
u8g2.print(t);
u8g2.print("C");
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
}
void printH(double h) {
u8g2.setCursor(0, 30);
u8g2.print("Humedad: ");
u8g2.print(h);
u8g2.print("%");
Serial.print("\tHumidity: ");
Serial.print(h);
Serial.print(" *C \r\n");
}
void setColdOn(void) {
if (isTimeForAChange) {
lastChangeTime = rtc.now();
digitalWrite(RED_PIN, HIGH);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
u8g2.print("COLD ON");
hotOn = false;
} else if (hotOn) {
u8g2.print("COLD STARTING");
} else {
u8g2.print("COLD ON");
}
}
void setColdOff(void) {
if (isTimeForAChange) {
lastChangeTime = rtc.now();
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
u8g2.print("COLD OFF");
hotOn = false;
} else if (hotOn) {
u8g2.print("COLD STOPPING");
} else {
u8g2.print("COLD OFF");
}
}
void setHotOn(void) {
if (isTimeForAChange) {
lastChangeTime = rtc.now();
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
u8g2.print("HOT ON");
hotOn = true;
} else if (!hotOn) {
u8g2.print("HOT STARTING");
} else {
u8g2.print("HOT ON");
}
}
void setHotOff(void) {
if (isTimeForAChange) {
lastChangeTime = rtc.now();
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
u8g2.print("HOT OFF");
hotOn = false;
} else if (hotOn) {
u8g2.print("HOT STOPPING");
} else {
u8g2.print("HOT OFF");
}
}
void setFanOn(void) {
if (isTimeForAChange) {
lastChangeTime = rtc.now();
digitalWrite(GREEN_PIN, HIGH);
u8g2.print(" - FAN ON");
fanOn = true;
} else if (!fanOn) {
u8g2.print("- FAN STARTING");
} else {
u8g2.print(" - FAN ON");
}
}
void setFanOff(void) {
if (isTimeForAChange) {
lastChangeTime = rtc.now();
digitalWrite(GREEN_PIN, LOW);
u8g2.print(" - FAN OFF");
fanOn = false;
} else if (fanOn) {
u8g2.print("- FAN STOPPING");
} else {
u8g2.print(" - FAN OFF");
}
}
void printDate(void) {
u8g2.setCursor(0, 50);
rightNow = rtc.now();
date = fixNumber(rightNow.year());
date += "-";
date += fixNumber(rightNow.month());
date += "-";
date += fixNumber(rightNow.day());
date += " ";
date += fixNumber(rightNow.hour());
date += ":";
date += fixNumber(rightNow.minute());
date += ":";
date += fixNumber(rightNow.second());
u8g2.print(date);
}
String fixNumber(int number) {
switch (number) {
case (0):
return "00";
case (1):
return "01";
case (2):
return "02";
case (3):
return "03";
case (4):
return "04";
case (5):
return "05";
case (6):
return "06";
case (7):
return "07";
case (8):
return "08";
case (9):
return "09";
default:
return String(number);
}
}
void handleRoot() {
String html = "<!DOCTYPE html>\r\n<html>\r\n\t<head><meta name='viewport' content='width=device-width, initial-scale=1.0'/><meta charset='utf-8'><style>body {font-size:140%;} #main {display: table; margin: auto; padding: 0 10px 0 10px; } h2 {text-align:center; }</style><title>Aire Acondicionado</title></head>";
html += "<body><div id='main'><p>Aire fijado a : ";
html += String(temp);
html += "C</p><p>Temperatura actual : ";
html += String(t);
html += "C</p><p>Humedad actual : ";
html += String(h);
html += "%</p><p>Hora actual : ";
html += date;
html += "</p></body></html>";
server.send(200, "text/html", html);
}
void handleNotFound() {
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);
}