-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_manager.cpp
507 lines (420 loc) · 17.7 KB
/
server_manager.cpp
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* @Author: Jakub Witowski
* @Project name: iBeacon
* @File name: serial_manager.cpp
*/
/* ==================================================================== */
/* ========================== include files =========================== */
/* ==================================================================== */
#include "server_manager.h"
/* ==================================================================== */
/* ======================== global variables ========================== */
/* ==================================================================== */
/* Create WebServer instance */
ESP8266WebServer WServer(80);
/* Serber Manager handler */
Server_Manager server;
/* NvM handler */
extern Nvm_Manager eeprom;
/* Sensor handler */
extern Sensor sensor;
/* Sensor values structure handler */
extern Sensor::Sensor_Values_T sens_val;
/* Upadte manager handler */
extern Update_Manager ota;
/* SensorState struct handler */
Server_SensorState_T sensorState;
/* Username and Password of the Website access */
String user_username;
String user_password;
/* Initialize the GpioState tab */
String GpioState[GPIO_REMOTE_USED] = {"OFF", "OFF"};
/* ==================================================================== */
/* ================== local function declarations ===================== */
/* ==================================================================== */
inline void handleControlData();
inline void handleLogin();
inline void handleUpdate();
/* ==================================================================== */
/* ================== local function definitions ===================== */
/* ==================================================================== */
/*
* handleControlData()
* - This functions handles the Server's requests related to the control website
*/
void handleControlData()
{
Server_Manager s;
String header;
if(!s.Server_IsAuthentified())
{
WServer.sendHeader("Location","/login");
WServer.sendHeader("Cache-Control","no-cache");
WServer.send(301);
}
else
{
/* Server authenticate passed ! */
if(WServer.hasArg("D4"))
{
s.Server_UpdateGPIO(Gpio_ID_D4, WServer.arg("D4"));
}
else if(WServer.hasArg("D5"))
{
s.Server_UpdateGPIO(Gpio_ID_D5, WServer.arg("D5"));
}
else
{
WServer.send(200, "text/html", s.Server_GetControlPage());
}
}
}
/*
* handleLogin()
* - This functions handles the Server's requests related to the login website
*/
void handleLogin()
{
Server_Manager s;
String msg = "";
if(WServer.hasHeader("Cookie"))
{
String cookie = WServer.header("Cookie");
}
if(WServer.hasArg("DISCONNECT"))
{
Serial.printf("SERVER -> LOGOUT\r\n");
WServer.sendHeader("Location","/login");
WServer.sendHeader("Cache-Control","no-cache");
WServer.sendHeader("Set-Cookie","ESPSESSIONID=0");
WServer.send(301);
}
else
{
if(WServer.hasArg("USERNAME") && WServer.hasArg("PASSWORD"))
{
/* Check if ligin credentials are appropriate */
if(WServer.arg("USERNAME") == user_username && WServer.arg("PASSWORD") == user_password)
{
WServer.sendHeader("Location","/");
WServer.sendHeader("Cache-Control","no-cache");
WServer.sendHeader("Set-Cookie","ESPSESSIONID=1");
WServer.send(301);
Serial.printf("SERVER -> LOGIN OK\r\n");
}
else
{
msg = "Wrong username or password!";
Serial.printf("SERVER -> LOGIN ERROR\r\n");
}
}
/* Show Login page */
WServer.send(200, "text/html", s.Server_GetLoginPage(msg));
}
}
/*
* handleUpdate()
* - This functions handles the Server's requests related to the update website
*/
void handleUpdate()
{
/* Initialize update manager */
ota.Update_Manager_Init();
WServer.send(200, "text/html", "<html>For update visit: <a href=\"url\">http://esp8266-webupdate/firmware</a></html>");
}
/* ==================================================================== */
/* ============================ functions ============================= */
/* ==================================================================== */
/*
* Server_Init()
* - This functions initializes the server
* - It should be called when the WiFi connection is established
*/
void Server_Manager::Server_Init()
{
/* Read Username and Password for Website access from NvM */
eeprom.Nvm_CredentialsRead(EEPROM_USER_CREDENTIALS_START_ADDR, user_username, user_password);
/* Connect the callbacks */
WServer.on("/", handleControlData);
WServer.on("/login", handleLogin);
WServer.on("/update", handleUpdate);
/* List of headers to be recorded */
const char *headerkeys[] = {"User-Agent","Cookie"};
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
/* Ask server to track these headers */
WServer.collectHeaders(headerkeys, headerkeyssize);
WServer.begin();
Serial.printf("SERVER -> Started\r\n");
Serial.printf("SERVER -> USERNAME: %s\r\n", user_username.c_str());
Serial.printf("SERVER -> PASSWORD: %s\r\n", user_password.c_str());
}
/*
* Server_HandleClient()
* - This functions handles Clients requests (webbrowsers requests)
* - This function should be called periodically in the loop
*/
void Server_Manager::Server_HandleClient()
{
WServer.handleClient();
}
/*
* Server_IsAuthentified()
* - This function checks if header is present and correct
*/
bool Server_Manager::Server_IsAuthentified()
{
bool success_status = false;
if(WServer.hasHeader("Cookie"))
{
String cookie = WServer.header("Cookie");
if(cookie.indexOf("ESPSESSIONID=1") != -1)
{
success_status = true;
}
else
{
Serial.printf("SERVER -> Authentication Failed\r\n");
}
}
return success_status;
}
/*
* Server_UpdateGPIO()
* - This functions updates the GPIO information in website's source code
*/
void Server_Manager::Server_UpdateGPIO(Gpio_ID_T gpio_id, String gpio_state)
{
Serial.printf("GPIO -> %d: ", GpioPin[gpio_id]);
if(gpio_state == "1")
{
Serial.printf("ON\r\n");
digitalWrite(GpioPin[gpio_id], HIGH);
/* Change span's value */
GpioState[gpio_id] = "On";
WServer.send(200, "text/html", Server_GetControlPage());
}
else if(gpio_state == "0")
{
Serial.printf("OFF\r\n");
digitalWrite(GpioPin[gpio_id], LOW);
/* Change span's value */
GpioState[gpio_id] = "Off";
WServer.send(200, "text/html", Server_GetControlPage());
}
else
{
Serial.printf("GPIO -> Unknown request\r\n");
}
}
/*
* Server_Update_SensorsState()
* - This functions updates the environment sensors status on the website
*/
void Server_Manager::Server_Update_SensorsState(String t_status, String p_status, String h_status, String l_status)
{
sensorState.Temp_SensorState = t_status;
sensorState.Pres_SensorState = p_status;
sensorState.Humid_SensorState = h_status;
sensorState.Light_SensorState = l_status;
}
/*
* Server_GetControlPage()
* - This functions prints the info website on the server
*/
String Server_Manager::Server_GetControlPage()
{
String webpage = "";
webpage += "<html charset=UTF-8><head><meta http-equiv='refresh' content='60' name='viewport' content='width=device-width, initial-scale=1'/>";
webpage += "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script><script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>";
webpage += "<link href='https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/darkly/bootstrap.min.css' rel='stylesheet'>";
webpage += "<title>ESP8266 IoT - EMBEDDED SOLUTIONS jakub.witowski9302(at)gmail.com</title></head><body>";
webpage += "<div class='container-fluid'>";
webpage += "<div class='row'>";
webpage += "<div class='col-md-12'>";
webpage += "<div class='page-header'>";
webpage += "<h1>";
webpage += "iBeacon! <small>Smart home solutions</small>";
webpage += "<a href=\'/login?DISCONNECT=YES\' class='btn' type='button'>LogOut</a>";
webpage += "<a href=\'/update' class='btn' type='button'>Update</a>";
webpage += "</h1>";
webpage += "</div>";
webpage += "</div>";
webpage += "</div>";
webpage += "<div class='row'>";
webpage += "<div class='col-md-6'>";
webpage += "<div class='page-header'><h1><small>Light management</small></h1></div>";
webpage += "<div class='row'>";
webpage += "<div class='col-md-4'>";
webpage += "<ul class='nav nav-pills'>";
webpage += "<li class='active'>";
webpage += "<a href='#'>";
webpage += "<span class='badge pull-right'>";
webpage += GpioState[Gpio_ID_D4];
webpage += "</span>";
webpage += "D4 output";
webpage += "</a>";
webpage += "</li><br>";
webpage += "</ul>";
webpage += "</div>";
/* D4 (GPIO2) related buttons handling */
webpage += "<div class='col-md-4'>";
webpage += "<form action='/' method='POST'>";
webpage += "<button type='button submit' name='D4' value='1' class='btn btn-block btn-success'>";
webpage += "ON";
webpage += "</button>";
webpage += "</form>";
webpage += "</div>";
webpage += "<div class='col-md-4'>";
webpage += "<form action='/' method='POST'>";
webpage += "<button type='button submit' name='D4' value='0' class='btn btn-block btn-danger btn'>";
webpage += "OFF";
webpage += "</button>";
webpage += "</form>";
webpage += "</div>";
webpage += "</div>";
webpage += "<div class='row'><div class='col-md-12'></div></div>";
webpage += "<div class='row'>";
webpage += "<div class='col-md-4'>";
webpage += "<ul class='nav nav-pills'>";
webpage += "<li class='active'>";
webpage += "<a href='#'>";
webpage += "<span class='badge pull-right'>";
webpage += GpioState[Gpio_ID_D5];
webpage += "</span>";
webpage += "D5 output";
webpage += "</a>";
webpage += "</li><br>";
webpage += "</ul>";
webpage += "</div>";
/* D5 (GPIO14) related buttons handling */
webpage += "<div class='col-md-4'>";
webpage += "<form action='/' method='POST'>";
webpage += "<button type='button submit' name='D5' value='1' class='btn btn-block btn-success'>";
webpage += "ON";
webpage += "</button>";
webpage += "</form>";
webpage += "</div>";
webpage += "<div class='col-md-4'>";
webpage += "<form action='/' method='POST'>";
webpage += "<button type='button submit' name='D5' value='0' class='btn btn-block btn-danger btn'>";
webpage += "OFF";
webpage += "</button>";
webpage += "</form>";
webpage += "</div>";
webpage += "</div>";
webpage += "<div class='page-header'> <h1><small>Sensors</small></h1></div>";
webpage += "<table class='table'>";
webpage += "<thead>";
webpage += "<tr><th>#</th><th>Sensor</th><th>Value</th><th>Status </th></tr>";
webpage += "</thead>";
webpage += "<tbody>";
webpage += "<tr class='active'><td>1</td><td>Temperature</td>";
webpage += "<td>";
webpage += sens_val.temperature;
webpage += " ℃";
webpage += "</td>";
webpage += "<td>";
webpage += sensorState.Temp_SensorState;
webpage += "</td>";
webpage += "</tr>";
webpage += "<tr class='success'><td>2</td><td>Humidity</td>";
webpage += "<td>";
webpage += sens_val.humidity;
webpage += " %";
webpage += "</td>";
webpage += "<td>";
webpage += sensorState.Humid_SensorState;
webpage += "</td>";
webpage += "</tr>";
webpage += "<tr class='warning'><td>3</td> <td>Pressure</td>";
webpage += "<td>";
webpage += sens_val.pressure;
webpage += " hPa";
webpage += "</td>";
webpage += "<td>";
webpage += sensorState.Pres_SensorState;
webpage += "</td>";
webpage += "</tr>";
webpage += "<tr class='danger'><td>4</td><td>Light level</td>";
webpage += "<td>";
webpage += sens_val.light;
webpage += " %";
webpage += "</td>";
webpage += "<td>";
webpage += sensorState.Light_SensorState;
webpage += "</td>";
webpage += "</tr>";
webpage += "</tbody>";
webpage += "</table>";
webpage += "</div>";
webpage += "<div class='col-md-6'></div>";
webpage += "</div>";
webpage += "<div class='row'><div class='col-md-6'></div>";
webpage += "<div class='col-md-6'></div>";
webpage += "</div>";
webpage += "<div class='row'>";
webpage += "<div class='col-md-4'>";
webpage += "<address><strong>Embedded Solutions</strong><br>[email protected]</address>";
webpage += "</div>";
webpage += "<div class='col-md-4'></div>";
webpage += "<div class='col-md-4'></div>";
webpage += "</div>";
webpage += "</div>";
webpage += "</body></html>";
return webpage;
}
/*
* Server_GetLoginPage()
* - This functions prints the login website on the server
*/
String Server_Manager::Server_GetLoginPage(String info_msg)
{
String webpage = "";
webpage += "<html charset=UTF-8><head><meta http-equiv='refresh' content='60' name='viewport' content='width=device-width, initial-scale=1'/>";
webpage += "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script><script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>";
webpage += "<link href='https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/darkly/bootstrap.min.css' rel='stylesheet'>";
webpage += "<title>Login ESP8266 IoT - EMBEDDED SOLUTIONS</title></head><body>";
webpage += "<div class='container-fluid'>";
webpage += "<div class='row'>";
webpage += "<div class='col-md-12'>";
webpage += "<div class='page-header'>";
webpage += "<h1>iBeacon! <small>Login page</small></h1>";
webpage += "</div>";
webpage += "<form class='form-horizontal' role='form'>";
webpage += "<div class='form-group'>";
webpage += "<label class='col-sm-2 control-label'>";
webpage += "User";
webpage += "</label>";
/* Username text handling */
webpage += "<div class='col-sm-3'>";
webpage += "<input type='text' class='form-control' name='USERNAME' placeholder='user name'><br>";
webpage += "</div>";
webpage += "</div>";
webpage += "<div class='form-group'>";
webpage += "<label class='col-sm-2 control-label'>";
webpage += "Password";
webpage += "</label>";
/* Password text handling */
webpage += "<div class='col-sm-3'>";
webpage += "<input type='password' class='form-control' name='PASSWORD' placeholder='password'><br>";
webpage += "</div>";
webpage += "</div>";
webpage += "<div class='form-group'>";
webpage += "<div class='col-sm-offset-2 col-sm-10'>";
/* Sign In Button handling */
webpage += "<form action='/login' method='POST'>";
webpage += "<button type='submit button' name='SUBMIT' value='Submit' class='btn btn-default'>";
webpage += "Sign in";
webpage += "</button>";
webpage += "</form>";
webpage += "    " + info_msg;
webpage += "</div>";
webpage += "</div>";
webpage += "</form>";
webpage += "</div>";
webpage += "</div>";
webpage += "</div>";
webpage += "</body></html>";
return webpage;
}
/* EOF */