-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.ino
236 lines (206 loc) · 10.7 KB
/
webserver.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
//
// webserver.ino - Implement a web control panel for the Esp9266 Starter system.
//
// Pages of the control website include a left-side button panel with buttons that
// either link to other pages of the system or invoke operations as follows:
//
// Button Function
// ----------- -----------------
// Home Displays basic operational status
// History Links to a page with the Event History (see eventHistory.ino)
// Settings Links to a page to edit/save the settings file managed by prefsMfr.ino (uses html <textarea>)
// Restart Calls doSoftReset() function in Esp8266Starter.ino to inits counters, timers, etc
// Send Mail 1 Results in a call to sendmail(1) through a helper function in Esp8266Starter.ino
// Send Mail 2 Results in a call to sendmail(2) through a helper function in Esp8266Starter.ino
// Erase WiFi calls WiFiManager.resetSettings() then ESP.reset() (after confirm popup)
// Hard Reset calls ESP.reset() (after confirm popup)
//
// To run webserver, call setupWebserver() in setup() and handleWebseverProcessing() in loop()
//
// Dependencies
// - ESP8266WebServer, WiFiManager
// - From prefsMgr.ino - loadPreferencesFileToString(), saveUpdatedPreferencesFile()
// - From eventHistory.ino - addEventHistoryEntry(), getEventHistoryTimestamp(),
// getEventHistoryString(), getFullEventHistoryAsString (from eventHistory.ino)
// - From dateTimeUtils.ino - elapsedMillisToVerboseString(), elapsedMillisToDateString(),
// elapsedMillisToString(), epochTimeToDateString()
// - Various global vars and functions from Esp8266Starter.ino
//
// The webserver implementation is fully functional but is intended as a starter system that
// will be extensively customized based on its intended application
//
ESP8266WebServer server(80);
#define SHOW_STATUS_IN_CONTENT_PANE 1
#define SHOW_HISTORY_IN_CONTENT_PANE 2
#define SHOW_SETTINGS_IN_CONTENT_PANE 3
void setupWebserver()
{
server.on("/", handle_OnConnect);
server.on("/show_event_history", handle_history);
server.on("/configure_settings", handle_settings);
server.on("/settings_save", handle_settings_save);
server.on("/send_mail1", handle_sendMail1);
server.on("/send_mail2", handle_sendMail2);
server.on("/do_soft_reset", handle_soft_reset);
server.on("/do_erase_wifi", handle_erase_wifi);
server.on("/do_hard_reset", handle_hard_reset);
server.onNotFound(handle_NotFound);
server.begin();
}
void handleWebseverProcessing() {
server.handleClient();
}
void handle_soft_reset() {
timeSinceLastWebActivity=0;
Serial.println("Performing soft reset");
doSoftReset();
server.sendHeader("Location","/");
server.send(303);
server.send(200, "text/html", SendHTML(SHOW_STATUS_IN_CONTENT_PANE));
}
void handle_hard_reset() {
Serial.println("Performing hard reset");
server.sendHeader("Location","/");
server.send(303);
server.send(200, "text/html", SendHTML(SHOW_STATUS_IN_CONTENT_PANE));
delay(500);
ESP.reset();
}
void handle_erase_wifi() {
Serial.println("Erasing wifi settings");
delay(500);
WiFiManager wifiManager;
wifiManager.resetSettings();
server.sendHeader("Location","/");
server.send(303);
server.send(200, "text/html", SendHTML(SHOW_STATUS_IN_CONTENT_PANE));
delay(700);
ESP.reset();
}
void handle_sendMail1() {
timeSinceLastWebActivity=0;
Serial.println("Calling sendMailMessageType1().");
sendMailMessageType1();
server.sendHeader("Location","/");
server.send(303);
server.send(200, "text/html", SendHTML(SHOW_STATUS_IN_CONTENT_PANE));
}
void handle_sendMail2() {
timeSinceLastWebActivity=0;
Serial.println("Calling sendMailMessageType2().");
sendMailMessageType2();
server.sendHeader("Location","/");
server.send(303);
server.send(200, "text/html", SendHTML(SHOW_STATUS_IN_CONTENT_PANE));
}
void handle_history() {
server.send(200, "text/html", SendHTML(SHOW_HISTORY_IN_CONTENT_PANE));
}
void handle_settings() {
timeSinceLastWebActivity=0;
server.send(200, "text/html", SendHTML(SHOW_SETTINGS_IN_CONTENT_PANE));
}
void handle_settings_save() {
String configBuffer = server.arg("configBuffer");
timeSinceLastWebActivity=0;
saveUpdatedPreferencesFile(configBuffer);
addEventHistoryEntry("Configuration settings were saved");
server.sendHeader("Location","/configure_settings");
server.send(303);
server.send(200, "text/html", SendHTML(SHOW_SETTINGS_IN_CONTENT_PANE));
}
void handle_OnConnect() {
timeSinceLastWebActivity=0;
server.send(200, "text/html", SendHTML(SHOW_STATUS_IN_CONTENT_PANE));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String ptr;
String SendHTML(int pageToShowInContentPane){
ptr.reserve(500); // Needs to be big. Set initial size and allocation size to avoid heap frag
ptr = "<!DOCTYPE html> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"initial-scale=1.0\">\n";
ptr += "<title>ESP8266 Starter System</title>\n";
ptr += "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">\n";
ptr += "<style>html,body {height: 100%;display: flex;flex-direction: column;font-family: Helvetica;margin: 0px;}\n";
ptr += "#header {padding: 10px;background-color: blue;text-align:center;color: white;border-bottom: 1px solid black;}\n";
ptr += "#headerRightText {float: right; line-height: 2.8; }\n";
ptr += "#container {padding: 0;margin: 0;display: flex;flex-direction: row;flex-grow: 1;}\n";
ptr += "#sidebar {padding: 12px;background-color: #e7e7e7;}\n";
ptr += "#content {flex-grow: 1;padding: 10px;}\n";
ptr += "h1 {display: inline; margin: 0;text-align: center;}\n";
ptr += "h3 {text-align: center; color: #444444;}\n";
ptr += "p {font-size: 16px; color: #888;}\n";
ptr += ".button {display:block;color:white;padding: 7px;text-align:center;text-decoration: none;font-size: 16px; margin: 0px auto 15px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-green {background-color: #1abc9c;}\n";
ptr += ".button-red {background-color: #B5153F;}\n";
ptr += ".button-yellow {background-color: #E2d606;}\n";
ptr += "</style></head>\n";
ptr += "<body><div id=\"header\"><h1>ESP8266 System Monitor</h1></div>\n";
ptr += "<div id=\"container\"><div id=\"sidebar\">\n"; //<h2>Controls</h2>\n";
ptr += "<a class=\"button button-green\" href=\"/\">Home</a>\n";
ptr += "<a class=\"button button-green\" href=\"/show_event_history\">History</a>\n";
ptr += "<a class=\"button button-green\" href=\"/configure_settings\">Settings</a>\n";
ptr += "<a class=\"button button-yellow\" href=\"/do_soft_reset\"\">Restart</a>\n";
ptr += "<a class=\"button button-yellow\" href=\"/send_mail1\">Send Mail 1</a>\n";
ptr += "<a class=\"button button-yellow\" href=\"/send_mail2\">Send Mail 2</a>\n";
ptr += "<a class=\"button button-red\" href=\"/do_erase_wifi\" onclick=\"return confirm('Erase all WiFi settings and reboot as 192.168.4.1. Are you sure?')\">Erase Wifi</a>\n";
ptr += "<a class=\"button button-red\" href=\"/do_hard_reset\"onclick=\"return confirm('Perform hardware reset. Are you sure?')\">Hard Reset</a>\n";
if (pageToShowInContentPane == SHOW_STATUS_IN_CONTENT_PANE)
addCurrentStatusWebPageContents(ptr);
else if (pageToShowInContentPane == SHOW_HISTORY_IN_CONTENT_PANE)
addHistoryWebPageContents(ptr);
else if (pageToShowInContentPane == SHOW_SETTINGS_IN_CONTENT_PANE)
addSettingsWebPageContents(ptr);
ptr += "</pre></div></div></body></html>\n";
return ptr;
}
void addCurrentStatusWebPageContents(String& ptr) {
ptr += "</div><div id=\"content\"><h3>Current Status</h3><pre>\n";
// For better heap management, use "ptr+=" when concating strings (mutate object instead of creating new object)
// this is more efficient than ptr += "a" + "b" + "c"; which creates many objects and heap fragmentation
ptr += " System Uptime: "; ptr += elapsedMillisToVerboseString(timeSinceSystemStarted); ptr += "\n";
if (timeSinceWebSessionStarted < MILLIS_IN_A_DAY)
{ptr += " Session Time: "; ptr += elapsedMillisToVerboseString(timeSinceWebSessionStarted); ptr += "\n";}
ptr += " Free Memory: "; ptr += String(freeMemory(), DEC); ptr += " bytes\n\n";
if (email1SentCount > 0)
{ptr += " Email 1 Messages: "; ptr += String(email1SentCount, DEC); ptr += "\n";}
if (email2SentCount > 0)
{ptr += " Email 2 Messages: "; ptr += String(email2SentCount, DEC); ptr += "\n";}
if (emailSupressedCount > 0)
{ptr += " Emails Supressed: "; ptr += String(emailSupressedCount, DEC); ptr += "\n";}
if (emailErrorCount > 0)
{ptr += "Email Send Errors: "; ptr += String(emailErrorCount, DEC); ptr += "\n";}
if (getNumberOfEventHistoryEntries() > 0)
{ptr += " History Entries: "; ptr += String(getNumberOfEventHistoryEntries(), DEC); ptr += "\n";}
ptr +="\n Current Time: "; ptr += elapsedMillisToDateString(0); ptr += "\n\n";
if ((email1SentCount > 0) || (email2SentCount > 0)) {
ptr +=" Last Email Sent: "; ptr += elapsedMillisToDateString(timeSinceLastEmailSentOk);
ptr += " ("; ptr += elapsedMillisToString(timeSinceLastEmailSentOk); ptr += " ago)\n"; }
if ((email1SentCount > 0) || (email2SentCount > 0) || (emailSupressedCount > 0) || (emailErrorCount > 0)) {
ptr +=" Last Email Attempt: "; ptr += elapsedMillisToDateString(timeSinceLastEmailAttempt);
ptr += " ("; ptr += elapsedMillisToString(timeSinceLastEmailAttempt); ptr += " ago)\n"; }
if (emailErrorCount > 0) {
ptr +=" Last Email Error: "; ptr += elapsedMillisToDateString(timeSinceLastEmailSendError);
ptr += " ("; ptr += elapsedMillisToString(timeSinceLastEmailSendError); ptr += " ago)\n"; }
if (softResetCount > 0) {
ptr +=" Last Soft Reset: "; ptr += elapsedMillisToDateString(timeSinceSoftReset);
ptr += " ("; ptr += elapsedMillisToString(timeSinceSoftReset); ptr += " ago)\n"; }
if (getNumberOfEventHistoryEntries() > 0) {
ptr +="\n Last Event: "; ptr += epochTimeToDateString(getEventHistoryTimestamp(1));
ptr +=" "; ptr += getEventHistoryString(1); ptr += "\n"; }
}
void addHistoryWebPageContents(String& ptr) {
ptr += "</div><div id=\"content\"><h3>Event History</h3><pre>\n";
ptr +=" Current Time: "; ptr += elapsedMillisToDateString(0); ptr += "\n\n";
ptr += getFullEventHistoryAsString(100);
}
void addSettingsWebPageContents(String& ptr) {
ptr += "</div><div id=\"content\"><h3>Current Configuration Settings</h3>\n";
ptr += "<FORM METHOD=\"POST\" action=\"/settings_save\">";
ptr += "<textarea id=\"configBuffer\" name=\"configBuffer\" style=\"display:block\" rows=\"20\" cols=\"90\" spellcheck=\"false\">";
ptr += loadPreferencesFileToString();
ptr += "</textarea><input type=\"submit\" value=\"Save Changes\"></form>";
ptr += "<pre>";
}