-
Notifications
You must be signed in to change notification settings - Fork 1
/
espegi.ino
283 lines (226 loc) · 7.21 KB
/
espegi.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
/***************************************************
Espegi - Sonoff (esp8266 based) control with Blynk and web interface
Created by: regi18
Version: Version: 3.5.0
Github:
regi18.ml | regi18.github.io
**************************************************/
#include "config.h"
#include "webpage.h"
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#ifdef DHT_SENSOR
#include <DHT.h>
#endif
int state = 0; // Variable to store the relay state
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = BUTTON_DEBOUNCE_DELAY; // The minimum time (ms) between button clicks
#ifdef DHT_SENSOR
float temp = 0, hum = 0; // Variables for the DHT sensor
#endif
ESP8266WebServer server(80);
#ifdef DHT_SENSOR
DHT dht(DHT_PIN, DHT_TYPE);
WiFiClient client;
#endif
BlynkTimer timer;
// Prototipes
void toggleRelay();
void handleRoot();
void handleToggleRelay();
void handleRestart();
void handleUpdatePage();
void updateFirmware();
void checkButton();
void getDhtData();
void sendDataTS();
void setup()
{
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
#ifdef DHT_SENSOR
pinMode(DHT_PIN, INPUT);
dht.begin();
timer.setInterval(15000L, getDhtData);
timer.setInterval(1200000L, sendDataTS);
#endif
timer.setInterval(150L, checkButton);
WiFi.hostname(HOSTNAME);
Blynk.begin(auth, ssid, wifiPassword);
digitalWrite(LED_PIN, 0);
delay(700);
digitalWrite(LED_PIN, 1);
delay(700);
digitalWrite(LED_PIN, 0);
delay(700);
digitalWrite(LED_PIN, 1);
// Web Server Setup
server.on("/", handleRoot);
server.on("/toggle", handleToggleRelay);
server.on("/restart", handleRestart);
server.on("/forcereload", handleRoot);
server.on("/togglecmd", handleToggleRelayCmd);
#ifdef DHT_SENSOR
server.on("/temperaturecmd", handleTemperatureCmd);
#endif
// Update page (where you can choose and upload a file)
server.on("/update", HTTP_GET, [](){
if(ota_username != emptyString && ota_password != emptyString && !server.authenticate(ota_username.c_str(), ota_password.c_str()))
return server.requestAuthentication();
handleUpdatePage();
});
server.on("/startupdate", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, updateFirmware);
server.begin();
}
void loop()
{
if (Blynk.connected()) { Blynk.run(); }
else { Blynk.connect(); }
timer.run();
server.handleClient();
}
BLYNK_CONNECTED() {
Blynk.syncAll();
}
/* ----------------------------------- RELAY ------------------------------------- */
/* Receives values from Blynk */
BLYNK_WRITE(RELAY_VPIN)
{
state = param.asInt();
digitalWrite(RELAY_PIN, state);
}
/* Toggle Relay */
void toggleRelay() {
state = !state;
digitalWrite(RELAY_PIN, state);
Blynk.virtualWrite(RELAY_VPIN, state);
}
/* Handle Physical Button */
void checkButton() {
if (!digitalRead(BUTTON_PIN)) {
if ((millis() - lastDebounceTime) > debounceDelay) { toggleRelay(); }
lastDebounceTime = millis();
}
}
/* ----------------------------------- WEBPAGE ------------------------------------- */
/* OTA Server */
void handleUpdatePage() {
String s = UPDATE_PAGE_PART1;
s += HOSTNAME;
s += UPDATE_PAGE_PART2;
server.send(200, "text/html", s);
}
/* Index page */
void handleRoot() {
String s = INDEX_START;
s += state ? "ON" : "OFF";
s += INDEX_PART1;
#ifdef DHT_SENSOR
s += INDEX_DHT_1;
s += temp;
s += INDEX_DHT_2;
s += hum;
s += INDEX_DHT_END;
#endif
s += INDEX_PART2;
s += HOSTNAME;
s += INDEX_END;
server.send(200, "text/html", s);
}
/* Handle web interface when toggling actuator */
void handleToggleRelay() {
toggleRelay();
handleRoot();
}
/* Update firmware and checks for authentication */
void updateFirmware() {
bool authenticated = (ota_username == emptyString || ota_password == emptyString || server.authenticate(ota_username.c_str(), ota_password.c_str()));
if(!authenticated) { return; }
const String updateMessage = UPDATE_MESSAGE;
const String EndUpdateMessage = END_UPDATE_MESSAGE;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) {
server.send(200, "text/html", updateMessage + "Error:<br/>!Update.begin(maxSketchSpace)" + EndUpdateMessage);
}
} else if (authenticated && upload.status == UPLOAD_FILE_WRITE) {
//flashing firmware to ESP
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
server.send(200, "text/html", updateMessage + "Error:<br/>Buffers sizes differs" + EndUpdateMessage);
}
} else if (authenticated && upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
server.send(200, "text/html", updateMessage + "Success!<br/>Rebooting..." + EndUpdateMessage);
} else {
server.send(200, "text/html", updateMessage + "Error!" + EndUpdateMessage);
}
}
else if(authenticated && upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
server.send(200, "text/html", updateMessage + "Error:<br/>Update was aborted" + EndUpdateMessage);
}
}
void handleRestart() {
ESP.restart();
}
/* Relay toggling without HTML page */
void handleToggleRelayCmd() {
toggleRelay();
server.send(200, "text/plain", "OK");
}
/* Simple temperature retrieve */
#ifdef DHT_SENSOR
void handleTemperatureCmd() {
String s = String(temp);
s += "°C, ";
s += hum;
s += "%";
server.send(200, "text/html; charset=utf-8", s);
}
#endif
/* ----------------------------------- DHT SENSOR ------------------------------------- */
#ifdef DHT_SENSOR
/* Get DHT data */
void getDhtData(void)
{
hum = dht.readHumidity(); // Read humidity (percent)
temp = dht.readTemperature(false); // Read temperature as Celsius
if (isnan(hum) || isnan(temp)) // Check if any reads failed and exit early (to try again).
{
return;
}
Blynk.virtualWrite(TEMP_VPIN, temp);
Blynk.virtualWrite(HUM_VPIN, hum);
}
/* Send DHT data to thinkspeak */
void sendDataTS(void)
{
if (client.connect(TS_SERVER, 80))
{
String postStr = TS_API_KEY;
postStr += "&field1=";
postStr += String(temp);
postStr += "&field2=";
postStr += String(hum);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + TS_API_KEY + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(1000);
}
client.stop();
}
#endif