-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDigiTempServer.h
566 lines (484 loc) · 16.7 KB
/
DigiTempServer.h
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#ifndef DIGITEMPSERVER_H
#define DIGITEMPSERVER_H
#define server_copyrite " © Jan 2021 VictorWheeler [email protected] use, modify and distribute without restrictions"
#define server_compiledate __DATE__
#include "DigiTempESP.h"
#include <ESP8266mDNS.h> // mDNS Client or Bonjour Client library for the esp8266.
// For lookup using hostname.local instead of IP address
const int max_connection = 8;
const int channel = 4;
// OhOh can't delay or print in a event get out of event using the newClient flag.
boolean newClient = true; // First run We are new!
typedef struct {
IPAddress ipAddress;
boolean isAlive; //validIP;
String myHostName;
Th_temp Th_t;
unsigned long last_check; // Last time sensor was accessed
} clist;
clist clients[max_connection + 2]; // include space for AP if DHT is enabled
// WiFiEventSoftAPModeStationConnected Manage incoming device connection on ESP access point
void onNewStation(WiFiEventSoftAPModeStationConnected sta_info) {
if(DEBUG){
Serial.print("New Station : ");
Serial.print(sta_info.aid);
Serial.print(" ");
}
newClient = true;
}
//WiFiEventStationModeDisconnected
void onRemoveStation(WiFiEventSoftAPModeStationDisconnected sta_info) {
if(DEBUG){
Serial.println("Station Removed: ");
Serial.print(sta_info.aid);
Serial.print(" ");
}
newClient = true;
}
/*
*
* Setup wifi as a soft Access Point
*
*/
void setupAP() {
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.println("setupAP");
Serial.print("\n\rDigiTemp AP max "); Serial.print(max_connection); Serial.println(" WiFi connections");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP(ssid, password, channel, false, max_connection) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
// WiFi.softAP(ssid, password, channel, false, max_connection);
// IPAddress myIP = WiFi.softAPIP();
// Serial.print("AP IP address: "); Serial.println(myIP);
// Event subscription
static WiFiEventHandler e1 = WiFi.onSoftAPModeStationConnected(onNewStation);
static WiFiEventHandler e2 = WiFi.onSoftAPModeStationDisconnected(onRemoveStation);
}
/*
*
* Find the substring "key" in the string s and return the value associated with it.
* value is the chars following the white space after the key
*
*/
float parser(const String* s, const char* key) {
float retVal = NAN;
// Search for key
if (s->indexOf(key) >= 0) { // Found key
unsigned int ptr = s->indexOf(key);
//compute end of key
ptr += strlen(key);
// remove white space
while (isspace(s->charAt(ptr)) && ptr < s->length()) {
ptr++;
} // ptr now at start of value or EOLN
unsigned int stptr = ptr; // start of the value
// find end of value
while ((isdigit(s->charAt(ptr)) || s->charAt(ptr) == '.') && ptr < s->length()) {
ptr++;
}
if(ptr > stptr){ // Not a number
retVal = s->substring(stptr, ptr).toFloat();
}
}
return retVal;
}
boolean checkLocalStation(clist* addss) {
Serial.print(F(" checkLocalStation() "));
// addss->last_check = millis();
addss->Th_t.h = Th_t.h;
addss->Th_t.f = Th_t.f;
addss->Th_t.c = Th_t.c;
addss->Th_t.tmaxf = Th_t.tmaxf;
addss->Th_t.tminf = Th_t.tminf;
addss->myHostName = hostName;
return true;
}
/**
* checkStation()
* verify active stations client array still exists and fill structure clist with data
*
*
*/
//boolean checkStation(IPAddress stationIP){
boolean checkStation(clist* addss) {
//boolean checkStation(int index) {
// clist* addss = &clients[index];
//addss->isAlive = true;
addss->last_check = millis();
Serial.print(" Hello from checkStation ");
if (addss->ipAddress == WiFi.softAPIP()) {
return checkLocalStation(addss); // local host url not working
}
String url = "/getData";
String host = addss->ipAddress.toString();
Serial.print("Connecting to ");
Serial.print(host + url);
WiFiClient client;
//Serial.print("looking for 404 "); //Serial.println(client.connect(host, 80));
if (!client.connect(host, 80)) {
Serial.print(" Connection failed "); Serial.println(client.status());
addss->isAlive = false;
return false;
} else {
Serial.println();
addss->isAlive = true;
}
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
delay(10); yield();
int ptr;
int eoln;
float temp;
String temps;
Serial.println("Trying to read " + host + url);
while (client.connected() || client.available()) {
if (client.available()) {
String line = client.readStringUntil('\n');
if(line.indexOf("404") > 0){
Serial.println(F(" ************************** 4 0 4 ********************************* "));
addss->isAlive = false;
client.stop();
return false;
} else {
// if (line.indexOf("404 Not found") == -1) {
// Humidity
ptr = line.indexOf("Humidity:"); // Quick check for now need to modify parser to return boolean and something else for the retVal
if (ptr >= 0) {
temp = parser(&line, "Humidity:");
addss->Th_t.h = temp;
}
// Temperature (both)
ptr = line.indexOf("Temperature:");
if (ptr >= 0) {
temp = parser(&line, "Temperature:");
ptr = line.indexOf("F");
if (ptr >= 0)
addss->Th_t.f = temp;
else
addss->Th_t.c = temp;
}
ptr = line.indexOf("Temperature_Max:");
if (ptr >= 0) {
temp = parser(&line, "Temperature_Max:");
addss->Th_t.tmaxf = temp;
}
ptr = line.indexOf("Temperature_Min:");
if (ptr >= 0) {
temp = parser(&line, "Temperature_Min:");
addss->Th_t.tminf = temp;
}
// Hostname
ptr = line.indexOf("Hostname:");
if (ptr >= 0) {
eoln = line.length();
if (eoln > ptr) { // JIC WTGBIN
temps = line.substring((ptr + 10), eoln);
// Serial.println("Found for Hostname: " + temps);
addss->myHostName = temps;
}
}
}
// else {
// Serial.println( " 404 found bypass");
// addss->isAlive = false;
// client.stop();
// return false;
// }
} // else { Serial.print('.\r');} //"client.available = false");}
yield();
} // while connected
client.stop();
return true;
}
/*
*
* load client array with IP and isAlive boolean.
* reset = false do not change client's isAlive status
*
*/
void Load_Client_List(boolean reset) {
Serial.printf("New Station Number of connections : %i\n", WiFi.softAPgetStationNum());
int ipidx = 0;
// Special case for this AP enabled with DHT always stored in location 0
clients[ipidx].ipAddress = WiFi.softAPIP();
if(reset){ clients[ipidx].isAlive = true;}
Serial.print(ipidx); Serial.print(":\t"); Serial.println(clients[ipidx].ipAddress.toString());
ipidx++; // ready for remote clients
// Add the remote clients
struct station_info *station_list = wifi_softap_get_station_info();
while (station_list != NULL) {
clients[ipidx].ipAddress = IPAddress((&station_list->ip)->addr);
Serial.print(ipidx); Serial.print(":\t"); Serial.println(clients[ipidx].ipAddress.toString());
if(newClient){ clients[ipidx].isAlive = true;} // Default to active until get_Data() is verified
//if(reset){ Serial.print(ipidx); Serial.println(" = TRUE");}
station_list = STAILQ_NEXT(station_list, next);
ipidx++; // ipidx++; is not working use long-hand
}
wifi_softap_free_station_info();
}
/*
*
* Update remote station values
*
*/
boolean read_Client(int Station_Number){
boolean retVal = false;
Load_Client_List(true); // Some reason the clist is not loaded on new client so update clist each call
// TODO remove after debug
Serial.print(F("Read Client ")); Serial.print(Station_Number);
if (clients[Station_Number].isAlive) {
retVal = checkStation(&clients[Station_Number]);
// TODO remove after debug
Serial.println(F(" is Active "));
} else {
// TODO remove after debug
Serial.println(F(" is Not Alive "));
retVal = false;
}
// TODO remove after debug
for(int s=0; s <= max_connection; s++){
Serial.print(s);
Serial.print(F("\tHostname: ")); Serial.print(clients[s].myHostName);
if(clients[s].isAlive) Serial.print(F("\tAlive")); else Serial.print(F("\tDead "));
Serial.print(F("\tIP Address: ")); Serial.print(clients[s].ipAddress);
Serial.print(F("\tLast read: ")); Serial.println(clients[s].last_check);
}
return retVal;
}
/**
*
*
*
*
*/
String SendHTML() {
char tempFs[6];
char humis[5];
char connections[3];
sprintf(connections, "%2d", WiFi.softAPgetStationNum());
//String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr += "<head>\n";
ptr += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>DigiTemp AP Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;} h6 {color: #DFF2FB;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "<script type=\"text/javascript\">setTimeout(\"location.reload()\", 45000);</script>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1>ESP8266 DigiTemp Server <a href=toggle> {{S}} </a> </h1>\n";
ptr += "<h3>Using Access Point(AP) Mode ";
ptr += connections;
ptr += " clients</h3>\n";
// Loop starts at one clients[0] is special case for local DHT
for (int ii = 0; ii <= WiFi.softAPgetStationNum(); ii++) { //max_connection
if (clients[ii].isAlive) {
ptr += "<p>"; // if(0 != ii) ptr += "Connection: ";
ptr += "<a href=//";
ptr += clients[ii].ipAddress.toString();
ptr += "/getData";
ptr += ">";
if(clients[ii].myHostName.length() < 1) {
ptr += clients[ii].ipAddress.toString();
} else {
ptr += clients[ii].myHostName;
}
ptr += "</a>";
ptr += "<br>";
//if (clients[ii].isAlive) {
ptr += " {{HighLow}} ";
// if(clients[ii].ipAddress == WiFi.softAPIP()){
// checkLocalStation(&clients[ii]);
// } else {
// checkStation(&clients[ii]);
// }
// if (checkStation(&clients[ii])) {
// if(clients[ii].isAlive) {
if (isnan(clients[ii].Th_t.f)) {
Serial.print(clients[ii].Th_t.f); Serial.println(" tempF is not a number");
} else {
if(scale){
Serial.print(dht.convertFtoC(clients[ii].Th_t.f));
} else {
Serial.print(clients[ii].Th_t.f);
}
}
if (isnan(clients[ii].Th_t.h)) {
Serial.println("humi is not a number");
}
if(scale){
sprintf(tempFs, "%02.2f", dht.convertFtoC(clients[ii].Th_t.f));
} else {
sprintf(tempFs, "%02.2f", clients[ii].Th_t.f);
}
sprintf(humis, "%02.1f", clients[ii].Th_t.h);
ptr += "<a class=\"button button-off\" href=\"/stations\"> ";
ptr += tempFs;
if(scale){
ptr += "C ";
} else {
ptr += "F ";
}
ptr += humis;
ptr += "%</a>";
char highlow[40];
if(scale){
sprintf(highlow, "High: %02.2fC\t\tLow: %02.2fC", dht.convertFtoC(clients[ii].Th_t.tmaxf), dht.convertFtoC(clients[ii].Th_t.tminf));
ptr.replace("{{S}}", "C");
} else {
sprintf(highlow, "High: %02.2fF\t\tLow: %02.2fF", clients[ii].Th_t.tmaxf, clients[ii].Th_t.tminf);
ptr.replace("{{S}}", "F");
}
ptr.replace("{{HighLow}}", String(highlow));
}
// else {
// ptr += "<a class=\"button button-off\" href=\"/stations\">Off Line</a>";
// clients[ii].isAlive = false;
// }
} // for WiFi.softAPgetStationNum
ptr += "<h6>"; ptr += copyrite; ptr += " "; ptr += compiledate; ptr += "</h6>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
/* Just a little test message. Go to http://192.168.4.1 in a web browser
connected to this access point to see it.
*/
void rootPage(){
//void handleRoot() {
if(DEBUG) Serial.println("Hello from handleRoot");
Server.send(200, "text/html", SendHTML());
//Server.client().stop();
Server.client().flush();
}
String pleaseWait() {
//newClient = true;
char connections[3];
sprintf(connections, "%2d", WiFi.softAPgetStationNum());
String ptr = "<!DOCTYPE html> <html>\n";
ptr += "<head>\n";
ptr += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<script type=\"text/javascript\">setTimeout(\"location.replace(location.origin)\", 5000);</script>\n";
ptr += "<title>DigiTemp AP Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1>Please Wait</h1>\n";
ptr += "<h3>Loading ";
ptr += connections;
ptr += " clients</h3>\n";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
// Currently for Debug easy way to Reset client list from web page
void updateStations() {
if(DEBUG) Serial.println("Hello from updateStations");
Server.send(200, "text/html", pleaseWait());
Server.client().flush();
newClient = true; //clientList();
//handleRoot();
}
/*
*
*
*
*
*/
//Server.on("/toggle", toggleCF);
void toggleCF() { // toggle Temperature scale 'C' of 'F'
if (DEBUG) {
Serial.print("Hello from toggleCF Switching ");
if (scale) {
Serial.println("Celsius to Fahrenheit");
} else {
Serial.println("Fahrenheit to Celsius");
}
}
scale = !scale;
String content;
content += "<!DOCTYPE html> \n";
content += "<html>\n";
content += "<head>\n";
content +=
"<script> location.replace(\"http://{{DiGiTempServerIP}}/\") </script>\n";
content += "</head>\n";
content += "</html>\n ";
content.replace("{{DiGiTempServerIP}}", WiFi.softAPIP().toString());
Server.send(301, "text/html", content);
Server.client().flush();
}
void setupServer() {
Server.on("/toggle", toggleCF);
Server.on("/stations", updateStations);
Serial.println("HTTP server started");
}
/*
Setup mDNS responder
Allows this device to be found on local network by using {nyhostname}.local instead of IP address
using an mDNS Client or Bonjour
*/
void setupmDNS() {
// Set up mDNS responder:
if (!MDNS.begin(hostName)) {
Serial.println(F("Error setting up MDNS responder!"));
}
Serial.print(F("mDNS responder started connect using http://"));
Serial.print(hostName);
Serial.println(F(".local"));
}
void my_setup() {
hostName = MY_HOSTNAME;
Serial.print(F("Running as Host "));
Serial.println(hostName);
setupAP();
setupServer();
setupmDNS();
}
/*
* Main loop
*
*/
int client_num = 0;
void my_loop() {
MDNS.update(); // required for the mDNS lookup this is the key to making mDNS work
// Undocumented I think update() is what responds to DNS requests
// and needs to be called frequently
if(newClient){
if(DEBUG) Serial.println("There is a new client");
Load_Client_List(false);
newClient = false;
}
if(timeElapsed(minuteInterval/4)){
if(client_num > WiFi.softAPgetStationNum()) client_num = 0;
read_Client(client_num++);
}
}
void do_serial(char r){
Serial.print(r);
if(r == '?') Load_Client_List(true);
Serial.print(" Num connections: "); Serial.println(WiFi.softAPgetStationNum());
Serial.print(" ");
Serial.println(WiFi.localIP().toString());
Serial.println(WiFi.softAPIP().toString());
for(int s=0; s <= max_connection; s++){
Serial.print(s);
Serial.print(F("\tHostname: ")); Serial.print(clients[s].myHostName);
Serial.print(F("\tIP Address: ")); Serial.println(clients[s].ipAddress);
}
}
#endif // DIGITEMPSERVER_H