@@ -16,9 +16,21 @@ Use latest ESP core lib (from Github)
16
16
#define USE_WFM // to use ESPAsyncWiFiManager
17
17
// #define DEL_WFM // delete Wifi credentials stored
18
18
// (use once then comment and flash again), also HTTP /erase-wifi can do the same live
19
-
20
- #define USE_AUTH_STAT // .setAuthentication also for static (editor always requires auth)
21
- // #define USE_AUTH_WS // .setAuthentication also for ws, broken for Safari iOS
19
+
20
+ // AUTH COOKIE uses only the password, Base uses both
21
+ #define http_username " smart"
22
+ #define http_password " switch"
23
+
24
+ // See https://github.com/me-no-dev/ESPAsyncWebServer/pull/684
25
+ #define USE_AUTH_COOKIE
26
+ #define MY_COOKIE_FULL " LLKQ=7;max-age=31536000;"
27
+ #define MY_COOKIE_DEL " LLKQ="
28
+ #define MY_COOKIE " LLKQ=7"
29
+
30
+ #ifndef USE_AUTH_COOKIE
31
+ #define USE_AUTH_STAT // Base Auth for stat, /commands and SPIFFSEditor
32
+ // #define USE_AUTH_WS //Base Auth also for WS, not very supported
33
+ #endif
22
34
23
35
#include < ArduinoOTA.h>
24
36
#ifdef ESP32
@@ -55,7 +67,10 @@ Use latest ESP core lib (from Github)
55
67
56
68
// DHT
57
69
#define DHTTYPE DHT22 // DHT 11 // DHT 22, AM2302, AM2321 // DHT 21, AM2301
58
- #define DHTPIN 4 // D2
70
+ #define DHTPIN 4 // D2
71
+
72
+ #define DHT_T_CORR -0.5 // Temperature offset compensation of the sensor (can be -)
73
+ #define DHT_H_CORR 1.5 // Humidity offset compensation of the sensor
59
74
60
75
DHT dht (DHTPIN, DHTTYPE);
61
76
@@ -80,9 +95,7 @@ AsyncWebSocket ws("/ws");
80
95
const char * ssid = " MYROUTERSSD" ;
81
96
const char * password = " MYROUTERPASSWD" ;
82
97
#endif
83
- const char * hostName = " smartsw" ;
84
- const char * http_username = " smart" ;
85
- const char * http_password = " switch" ;
98
+ const char * hostName = " smartsw32" ;
86
99
87
100
// RTC
88
101
static timeval tv;
@@ -91,6 +104,15 @@ static time_t now;
91
104
// HW I/O
92
105
const int btnPin = 0 ; // D3
93
106
const int ledPin = 2 ; // D4
107
+
108
+ #ifdef ESP32
109
+ #define LED_ON 0x1
110
+ #define LED_OFF 0x0
111
+ #elif defined(ESP8266)
112
+ #define LED_ON 0x0
113
+ #define LED_OFF 0x1
114
+ #endif
115
+
94
116
int btnState = HIGH;
95
117
96
118
// Globals
@@ -101,7 +123,7 @@ float t = 0;
101
123
float h = 0 ;
102
124
bool udht = false ;
103
125
bool heat_enabled_prev = false ;
104
- int ledState;
126
+ int ledState = LED_OFF ;
105
127
106
128
struct EE_bl {
107
129
byte memid; // here goes the EEMARK stamp
@@ -178,30 +200,32 @@ void showTime()
178
200
}
179
201
180
202
if (heat_enabled_prev) { // smart control (delayed one cycle)
181
- if (((t - HYST) < ee.tempe )&&(ledState == HIGH )) { // OFF->ON once
182
- ledState = LOW ;
203
+ if (((t - HYST) < ee.tempe )&&(ledState == LED_OFF )) { // OFF->ON once
204
+ ledState = LED_ON ;
183
205
digitalWrite (ledPin, ledState); // apply change
184
206
ws.textAll (" led,ledon" );
185
207
}
186
- if ((((t + HYST) > ee.tempe )&&(ledState == LOW ))||(!heat_enabled)) { // ON->OFF once, also turn off at end of period.
187
- ledState = HIGH ;
208
+ if ((((t + HYST) > ee.tempe )&&(ledState == LED_ON ))||(!heat_enabled)) { // ON->OFF once, also turn off at end of period.
209
+ ledState = LED_OFF ;
188
210
digitalWrite (ledPin, ledState); // apply change
189
211
ws.textAll (" led,ledoff" );
190
212
}
191
- Serial.printf (ledState ? " LED OFF" : " LED ON" );
213
+
214
+ Serial.printf (ledState == LED_ON ? " LED ON" : " LED OFF" );
192
215
Serial.print (F (" , Smart enabled\n " ));
193
216
}
194
217
heat_enabled_prev = heat_enabled; // update
195
218
}
196
219
197
220
void updateDHT (){
198
- h = dht.readHumidity ();
199
- t = dht.readTemperature (); // Celsius or dht.readTemperature(true) for Fahrenheit
200
- if (isnan (h ) || isnan (t )) {
221
+ float h1 = dht.readHumidity ();
222
+ float t1 = dht.readTemperature (); // Celsius or dht.readTemperature(true) for Fahrenheit
223
+ if (isnan (h1 ) || isnan (t1 )) {
201
224
Serial.print (F (" Failed to read from DHT sensor!" ));
202
- h = 0 ; // debug w/o sensor
203
- t = 0 ;
204
- }
225
+ } else {
226
+ h = h1 + DHT_H_CORR;
227
+ t = t1 + DHT_T_CORR;
228
+ }
205
229
}
206
230
207
231
void analogSample ()
@@ -216,7 +240,7 @@ void checkPhysicalButton()
216
240
if (btnState != LOW) { // btnState is used to avoid sequential toggles
217
241
ledState = !ledState;
218
242
digitalWrite (ledPin, ledState);
219
- if (ledState) ws.textAll (" led,ledoff" );
243
+ if (ledState == LED_OFF ) ws.textAll (" led,ledoff" );
220
244
else ws.textAll (" led,ledon" );
221
245
}
222
246
btnState = LOW;
@@ -241,6 +265,16 @@ void mytimer() {
241
265
}
242
266
}
243
267
268
+ #ifdef USE_AUTH_COOKIE
269
+ bool myHandshake (AsyncWebServerRequest *request){ // false will 401
270
+ if (request->hasHeader (" Cookie" )){
271
+ String cookie = request->header (" Cookie" );
272
+ if (cookie.indexOf (MY_COOKIE) != -1 ) return true ;
273
+ else return false ;
274
+ } else return false ;
275
+ }
276
+ #endif
277
+
244
278
// server
245
279
void onWsEvent (AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
246
280
if (type == WS_EVT_CONNECT){
@@ -252,7 +286,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
252
286
Serial.printf (" [%u] Connected from %d.%d.%d.%d\n " , client->id (), ip[0 ], ip[1 ], ip[2 ], ip[3 ]);
253
287
showTime ();
254
288
analogSample ();
255
- if (ledState) ws.textAll (" led,ledoff" );
289
+ if (ledState == LED_OFF ) ws.textAll (" led,ledoff" );
256
290
else ws.textAll (" led,ledon" );
257
291
258
292
ws.printfAll (" Now,Setting,%02d:%02d,%02d:%02d,%+2.1f" , ee.hstart , ee.mstart , ee.hstop , ee.mstop , ee.tempe );
@@ -279,11 +313,11 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
279
313
}
280
314
if (data[0 ] == ' L' ) { // LED
281
315
if (data[1 ] == ' 1' ) {
282
- ledState = LOW ;
316
+ ledState = LED_ON ;
283
317
ws.textAll (" led,ledon" ); // for others
284
318
}
285
319
else if (data[1 ] == ' 0' ) {
286
- ledState = HIGH ;
320
+ ledState = LED_OFF ;
287
321
ws.textAll (" led,ledoff" );
288
322
}
289
323
digitalWrite (ledPin, ledState); // apply change
@@ -444,31 +478,74 @@ void setup(){
444
478
#ifdef USE_AUTH_WS
445
479
ws.setAuthentication (http_username,http_password);
446
480
#endif
481
+
482
+ #ifdef USE_AUTH_COOKIE
483
+ ws.handleHandshake (myHandshake);
484
+ #endif
485
+
447
486
ws.onEvent (onWsEvent);
448
487
server.addHandler (&ws);
449
488
450
489
#ifdef ESP32
490
+ #ifdef USE_AUTH_STAT
451
491
server.addHandler (new SPIFFSEditor (SPIFFS, http_username,http_password));
492
+ #elif defined(USE_AUTH_COOKIE)
493
+ server.addHandler (new SPIFFSEditor (SPIFFS)).setFilter (myHandshake);
494
+ #endif
452
495
#elif defined(ESP8266)
496
+ #ifdef USE_AUTH_STAT
453
497
server.addHandler (new SPIFFSEditor (http_username,http_password));
498
+ #elif defined(USE_AUTH_COOKIE)
499
+ server.addHandler (new SPIFFSEditor ()).setFilter (myHandshake);
500
+ #endif
454
501
#endif
455
-
502
+
503
+ #ifdef USE_AUTH_COOKIE
504
+ server.on (" /lg2n" , HTTP_POST, [](AsyncWebServerRequest *request){ // cookie test
505
+ if ((request->hasParam (" pa2w" ,true ) && (String (request->getParam (" pa2w" ,true )->value ().c_str ()) == String (http_password)))||(request->hasParam (" lg0f" ,true ))){
506
+ AsyncWebServerResponse *response = request->beginResponse (301 );
507
+ response->addHeader (" Location" , " /" );
508
+ response->addHeader (" Cache-Control" , " no-cache" );
509
+ if (request->hasParam (" lg0f" ,true )) response->addHeader (" Set-Cookie" , MY_COOKIE_DEL);
510
+ else response->addHeader (" Set-Cookie" , MY_COOKIE_FULL);
511
+ request->send (response);
512
+ } else request->send (200 , " text/plain" ," Wrong Password!" );
513
+ });
514
+ #endif
515
+
516
+ // below paths need individual auth ////////////////////////////////////////////////
517
+
456
518
server.on (" /free-ram" , HTTP_GET, [](AsyncWebServerRequest *request){ // direct request->answer
519
+ #ifdef USE_AUTH_STAT
520
+ if (!request->authenticate (http_username, http_password)) return request->requestAuthentication ();
521
+ #endif
457
522
request->send (200 , " text/plain" , String (ESP.getFreeHeap ()));
523
+ #ifdef USE_AUTH COOKIE
524
+ }).setFilter (myHandshake);
525
+ #else
458
526
});
527
+ #endif
459
528
460
-
461
- server.on (" /get-time" , HTTP_GET, [](AsyncWebServerRequest *request){
529
+ server.on (" /get-time" , HTTP_GET, [](AsyncWebServerRequest *request){
530
+ #ifdef USE_AUTH_STAT
531
+ if (!request->authenticate (http_username, http_password)) return request->requestAuthentication ();
532
+ #endif
462
533
if (request->hasParam (" btime" )){
463
534
time_t rtc = (request->getParam (" btime" )->value ()).toInt ();
464
535
timeval tv = { rtc, 0 };
465
536
settimeofday (&tv, nullptr );
466
537
}
467
538
request->send (200 , " text/plain" ," Got browser time ..." );
539
+ #ifdef USE_AUTH COOKIE
540
+ }).setFilter (myHandshake);
541
+ #else
468
542
});
469
-
543
+ # endif
470
544
471
545
server.on (" /hw-reset" , HTTP_GET, [](AsyncWebServerRequest *request){
546
+ #ifdef USE_AUTH_STAT
547
+ if (!request->authenticate (http_username, http_password)) return request->requestAuthentication ();
548
+ #endif
472
549
request->onDisconnect ([]() {
473
550
#ifdef ESP32
474
551
ESP.restart ();
@@ -477,9 +554,16 @@ void setup(){
477
554
#endif
478
555
});
479
556
request->send (200 , " text/plain" ," Restarting ..." );
557
+ #ifdef USE_AUTH COOKIE
558
+ }).setFilter (myHandshake);
559
+ #else
480
560
});
561
+ #endif
481
562
482
563
server.on (" /erase-wifi" , HTTP_GET, [](AsyncWebServerRequest *request){
564
+ #ifdef USE_AUTH_STAT
565
+ if (!request->authenticate (http_username, http_password)) return request->requestAuthentication ();
566
+ #endif
483
567
request->onDisconnect ([]() {
484
568
WiFi.disconnect (true );
485
569
#ifdef ESP32
@@ -489,12 +573,23 @@ void setup(){
489
573
#endif
490
574
});
491
575
request->send (200 , " text/plain" ," Erasing WiFi data ..." );
576
+ #ifdef USE_AUTH COOKIE
577
+ }).setFilter (myHandshake);
578
+ #else
492
579
});
580
+ #endif
581
+
582
+ // above paths need individual auth ////////////////////////////////////////////////
493
583
494
- #ifdef USE_AUTH_STAT
495
- server.serveStatic (" /" , SPIFFS, " /" ).setDefaultFile (" index.htm" ).setAuthentication (http_username,http_password);
584
+ #ifdef USE_AUTH_COOKIE
585
+ server.serveStatic (" /" , SPIFFS, " /" ).setDefaultFile (" index.htm" ).setFilter (myHandshake);
586
+ server.serveStatic (" /" , SPIFFS, " /login/" ).setDefaultFile (" index.htm" ).setFilter (!myHandshake);
496
587
#else
497
- server.serveStatic (" /" , SPIFFS, " /" ).setDefaultFile (" index.htm" );
588
+ #ifdef USE_AUTH_STAT
589
+ server.serveStatic (" /" , SPIFFS, " /" ).setDefaultFile (" index.htm" ).setAuthentication (http_username,http_password);
590
+ #else
591
+ server.serveStatic (" /" , SPIFFS, " /" ).setDefaultFile (" index.htm" );
592
+ #endif
498
593
#endif
499
594
500
595
server.onNotFound ([](AsyncWebServerRequest *request){ // nothing known
0 commit comments