-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiSwitchWeb.ino
383 lines (311 loc) · 9.55 KB
/
wifiSwitchWeb.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
/*
*
*/
#include <EEPROM.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <DNSServer.h>
ESP8266WebServer server ( 80 );
//define section
#define DEBUG //uncomment if you want debug
// NTP Servers:
static const char ntpServerName[] = "us.pool.ntp.org";
//static const char ntpServerName[] = "time.nist.gov";
//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
//static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";
//static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov";
const int timeZone = 5; //ufa
WiFiUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
time_t prevDisplay = 0; // when the digital clock was displayed
time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);
int hourON = 19;
int minuteON = 00;
int hourOFF = 23;
int minuteOFF = 00;
int relayPin = 4;
bool relayStatus;
bool saveUptime = false;
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
delay(250);
Serial.println("TimeNTP Example");
Serial.print("Connecting to ");
#endif
///////////////////////////////////WIFI MANAGER/////////////////////
WiFiManager wifiManager;
wifiManager.autoConnect();
server.close();
//////////////////////////////////WIFI////////////////////////////
Udp.begin(localPort);
#ifdef DEBUG
Serial.print("Local port: ");
Serial.println(Udp.localPort());
Serial.println("waiting for sync");
#endif
///////////////////////////////////WEB SREVER////////////////////////
server.on ( "/", handleRoot );
//server.on ( "/test.svg", drawGraph );
server.on ( "/inline", []()
{
server.send ( 200, "text/plain", "this works as well" );
} );
server.onNotFound ( handleNotFound );
server.begin();
#ifdef DEBUG
Serial.println ( "HTTP server started" );
#endif
//////////////////////////////////TIME////////////////////////////
setSyncProvider(getNtpTime);
setSyncInterval(300);
pinMode(relayPin, OUTPUT);
//////////////////////////////////eeprom////////////////////////
EEPROM.begin(512);
eepromRead();
}
void loop()
{
//////////////////////////////////save uptime///////////////////////
if ((now() > 1480896000) && !saveUptime)
{
EEPROM_ulong_write(5,now());
saveUptime = true;
}
server.handleClient();
if (timeStatus() != timeNotSet)
{
if (now() != prevDisplay)
{ //update the display only if time has changed
prevDisplay = now();
#ifdef DEBUG
digitalClockDisplay();
#endif
relayStatus = relayStatusCheck();
if (relayStatus)
{
digitalWrite (relayPin,LOW);
}
if (!relayStatus)
{
digitalWrite (relayPin,HIGH);
}
}
}
/////////////////////////////////args check///////////////
checkWebArgs();
}
void checkWebArgs()
{
String stemp;
long temp = 100;
if (!server.hasArg("hourON")) return;
for (int i=0; i < 4; ++i)
{
stemp = server.arg(i);
if (stemp != "") temp = stemp.toInt();
else temp = 100;
if ((i == 0) && (temp <= 24) && (temp >= 0)) hourON = temp;
if ((i == 1) && (temp <= 60) && (temp >= 0)) minuteON = temp;
if ((i == 2) && (temp <= 24) && (temp >= 0)) hourOFF = temp;
if ((i == 3) && (temp <= 60) && (temp >= 0)) minuteOFF = temp;
}
eepromWrite();
}
void eepromRead()
{
hourON = EEPROM.read(0);
minuteON = EEPROM.read(1);
hourOFF = EEPROM.read(2);
minuteOFF = EEPROM.read(3);
}
void eepromWrite()
{
EEPROM.write(0, hourON);
EEPROM.write(1, minuteON);
EEPROM.write(2, hourOFF);
EEPROM.write(3, minuteOFF);
EEPROM.commit();
}
// чтение
unsigned long EEPROM_ulong_read(int addr) {
byte raw[4];
for(byte i = 0; i < 4; i++) raw[i] = EEPROM.read(addr+i);
unsigned long &num = (unsigned long&)raw;
return num;
}
// запись
void EEPROM_ulong_write(int addr, unsigned long num) {
byte raw[4];
(unsigned long&)raw = num;
for(byte i = 0; i < 4; i++) EEPROM.write(addr+i, raw[i]);
}
bool relayStatusCheck()
{
bool s;
unsigned long todaySecs;
unsigned long duration;
long secON;
long secOFF;
secON = (hourON * 3600) + (minuteON * 60);
secOFF = (hourOFF * 3600) + (minuteOFF * 60);
todaySecs = now();
todaySecs %= 86400; //узнаем сколько секунд прошло за день
if (secOFF > secON)
{
if ((todaySecs >= secON) && (todaySecs < secOFF)) s = true; // если врем¤ больше включние¤ и меньше выключени¤ то ON
else s = false;
}
if (secOFF < secON)
{
if ((todaySecs >= secOFF) && (todaySecs < secON)) s = false; // врем¤ больше выключени¤ и меньше включени¤ - OFF
else s = true;
}
#ifdef DEBUG
Serial.print(F("now = "));
Serial.print(todaySecs);
Serial.print(F("; on = "));
Serial.print(secON);
Serial.print(F("; off= "));
Serial.println(secOFF);
Serial.print(F("; relayStatus = "));
Serial.println(s);
#endif
return s;
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(".");
Serial.print(month());
Serial.print(".");
Serial.print(year());
Serial.println();
}
void printDigits(int digits)
{
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
IPAddress ntpServerIP; // NTP server's ip address
while (Udp.parsePacket() > 0) ; // discard any previously received packets
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
#ifdef DEBUG
Serial.println("Transmit NTP Request");
Serial.print(ntpServerName);
Serial.print(": ");
Serial.println(ntpServerIP);
#endif
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
#ifdef DEBUG
Serial.println("Receive NTP Response");
#endif
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
#ifdef DEBUG
Serial.println("No NTP Response :-(");
#endif
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
void handleRoot() {
char temp[800];
int sec = now() % 86400;
int min = sec / 60;
int hr = min / 60;
unsigned long uptime = (int)(now() - EEPROM_ulong_read(5))/3600;
snprintf ( temp, 800,
"<html>\
<head>\
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no' >\
<title>ESP8266 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from ESP8266!</h1>\
<p>Time: %02d:%02d:%02d // Uptime: %02d hours</p>\
<p>Relay ON = %02d:%02d</p>\
<p>Relay OFF = %02d:%02d</p>\
<p>Relay Status = %02d</p>\
<form> \
<p> ON Time: <input type=text name=hourON size=2> : <input type=text name=minuteON size=2> </p>\
<p> OFF Time: <input type=text name=hourOFF size=2> : <input type=text name=minuteOFF size=2> </p>\
<p><input type=submit value=Save></p> \
</form>\
</body>\
</html>",
hr, min % 60, sec % 60, uptime, hourON, minuteON, hourOFF, minuteOFF, relayStatus
);
server.send ( 200, "text/html", temp );
}
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 );
}