@@ -166,7 +166,8 @@ float t = 0;
166
166
float h = 0 ;
167
167
bool udht = false ;
168
168
bool heat_enabled_prev = false ;
169
- int ledState = LED_OFF;
169
+ bool ledState = LED_OFF;
170
+ bool ledOut = LED_OFF;
170
171
171
172
struct EE_bl {
172
173
byte memid; // here goes the EEMARK stamp
@@ -180,15 +181,15 @@ struct EE_bl {
180
181
EE_bl ee = {0 ,0 ,0 ,0 ,0 ,0.1 }; // populate as initial
181
182
182
183
// SUBS
183
- void writeEE () {
184
+ void writeEE (){
184
185
ee.memid = EEMARK;
185
186
// EEPROM.put(EESC, sched); // only separately when needed with commit()
186
187
// EEPROM.put(EECH, memch); // not need to store and retrieve memch
187
188
EEPROM.put (EEBEGIN + memch*sizeof (ee), ee);
188
189
EEPROM.commit (); // needed for ESP8266?
189
190
}
190
191
191
- void readEE () {
192
+ void readEE (){
192
193
byte ChkEE;
193
194
if (memch > MEMMAX) memch = 0 ;
194
195
EEPROM.get (EEBEGIN + memch*sizeof (ee), ChkEE);
@@ -199,6 +200,15 @@ void readEE() {
199
200
}
200
201
}
201
202
203
+ void doOut (){
204
+ if (ledOut != ledState){ // only if changed
205
+ digitalWrite (ledPin, ledState); // consolidated here
206
+ ledOut = ledState; // update
207
+ if (ledState == LED_OFF) ws.textAll (" led,ledoff" );
208
+ else ws.textAll (" led,ledon" );
209
+ }
210
+ }
211
+
202
212
void showTime ()
203
213
{
204
214
byte tmpch = 0 ;
@@ -208,21 +218,21 @@ void showTime()
208
218
now = time (nullptr );
209
219
const tm* tm = localtime (&now);
210
220
ws.printfAll (" Now,Clock,%02d:%02d,%d" , tm->tm_hour , tm->tm_min , tm->tm_wday );
211
- if ((2 ==tm->tm_hour )&&(2 ==tm->tm_min )) {
221
+ if ((2 ==tm->tm_hour )&&(2 ==tm->tm_min )){
212
222
configTzTime (MYTZ, " pool.ntp.org" );
213
223
Serial.print (F (" Sync Clock at 02:02\n " ));
214
224
}
215
225
Serial.printf (" RTC: %02d:%02d\n " , tm->tm_hour , tm->tm_min );
216
226
217
- if (sched == 0 ) { // automatic
227
+ if (sched == 0 ){ // automatic
218
228
if ((tm->tm_wday > 0 )&&(tm->tm_wday < 6 )) tmpch = 0 ; // Mon - Fri
219
229
else if (tm->tm_wday == 6 ) tmpch = 1 ; // Sat
220
230
else if (tm->tm_wday == 0 ) tmpch = 2 ; // Sun
221
231
} else { // manual
222
232
tmpch = sched - 1 ; // and stays
223
233
}
224
234
225
- if (tmpch != memch) { // update if different
235
+ if (tmpch != memch){ // update if different
226
236
memch = tmpch;
227
237
readEE ();
228
238
ws.printfAll (" Now,Setting,%02d:%02d,%02d:%02d,%+2.1f" , ee.hstart , ee.mstart , ee.hstop , ee.mstop , ee.tempe );
@@ -237,20 +247,18 @@ void showTime()
237
247
else { // enable smart if different
238
248
239
249
if (((bmi < emi)&&(bmi <= xmi)&&(xmi < emi))||
240
- ((emi < bmi)&&((bmi <= xmi)||(xmi < emi)))) {
250
+ ((emi < bmi)&&((bmi <= xmi)||(xmi < emi)))){
241
251
heat_enabled = true ;
242
252
} else heat_enabled = false ;
243
253
}
244
254
245
- if (heat_enabled_prev) { // smart control (delayed one cycle)
246
- if (((t + HYST) < ee.tempe )&&(ledState == LED_OFF)) { // OFF->ON once
255
+ if (heat_enabled_prev){ // smart control (delayed one cycle)
256
+ if (((t + HYST) < ee.tempe )&&(ledState == LED_OFF)){ // OFF->ON once
247
257
ledState = LED_ON;
248
- digitalWrite (ledPin, ledState); // apply change
249
258
ws.textAll (" led,ledon" );
250
259
}
251
- if ((((t - HYST) > ee.tempe )&&(ledState == LED_ON))||(!heat_enabled)) { // ON->OFF once, also turn off at end of period.
260
+ if ((((t - HYST) > ee.tempe )&&(ledState == LED_ON))||(!heat_enabled)){ // ON->OFF once, also turn off at end of period.
252
261
ledState = LED_OFF;
253
- digitalWrite (ledPin, ledState); // apply change
254
262
ws.textAll (" led,ledoff" );
255
263
}
256
264
@@ -263,7 +271,7 @@ void showTime()
263
271
void updateDHT (){
264
272
float h1 = dht.readHumidity ();
265
273
float t1 = dht.readTemperature (); // Celsius or dht.readTemperature(true) for Fahrenheit
266
- if (isnan (h1) || isnan (t1)) {
274
+ if (isnan (h1) || isnan (t1)){
267
275
Serial.println (F (" Failed to read from DHT sensor!" ));
268
276
} else {
269
277
h = h1 + DHT_H_CORR;
@@ -279,11 +287,10 @@ void analogSample()
279
287
280
288
void checkPhysicalButton ()
281
289
{
282
- if (digitalRead (btnPin) == LOW) {
283
- if (btnState != LOW) { // btnState is used to avoid sequential toggles
290
+ if (digitalRead (btnPin) == LOW){
291
+ if (btnState != LOW){ // btnState is used to avoid sequential toggles
284
292
ledState = !ledState;
285
- digitalWrite (ledPin, ledState);
286
- if (ledState == LED_OFF) {
293
+ if (ledState == LED_OFF){
287
294
ws.textAll (" led,ledoff" );
288
295
Serial.println (F (" LED-OFF" ));
289
296
} else {
@@ -297,17 +304,17 @@ void checkPhysicalButton()
297
304
}
298
305
}
299
306
300
- void mytimer () {
307
+ void mytimer (){
301
308
++count; // 200ms increments
302
309
checkPhysicalButton ();
303
- if ((count % 25 ) == 1 ) { // update temp every 5 seconds
310
+ if ((count % 25 ) == 1 ){ // update temp every 5 seconds
304
311
analogSample ();
305
312
udht = true ;
306
313
}
307
- if ((count % 50 ) == 0 ) { // update temp every 10 seconds
314
+ if ((count % 50 ) == 0 ){ // update temp every 10 seconds
308
315
ws.cleanupClients ();
309
316
}
310
- if (count >= 150 ) { // cycle every 30 sec
317
+ if (count >= 150 ){ // cycle every 30 sec
311
318
showTime ();
312
319
count = 0 ;
313
320
}
@@ -385,22 +392,21 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
385
392
Serial.printf (" ws[%s][%u] %s-message[%llu]: " , server->url (), client->id (), (info->opcode == WS_TEXT)?" text" :" binary" , info->len );
386
393
387
394
if (info->opcode == WS_TEXT){
388
- for (size_t i=0 ; i < info->len ; i++) { // debug
395
+ for (size_t i=0 ; i < info->len ; i++){ // debug
389
396
msg += (char ) data[i];
390
397
}
391
- if (data[0 ] == ' L' ) { // LED
392
- if (data[1 ] == ' 1' ) {
398
+ if (data[0 ] == ' L' ){ // LED
399
+ if (data[1 ] == ' 1' ){
393
400
ledState = LED_ON;
394
401
ws.textAll (" led,ledon" ); // for others
395
402
}
396
- else if (data[1 ] == ' 0' ) {
403
+ else if (data[1 ] == ' 0' ){
397
404
ledState = LED_OFF;
398
405
ws.textAll (" led,ledoff" );
399
406
}
400
- digitalWrite (ledPin, ledState); // apply change
401
407
402
- } else if (data[0 ] == ' T' ) { // timeset
403
- if (len > 11 ) {
408
+ } else if (data[0 ] == ' T' ){ // timeset
409
+ if (len > 11 ){
404
410
data[3 ] = data[6 ] = data[9 ] = data[12 ] = 0 ; // cut strings
405
411
ee.hstart = (uint8_t ) atoi ((const char *) &data[1 ]);
406
412
ee.mstart = (uint8_t ) atoi ((const char *) &data[4 ]);
@@ -411,8 +417,8 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
411
417
memch = 255 ; // to force showTime()to send Setting
412
418
showTime ();
413
419
}
414
- } else if (data[0 ] == ' W' ) { // temperatureset
415
- if (len > 3 ) {
420
+ } else if (data[0 ] == ' W' ){ // temperatureset
421
+ if (len > 3 ){
416
422
if (ee.tempe != (float ) atof ((const char *) &data[1 ])){
417
423
ee.tempe = (float ) atof ((const char *) &data[1 ]);
418
424
Serial.printf (" [%u] Temp set %+2.1f\n " , client->id (), ee.tempe );
@@ -421,7 +427,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
421
427
showTime ();
422
428
}
423
429
}
424
- } else if ((data[0 ] == ' Z' )&&(len > 2 )) { // sched
430
+ } else if ((data[0 ] == ' Z' )&&(len > 2 )){ // sched
425
431
data[2 ] = 0 ;
426
432
if (sched != (uint8_t ) atoi ((const char *) &data[1 ])){
427
433
sched = (uint8_t ) atoi ((const char *) &data[1 ]);
@@ -434,7 +440,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
434
440
435
441
} else {
436
442
char buff[3 ];
437
- for (size_t i=0 ; i < info->len ; i++) {
443
+ for (size_t i=0 ; i < info->len ; i++){
438
444
sprintf (buff, " %02x " , (uint8_t ) data[i]);
439
445
msg += buff ;
440
446
}
@@ -457,12 +463,12 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
457
463
Serial.printf (" ws[%s][%u] frame[%u] %s[%llu - %llu]: " , server->url (), client->id (), info->num , (info->message_opcode == WS_TEXT)?" text" :" binary" , info->index , info->index + len);
458
464
459
465
if (info->opcode == WS_TEXT){
460
- for (size_t i=0 ; i < len; i++) {
466
+ for (size_t i=0 ; i < len; i++){
461
467
msg += (char ) data[i];
462
468
}
463
469
} else {
464
470
char buff[3 ];
465
- for (size_t i=0 ; i < len; i++) {
471
+ for (size_t i=0 ; i < len; i++){
466
472
sprintf (buff, " %02x " , (uint8_t ) data[i]);
467
473
msg += buff ;
468
474
}
@@ -488,7 +494,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
488
494
489
495
void setup (){
490
496
Serial.begin (115200 );
491
- Serial.setDebugOutput (true );
497
+ // Serial.setDebugOutput(true);
492
498
493
499
// Wifi
494
500
#ifdef USE_WFM
@@ -511,7 +517,7 @@ void setup(){
511
517
// WiFi.softAP(hostName); // Core SVN 5179 use STA as default interface in mDNS (#7042)
512
518
WiFi.mode (WIFI_STA); // Core SVN 5179 use STA as default interface in mDNS (#7042)
513
519
WiFi.begin (ssid, password);
514
- if (WiFi.waitForConnectResult () != WL_CONNECTED) {
520
+ if (WiFi.waitForConnectResult () != WL_CONNECTED){
515
521
Serial.print (F (" STA: Failed!\n " ));
516
522
WiFi.disconnect (false );
517
523
delay (1000 );
@@ -545,15 +551,14 @@ void setup(){
545
551
EEPROM.begin (EEALL);
546
552
// EEPROM.get(EECH, memch); //current channel, no need
547
553
readEE (); // populate structure if healthy
548
- digitalWrite (ledPin, ledState);
549
554
Serial.printf (" Timer set %02d:%02d - %02d:%02d\n " , ee.hstart , ee.mstart , ee.hstop , ee.mstop );
550
555
Serial.printf (" Temp set %+2.1f\n " , ee.tempe );
551
556
552
557
// FS
553
558
#ifdef USE_FatFS
554
- if (MYFS.begin (false ," /ffat" ,3 )) { // limit the RAM usage, bottom line 8kb + 4kb takes per each file, default is 10
559
+ if (MYFS.begin (false ," /ffat" ,3 )){ // limit the RAM usage, bottom line 8kb + 4kb takes per each file, default is 10
555
560
#else
556
- if (MYFS.begin ()) {
561
+ if (MYFS.begin ()){
557
562
#endif
558
563
Serial.print (F (" FS mounted\n " ));
559
564
} else {
@@ -654,7 +659,7 @@ void setup(){
654
659
#ifdef USE_AUTH_STAT
655
660
if (!request->authenticate (http_username, http_password)) return request->requestAuthentication ();
656
661
#endif
657
- request->onDisconnect ([]() {
662
+ request->onDisconnect ([](){
658
663
#ifdef ESP32
659
664
ESP.restart ();
660
665
#elif defined(ESP8266)
@@ -672,7 +677,7 @@ void setup(){
672
677
#ifdef USE_AUTH_STAT
673
678
if (!request->authenticate (http_username, http_password)) return request->requestAuthentication ();
674
679
#endif
675
- request->onDisconnect ([]() {
680
+ request->onDisconnect ([](){
676
681
#ifdef ESP32
677
682
/*
678
683
//https://github.com/espressif/arduino-esp32/issues/400#issuecomment-499631249
@@ -726,7 +731,7 @@ void setup(){
726
731
727
732
// OTA
728
733
ArduinoOTA.setHostname (hostName);
729
- ArduinoOTA.onStart ([]() {
734
+ ArduinoOTA.onStart ([](){
730
735
Serial.print (F (" OTA Started ...\n " ));
731
736
MYFS.end (); // Clean FS
732
737
ws.textAll (" Now,OTA" ); // for all clients
@@ -742,5 +747,6 @@ void loop(){
742
747
updateDHT ();
743
748
udht = false ;
744
749
}
750
+ doOut ();
745
751
ArduinoOTA.handle ();
746
752
}
0 commit comments