-
Notifications
You must be signed in to change notification settings - Fork 3
/
Smart_Home_Relay.ino
293 lines (272 loc) · 12.5 KB
/
Smart_Home_Relay.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
/*
written by Jake Pring from CircuitSpecialists.com
licensed as GPLv3
commands:
0xA00100A10D0A opens relay
0xA00101A20D0A closes relay
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266HTTPUpdateServer.h>
const char* ssid = "";
const char* password = "";
bool use_static_ip = false;
IPAddress static_address(192, 168, 1, 253);
IPAddress gateway_ip(192, 168, 1, 1);
IPAddress subnet_mask(255, 255, 255, 0);
IPAddress dns_one(8, 8, 8, 8);
IPAddress dns_two(8, 8, 4, 4);
const int esp_OTA_port = 8266;
const char* esp_hostname = "esp8266_OTA";
bool use_password = false;
const char* esp_password = "admin";
bool use_https = false;
const char* esp_http_update_hostname = "esp8266";
const char* update_path = "/firmware";
const char* update_username = "admin";
const char* update_password = "admin";
// The certificate is stored in PMEM
static const uint8_t x509[] = {
0x30, 0x82, 0x01, 0xc9, 0x30, 0x82, 0x01, 0x32, 0x02, 0x09, 0x00, 0xe6,
0x60, 0x8d, 0xa3, 0x47, 0x8f, 0x57, 0x7a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x29,
0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0a, 0x70,
0x73, 0x79, 0x63, 0x68, 0x6f, 0x70, 0x6c, 0x75, 0x67, 0x31, 0x12, 0x30,
0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x31, 0x32, 0x37, 0x2e,
0x30, 0x2e, 0x30, 0x2e, 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30,
0x32, 0x32, 0x34, 0x30, 0x38, 0x30, 0x35, 0x33, 0x36, 0x5a, 0x17, 0x0d,
0x33, 0x30, 0x31, 0x31, 0x30, 0x33, 0x30, 0x38, 0x30, 0x35, 0x33, 0x36,
0x5a, 0x30, 0x29, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a,
0x0c, 0x0a, 0x70, 0x73, 0x79, 0x63, 0x68, 0x6f, 0x70, 0x6c, 0x75, 0x67,
0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x31,
0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x30, 0x81, 0x9f, 0x30,
0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
0x05, 0x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, 0x70, 0x3f, 0x78,
0xc6, 0xe1, 0x5a, 0xac, 0xb1, 0x93, 0x5b, 0x41, 0x35, 0x45, 0x47, 0xf8,
0x07, 0x86, 0x40, 0x34, 0xa2, 0x9e, 0x2a, 0x16, 0x8d, 0xea, 0xf9, 0x1e,
0x1f, 0xd7, 0x70, 0xb4, 0x28, 0x6b, 0xd8, 0xf5, 0x3f, 0x33, 0x3f, 0xc2,
0x2c, 0x69, 0xf2, 0xa3, 0x54, 0x4d, 0xbf, 0x7d, 0xf9, 0xde, 0x05, 0x0c,
0x9c, 0xe3, 0x1b, 0x72, 0x07, 0x7b, 0x41, 0x76, 0x1a, 0x57, 0x03, 0x5d,
0xb2, 0xff, 0x4c, 0x17, 0xbd, 0xd7, 0x73, 0x32, 0x98, 0x26, 0x6b, 0x2c,
0xc4, 0xbf, 0x6e, 0x01, 0x36, 0x8b, 0xbf, 0x00, 0x48, 0x9c, 0xfb, 0x3d,
0x7d, 0x76, 0x1f, 0x55, 0x96, 0x43, 0xc5, 0x4e, 0xc1, 0xa3, 0xa1, 0x6a,
0x94, 0x5f, 0x84, 0x3a, 0xdd
};
// And so is the key. These could also be in DRAM
static const uint8_t rsakey[] = {
0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xb6,
0x59, 0xd0, 0x57, 0xbc, 0x3e, 0xb9, 0xa0, 0x6c, 0xf5, 0xd5, 0x46, 0x49,
0xaa, 0x9a, 0xb3, 0xbf, 0x09, 0xa9, 0xbb, 0x82, 0x3b, 0xdf, 0xb7, 0xe3,
0x5a, 0x8e, 0x31, 0xf7, 0x27, 0xdf, 0xaa, 0xed, 0xa3, 0xd6, 0xf6, 0x74,
0x35, 0xfc, 0x8d, 0x0b, 0xbc, 0xa2, 0x96, 0x10, 0x57, 0xe8, 0xb2, 0xaa,
0x94, 0xf2, 0x47, 0x12, 0x4e, 0x3f, 0x7c, 0x5e, 0x90, 0xfe, 0xad, 0x75,
0x88, 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6d, 0x9d, 0xdb, 0x7b, 0x42, 0xe0, 0x0c, 0xab, 0xb7, 0x33,
0x22, 0x2a, 0xf4, 0x7e, 0xff, 0xae, 0x80, 0xb4, 0x8f, 0x88, 0x0a, 0x46,
0xb2, 0xf8, 0x43, 0x11, 0x92, 0x76, 0x61, 0xbd, 0x02, 0x40, 0x5c, 0x86,
0x3a, 0xdc, 0x33, 0x1a, 0x0e, 0xcb, 0xa7, 0xb9, 0xf6, 0xae, 0x47, 0x5e,
0xbc, 0xff, 0x18, 0xa2, 0x8c, 0x66, 0x1a, 0xf4, 0x13, 0x00, 0xa2, 0x9d,
0x3e, 0x5c, 0x9e, 0xe6, 0x4c, 0xdd, 0x4c, 0x0f, 0xe2, 0xc2, 0xe4, 0x89,
0x60, 0xf3, 0xcc, 0x8f, 0x3a, 0x5e, 0xce, 0xaa, 0xbe, 0xd8, 0xb6, 0x4e,
0x4a, 0xb5, 0x4c, 0x0f, 0xa5, 0xad, 0x78, 0x0f, 0x15, 0xd8, 0xc9, 0x4c,
0x2b, 0xc1, 0x02, 0x40, 0x4e, 0xe9, 0x78, 0x48, 0x94, 0x11, 0x75, 0xc1,
0xa2, 0xc7, 0xff, 0xf0, 0x73, 0xa2, 0x93, 0xd7, 0x67, 0xc7, 0xf8, 0x96,
0xac, 0x15, 0xaa, 0xe5, 0x5d, 0x18, 0x18, 0x29, 0xa9, 0x9a, 0xfc, 0xac,
0x48, 0x4d, 0xa0, 0xca, 0xa2, 0x34, 0x09, 0x7c, 0x13, 0x22, 0x4c, 0xfc,
0x31, 0x75, 0xa0, 0x21, 0x1e, 0x7a, 0x91, 0xbc, 0xb1, 0x97, 0xde, 0x43,
0xe1, 0x40, 0x2b, 0xe3, 0xbd, 0x98, 0x44, 0xad
};
// Create an instance of the server with port
ESP8266WebServer httpServer(80);
ESP8266WebServerSecure httpsServer(443);
ESP8266HTTPUpdateServer httpUpdater;
void setup() {
Serial.begin(9600);
delay(10);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(3000);
ESP.restart();
}
Serial.println("\nWiFi connected");
ArduinoOTA.setPort(esp_OTA_port);
ArduinoOTA.setHostname(esp_hostname);
if (use_password) {
MD5Builder md5;
md5.begin();
md5.add(esp_password);
md5.calculate();
ArduinoOTA.setPassword(esp_password);
ArduinoOTA.setPasswordHash(md5.toString().c_str());
}
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
switch (error) {
case OTA_AUTH_ERROR:
Serial.println("Auth Failed");
break;
case OTA_BEGIN_ERROR:
Serial.println("Begin Failed");
break;
case OTA_CONNECT_ERROR:
Serial.println("Connect Failed");
break;
case OTA_RECEIVE_ERROR:
Serial.println("Receive Failed");
break;
case OTA_END_ERROR:
Serial.println("End Failed");
break;
}
});
// Start the OTA server
ArduinoOTA.begin();
Serial.println("OTA Started... Ready");
// Start the HTTP Server
MDNS.begin(esp_http_update_hostname);
httpServer.on("/", handleRoot);
/*********************************************
URI Script functions
**********************************************/
httpServer.on("/turnon", []() {
relay(1);
httpServer.send(200, "text/plain", "Relay has been set to the on position");
});
httpServer.on("/turnoff", []() {
relay(0);
httpServer.send(200, "text/plain", "Relay has been set to the off position");
});
httpServer.on("/garagedoor", []() {
relay(1);
delay(180);
relay(0);
httpServer.send(200, "text/plain", "Garagedoor cycled");
});
httpServer.on("/usepassword", []() {
MD5Builder md5;
md5.begin();
md5.add(esp_password);
md5.calculate();
ArduinoOTA.setPassword(esp_password);
ArduinoOTA.setPasswordHash(md5.toString().c_str());
httpServer.send(200, "text / plain", "Password Set " + (String)esp_password + " hash: " + md5.toString());
});
httpServer.onNotFound(handleNotFound);
/*********************************************
END
**********************************************/
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username '%s' and password '%s'\n", esp_http_update_hostname, update_path, update_username, update_password);
// Start the HTTPS Update Server
if (use_https) {
httpsServer.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
httpUpdater.setup(&httpsServer, update_path, update_username, update_password);
httpsServer.begin();
MDNS.addService("https", "tcp", 443);
Serial.printf("HTTPSUpdateServer ready!\nOpen https://%s.local%s in your browser and login with username '%s' and password '%s'\n", esp_http_update_hostname, update_path, update_username, update_password);
}
// Print the IP address
Serial.println(WiFi.localIP());
delay(100);
if (use_static_ip) {
WiFi.config(static_address, gateway_ip, subnet_mask, dns_one, dns_two);
Serial.println(WiFi.localIP());
}
}
void loop() {
ArduinoOTA.handle();
httpServer.handleClient();
}
void relay(bool value) {
const byte close_relay[] = {0xA0, 0x01, 0x01, 0xA2, 0x0D, 0x0A};
const byte open_relay[] = {0xA0, 0x01, 0x00, 0xA1, 0x0D, 0x0A};
(value) ? Serial.write(close_relay, sizeof(close_relay)) : Serial.write(open_relay, sizeof(open_relay));
}
void handleRoot() {
httpServer.send(200, "text/plain", "http server running on my esp!");
}
void handleNotFound() {
String message = "File Not Found\n\nURI: ";
message += httpServer.uri();
message += "\nMethod: ";
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += httpServer.args();
message += "\n";
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
}
httpServer.send(404, "text/plain", message);
}