-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAIR-BENTO.ino
1013 lines (910 loc) · 28.8 KB
/
AIR-BENTO.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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
MQTT transmitter from MH-Z14A CO2 sensor , GP2Y1014AU0F/GP2Y1010AU0F dust sensor and
pressure sensor BMP180 for ESP32
. connects ESP32 to NodeRED or Thingsboard via WiFi / MQTT server at 1883 port
or broadcast co2 and dust level as BLE name (option)
Author: coniferconifer
License: Apache License v2
Sep 16,2018
GPIO39 for GP2Y1014AU0F was dead wrong. changed to GPIO12.
GPIO34-39 can not be used for OUTPUT.
GPIO36 for SPI CS is changed to GPIO14.
Aug 21,2018
disabeABCcommand() is commented out and enableABCcommand() is ON.
Aug 4,2018
watch dog timer for loop monitoring
May 14,2018
I2C SHT21 humidity sensor is supported by #define HUMIDITY
May 13,2018
ESP32 Deep sleep mode is implemented , but comment outed. try #define DEEPSLEEP
ESP32 sleeps 30sec after data display is done once.
May 12,2018
intermittent WiFi connection
May 10,2018
uniq clientId for MQTT is supported
http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
"The first UTF-encoded string. The Client Identifier (Client ID) is between 1 and 23 characters long,
and uniquely identifies the client to the server. It must be unique across all clients connecting to
a single server, and is the key in handling Message IDs messages with QoS levels 1 and 2. If the
Client ID contains more than 23 characters, the server responds to the CONNECT message with a
CONNACK return code 2: Identifier Rejected."
"If a client with the same Client ID is already connected to the server, the "older" client
must be disconnected by the server before completing the CONNECT flow of the new client."
May 4,2018
1) MQTT post begins after WARMUPTIME msec elapsed to avoid unusual data
from MH-Z14A, mostly 400 or 410ppm after power on.
2) averaged pressure is used to send it to MQTT server
3) AIR BENTO dose not publish data to MQTT server if data integrity is lost.
such as CO2 is higher than 5000 or less than 300, etc.
May 3,2018
If there is a communication error for MH-Z14A , then read out remaining data from MH-Z14A.
May 2,2018
BLE is optional due to compiled flash memory size exceeds 100%
April 30,2018
BMP180 air pressure sensor is supported.
1. supports multiple WiFi access points and multiple MQTT servers for each access point.
2. BLE advertizing
when there is no WiFi access points , then simple BLE is used to advertize sensor values.
by using BLE name.(option)
3. supports mono OLED display SSD1306
note: I have no idea to run WiFi and BLE simultaneously, so if WiFi is not accessible then BLE is ON
at boot time.
*/
// References
// CO2 sensor:
// http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z14a_co2-manual-v1_01.pdf
// http://www.winsen-sensor.com/d/files/PDF/Infrared%20Gas%20Sensor/NDIR%20CO2%20SENSOR/MH-Z14%20CO2%20V2.4.pdf
// https://revspace.nl/MHZ19
//
// SSD1306 library from https://github.com/LilyGO/ESP32-OLED0.96-ssd1306
// http://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/spi_master.html
//
// PubSubClient from https://github.com/knolleary/pubsubclient
// MQTT server with visiualization tools https://thingsboard.io/
// public NTP server https://developers.google.com/time/
//
// Dust sensor:
// https://media.digikey.com/pdf/Data%20Sheets/Sharp%20PDFs/GP2Y1014AU0F_Spec_2-6-15.pdf
// technical detail of GP2Y10' analog front end http://www.ti.com/lit/ug/tidub65c/tidub65c.pdf
//
// BMP180 air pressure sensor with temperature
// https://github.com/adafruit/Adafruit-BMP085-Library
//
// trouble shooting:
// Why GPIO_NUM_33 is used for ADC
// referer to "ADC2 Channel cannot be used when WiFi is in use #440"
// https://github.com/espressif/arduino-esp32/issues/440
#define VERSION "20180916"
#include <WiFi.h>
#include <PubSubClient.h>
#define VERBOSE //print messages to console
#define OLED //Use SPI SSD1603 display
#define BMP180
#define HUMIDITY //SHT21
//#define BLE // by using the latest Arduino core for ESP32 with BLE ,
// the compiled flash memory reached to 104%.
// if #define BLE is not used , it is 45%.
// Use "Partition Scheme" in "Tools" menu in Arduino IDE and select "No OTA(large APP)" then this
// program will fit in the space for #define BLE
// hardware configuration
// SDA ---GPIO21 , SCK ---GPIO22 for BMP180
// CO2 sensor TX(19) --- GPIO 16 (RX) , RX --- GPIO 17(TX)
// SSD1603 RES --- GPIO0,DC/MISO --- GPIO2, CS --- GPIO14, D1/MOSI --- GPIO23, D0(SCK) --- GPIO18
// GY2P LED ---GPIO12, Vo ----GPIO33
//
HardwareSerial Co2Sensor(2);
//HardwareSerial Serial1(1); //RX as GPIO 9 TX as GPIO 10
#ifdef OLED
#include "SSD1306Spi.h"
#endif
// Initialize the OLED display using SPI
#ifdef HUMIDITY
#include <SHT21.h>
SHT21 SHT21;
#endif
#ifdef OLED
#ifdef BMP180
//SSD1306Spi display(0, 2, 36); //GPIO0(RES),GPIO2(DC/MISO),GPIO36(CS), GPIO23(D1(MOSI)),GPIO18(D0(SCK))
SSD1306Spi display(0, 2, 14); //GPIO0(RES),GPIO2(DC/MISO),GPIO14(CS), GPIO23(D1(MOSI)),GPIO18(D0(SCK))
#else
SSD1306Spi display(0, 2, 21); //GPIO0(RES),GPIO2(DC/MISO),GPI21(CS), GPIO23(D1(MOSI)),GPIO18(D0(SCK))
#endif
#endif
#define GP2YLED 12 //GPIO12 is used to drive LED pin of GP2Y by NPN transister 2N5551
//#define GPIO33 33 // Analog digital converter works when WiFi is in use
// do not use ADC2* pins with WiFi ON
// referer to "ADC2 Channel cannot be used when WiFi is in use #440"
// https://github.com/espressif/arduino-esp32/issues/440
//#define WIFI_POWERSAVE // intermittent WiFi connection reduces power consumpution from 0.18A to 0.08 A on average
// as of June 3,2018 , recompiled version with WIFI_POWERSAVE does not send data to MQTT server
//-------- Customise these values -----------
#include "credentials.h"
// credentials.h should include #define WIFI_SSID XXXXXX
// #define WIFI_PASS YYYYYY
// home , office
//#define BLETEST
#ifdef BLETEST
#define WIFI_SSID3 "testSSID" //non existing SSID to turn on BLE mode
char* ssidArray[] = { WIFI_SSID3, WIFI_SSID3 , WIFI_SSID3};
#else
char* ssidArray[] = { WIFI_SSID , WIFI_SSID1, WIFI_SSID2};
#endif
char* passwordArray[] = {WIFI_PASS, WIFI_PASS1, WIFI_PASS2};
char* tokenArray[] = { TOKEN , TOKEN1, TOKEN2};
char* serverArray[] = {SERVER, SERVER1, SERVER2};
#define MQTTRETRY 1
#define DEVICE_TYPE "ESP32" //
String clientId = DEVICE_TYPE ; //uniq clientID will be generated from MAC
char topic[] = "v1/devices/me/telemetry"; //for Thingsboard
#define MQTTPORT 1883 //for Thingsboard or MQTT server
#define WARMUPTIME 90000 // 60sec -> 90sec
#define TIMEZONE 9 //in Japan
#define NTP1 "time.google.com"
#define NTP2 "ntp.nict.jp"
#define NTP3 "ntp.jst.mfeed.ad.jp"
//https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Timer/WatchdogTimer/WatchdogTimer.ino#include "esp_system.h"
const int wdtTimeout = 180000; //time in ms to trigger the watchdog
hw_timer_t *timer = NULL;
void IRAM_ATTR resetModule() {
ets_printf("reboot\n");
esp_restart_noos();
}
/* https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/DeepSleep/ExternalWakeUp/ExternalWakeUp.ino
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
}
}
#define CELSIUS
#ifdef BLE
#include "SimpleBLE.h"
SimpleBLE ble;
#endif
//long int etime = 0;
float mhtempC = 0.0; //temperature in C reported from MH-Z14A
float mhtempF = 0.0; //temperature in F reported from MH-Z14A
////#include "esp_deep_sleep.h"
WiFiClient wifiClient;
PubSubClient client(serverArray[0], MQTTPORT, wifiClient);
// index of WiFi access point
int AP = -1; // access point is not yet found , -1 means not WiFi , but Simple BLE mode
#ifdef BMP180
#include <Adafruit_BMP085.h>
#include <Wire.h>
Adafruit_BMP085 pressure;
#endif
//#define DEEPSLEEP // experimental deep sleep while waiting
#ifdef DEEPSLEEP
#define WAITLOOP 1
#define SLEEPSEC 30
#else
#define WAITLOOP 5 // loop times x (co2,temp,pressure,dust)x DISPLAYINTERVAL(in msec) for each OLED display
#endif
void setup() {
pinMode(GP2YLED, OUTPUT); // do not use board LED for NodeMCU ESP32 board
digitalWrite(GP2YLED, LOW);
Co2Sensor.begin(9600); // communication with MH-Z14A
Serial.begin(115200);
print_wakeup_reason();
#ifdef VERBOSE
Serial.println();
Serial.print("AIR BENTO "); Serial.println( VERSION);
Serial.println("ESP32-MH-Z14A-GP2Y10-BMP180-SHT21-WiFi-MQTT or BLE");
#ifdef BLE
Serial.println("supports multiple WiFi access points or BLE");
#else
Serial.println("supports multiple WiFi access points");
#endif
#endif
#ifdef OLED
// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
#ifndef DEEPSLEEP
display.drawString(0, 0, "AIR BENTO");
display.drawString(0, 32, VERSION);
display.display();
delay(1000);
drawProgressBarMain(); // just for demonstration
delay(1000);
display.clear();
display.display();
#endif
// generate uniq clientId
uint64_t chipid;
chipid = ESP.getEfuseMac();
clientId += "-";
// clientId += DEVICE_ID;
clientId += String((uint32_t)chipid, HEX);
Serial.println("clientId :" + clientId);
#endif
AP = initWiFi();
if ( AP != -1) { // found WiFi AP
client.setClient(wifiClient);
client.setServer(serverArray[AP], MQTTPORT); // MQTT server for NodeRED or MQTT by Thingsboarxd
}
#ifdef PREHEATING
int i;
for (i = 0; i < 180; i++) {
#ifdef VERBOSE
Serial.print(".");
#endif
delay(1000);
}
#endif
/*
#ifdef DEEPSLEEP
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_MAX, ESP_PD_OPTION_OFF);
#endif
*/
// disableABCcommand(); //I'm not sure this works for MH-Z14A
// resetCO2(); // if co2 sensor should be calibrated, then leave AIR BENTO outdoor for at least 30min
// and run resetCO2(); once
enableABCcommand();
initDust();
#ifdef BMP180
if (pressure.begin()) {
Serial.println("BMP180 init success");
float P;
P = pressure.readPressure();
Serial.println(P / 100);
} else {
Serial.println("BMP180 init fail\n\n");
}
#endif
timer = timerBegin(0, 80, true); //timer 0, div 80
timerAttachInterrupt(timer, &resetModule, true); //attach callback
timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
timerAlarmEnable(timer); //enable interrupt
}
boolean mqttflag = false;
void loop() {
#define DISPLAYINTERVAL 2000
timerWrite(timer, 0); //reset timer (feed watchdog)
long int co2; long int d; float P; float T; float H;
d = 0; co2 = 0; P = 0.0; T = 0.0;
long int co2_ave = 0; long int d_ave = 0; float P_ave = 0.0; float T_ave = 0.0; float H_ave = 0.0;
int i;
boolean dataIntegrity;
dataIntegrity = HIGH;
#define MAXCO2 5000
#ifdef DEEPSLEEP
#define MINCO2 430
#else
#define MINCO2 390
#endif
for (i = 0; i < WAITLOOP; i++) {
co2 = getCO2();
// co2 = 9999; // test data for dataIntegrity=LOW;
// co2 = 9; // test data for dataIntegrity=LOW;
if ((co2 > MAXCO2) || (co2 < MINCO2)) dataIntegrity = LOW;
co2_ave = co2_ave + co2;
#ifdef OLED
displayCO2(co2);
#endif
delay(DISPLAYINTERVAL);
d = getDust(); d_ave = d_ave + d;
#ifdef OLED
displayDust(d);
#endif
delay(DISPLAYINTERVAL);
T = getTemperature(); T_ave = T_ave + T;
#ifdef CELSIUS
if ( (T > 50.0) ) dataIntegrity = LOW; // it is too hot , indicating something wrong in reading the sensor
#else // dataIntegrity flag is set to LOW , will prevent MQTT server to
// get unreliable data
// 122F == 50C
if ( (T > 122.0) ) dataIntegrity = LOW;
#endif
#ifdef OLED
displayTemp(T);
#endif
delay(DISPLAYINTERVAL);
#ifdef HUMIDITY
H = GetHumidity(); H_ave = H_ave + H;
if ( (H < 0.0) || (H > 100.0) ) dataIntegrity = LOW;
#ifdef OLED
displayHumidity(H);
#endif
delay(DISPLAYINTERVAL);
#endif
#ifdef BMP180
P = pressure.readPressure() / 100.0;
// P =1200;// test data for dataIntegrity=LOW;
// P = 0;// test data for dataIntegrity=LOW;
if ( (P < 500.0) || (P > 1100.0) ) dataIntegrity = LOW;
P_ave = P_ave + P;
#ifdef OLED
displayPressure(P);
#endif
delay(DISPLAYINTERVAL);
#else
P = 1000.0; P_ave = P_ave + P;
#endif
}
co2 = co2_ave / WAITLOOP;
d = d_ave / WAITLOOP;
T = T_ave / WAITLOOP;
P = P_ave / WAITLOOP;
H = H_ave / WAITLOOP;
#ifdef OLED
displayCO2(co2);
#endif
if ( AP == -1 ) {
#ifdef WIFI_POWERSAVE
Serial.println("WiFi turn off");
WiFi.mode(WIFI_OFF);
#endif
#ifdef OLED
#ifdef BLE
displayBLE();
#endif
#endif
#ifdef BLE
simpleBLE(co2, d); //annouce CO2 as BLE name and wait for WAITLOOP
#endif
} else {
#ifdef WIFI_POWERSAVE
initWiFi_retry();
#endif
if (dataIntegrity == HIGH) {
publishToMQTT(co2, P, T, H, d );
} else {
Serial.println("Data Integrity Error occurred. MQTT publish is not done.");
}
} // end of WiFi mode
}
void publishToMQTT(int co2, float P, float T, float H, float d) {
// WiFi mode
String payload = "{";
payload += "\"temperature\":"; payload += String( T , 1); payload += ",";
#ifdef HUMIDITY
payload += "\"humidity\":"; payload += String(H, 1) ; payload += ",";
#endif
payload += "\"co2\":"; payload += co2; payload += ",";
#ifdef BMP180
payload += "\"press\":"; payload += String(P, 2); payload += ",";
#endif
payload += "\"dust\":"; payload += String(d, 0); payload += ",";
payload += "\"rssi\":"; payload += WiFi.RSSI();
//payload += ",";
// payload += "\"date\":"; payload += s;
payload += "}";
// dont send too long string to MQTT server
// max 128byte
#ifndef DEEPSLEEP
#ifdef OLED
displayWiFiAP(AP, WiFi.RSSI());
displayWiFi();
delay(DISPLAYINTERVAL);
displayServer(AP);
delay(DISPLAYINTERVAL);
#endif
#endif
#ifdef VERBOSE
Serial.print("Sending payload: "); Serial.println(payload);
Serial.print("AP = "); Serial.println(AP);
#endif
// if (!client.connected()) {
// initWiFi_retry();
// }
#ifdef VERBOSE
Serial.print("Reconnecting client to "); Serial.println(serverArray[AP]);
#endif
int mqttloop = 0;
while (1) { // for thingsboard MQTT server
mqttflag = client.connect(clientId.c_str(), tokenArray[AP], NULL);
if (mqttflag == true) break;
Serial.print("-"); delay(500);
mqttloop++;
if (mqttloop > MQTTRETRY) { //there may be something wrong
mqttflag = false;
initWiFi_retry();
// ESP.restart();
break;
}
}
if (mqttflag == true) {
#ifndef DEEPSLEEP
if (millis() > WARMUPTIME) { // check if CO2 sensor get enough warm up time
#else
if (true) { //in case of DEEP SLEEP , MQTT publish any time
#endif
if (client.publish(topic, (char*) payload.c_str())) {
#ifdef VERBOSE
Serial.println("Publish ok");
#endif
} else {
#ifdef VERBOSE
Serial.println("Publish failed");
#endif
}
} // WARMUP decision end
} else {
#ifdef VERBOSE
Serial.println("unable to connect to MQTT server");
#endif
}
#ifdef WIFI_POWERSAVE
#ifdef VERBOSE
Serial.println("WiFi turn off");
#endif
WiFi.mode(WIFI_OFF);
#endif
#ifdef DEEPSLEEP
Serial.print("Deep Sleep: "); Serial.println(SLEEPSEC);
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
esp_sleep_enable_timer_wakeup((uint64_t)SLEEPSEC * (uint64_t)uS_TO_S_FACTOR);
// esp_deep_sleep_enable_timer_wakeup(SLEEPSEC * 1000 * 1000); // wakeup(restart) after WAITLOOP msecs
// esp_deep_sleep_start();
#else
// int i;
// for (i = 0; i < WAITLOOP; i++) {
// delay(1000);
// }
#endif
}
#ifdef OLED
void displayTime() {
struct tm timeInfo;
if ( AP == -1) return; // BLE mode , do nothing
getLocalTime(&timeInfo);
Serial.printf("Date: %04d/%02d/%02d %02d:%02d:%02d , ",
timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
char s[20];
sprintf(s, "%04d/%02d/%02d %02d:%02d:%02d",
timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, s );
display.display();
}
#ifdef BLE
void displayBLE() {
// display.clear();
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.setFont(ArialMT_Plain_10);
display.drawString(127, 0, "BLE mode" );
display.display();
}
#endif
void displayServer(int i) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "MQTT server at" );
display.drawString(0, 20, (String)serverArray[i] );
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(127, 40, "port=" + (String)MQTTPORT);
display.display();
}
void displayWiFiAP(int i, int rssi) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "...connected to" );
display.drawString(0, 20, (String)ssidArray[i] );
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(127, 40, (String)rssi + "dBm");
display.display();
}
void displayWiFi() {
// display.clear();
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.setFont(ArialMT_Plain_10);
display.drawString(127, 0, "WiFi" );
display.display();
}
void displayWiFioff() {
// display.clear();
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.setFont(ArialMT_Plain_10);
display.drawString(127, 0, "WiFi off" );
display.display();
}
void displayWiFiSearch() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.setFont(ArialMT_Plain_10);
display.drawString(127, 0, "WiFi AP Search" );
display.display();
}
void drawProgressBarMain() {
int counter;
for (counter = 0; counter < 100; counter++) {
drawProgressBar(counter) ;
delay(10);
}
}
void drawProgressBar(int progress) {
// int progress = counter % 100;
// draw the progress bar
display.setFont(ArialMT_Plain_24);
display.clear(); display.display();
display.drawProgressBar(0, 50, 120, 10, progress);
// draw the percentage as String
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 15, String(progress) + "%");
display.display();
}
void displayDust(int d) {
display.clear(); display.display();
displayTime();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
if (d >= 100) {
display.drawString(0, 10, "Dust: hell!" );
} else {
if (d >= 50) {
display.drawString(0, 10, "Dust: bad!" );
} else {
if (d > 20) {
display.drawString(0, 10, "Dust: bad" );
} else {
display.drawString(0, 10, "Dust: good" );
}
}
}
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(127, 36, " " + (String)d + "ug/m3");
display.display();
#ifdef VERBOSE
Serial.print("averaged dust(ug/m3) = "); Serial.println(d);
#endif
}
void displayCO2(int co2) {
display.clear(); display.display();
displayTime();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
if (co2 >= 1500) {
display.drawString(0, 8, "CO2: bad!" );
} else {
if (co2 >= 1000) {
display.drawString(0, 10, "CO2: vent" );
} else {
if (co2 > 390) {
display.drawString(0, 10, "CO2: good" );
} else {
display.drawString(0, 10, "CO2: " );
}
}
}
display.setTextAlignment(TEXT_ALIGN_RIGHT);
if (co2 > 350 ) {
display.drawString(127, 36, (String)co2 + "ppm");
} else {
display.drawString(127, 36, "warming up");
}
display.display();
// Serial.print("CO2 level(ppm): " );
Serial.println(co2);
}
void displayTemp(float temp) {
display.clear(); display.display();
displayTime();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
display.drawString(0, 8, "Temp.:" );
display.setTextAlignment(TEXT_ALIGN_RIGHT);
int itemp;
itemp = (int)temp;
#ifdef CELSIUS
display.drawString(127, 36, (String)itemp + "C");
#else
display.drawString(127, 36, (String)itemp + "F");
#endif
display.display();
Serial.println(itemp);
}
#ifdef BMP180
void displayPressure(float P) {
display.clear(); display.display();
displayTime();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
display.drawString(0, 8, "Press.:" );
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(127, 36, (String)P + "hPa");
display.display();
Serial.println(P);
}
#ifdef HUMIDITY
void displayHumidity(float H) {
display.clear(); display.display();
displayTime();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
display.drawString(0, 8, "Humid.:" );
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(127, 36, (String)H + "%RH");
display.display();
Serial.println(H);
}
#endif
#endif
#endif
#ifdef BLE
void simpleBLE(int co2, int d) {
#ifdef VERBOSE
Serial.println("Simple BLE mode");
#endif
String out = "CO2(ppm) ";
out += (String)co2;
out += ",dust ";
out += (String)d;
out += "(ug/m3)";
ble.begin(out);
delay(WAITLOOP * 1000);
}
#endif
#define MAX_TRY 15
int initWiFi() {
int i ;
int numaccesspt = (sizeof(ssidArray) / sizeof((ssidArray)[0]));
#ifdef OLED
displayWiFiSearch() ;
#endif
#ifdef VERBOSE
Serial.print("Number of Access Point = "); Serial.println(numaccesspt);
#endif
for (i = 0; i < numaccesspt; i++) {
#ifdef VERBOSE
Serial.print("WiFi connecting to "); Serial.println(ssidArray[i]);
#endif
WiFi.mode(WIFI_OFF);
WiFi.begin(ssidArray[i], passwordArray[i]);
int j;
for (j = 0; j < MAX_TRY; j++) {
if (WiFi.status() == WL_CONNECTED) {
int rssi = WiFi.RSSI();
Serial.printf("RSSI= %d\n", rssi);
// displayWiFiAP(i, rssi);
// delay(3000);
configTime(TIMEZONE * 3600L, 0, NTP1, NTP2, NTP3);
#ifdef VERBOSE
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
#endif
#ifdef OLED
display.clear();
display.display();
#endif
return (i);
}
#ifdef OLED
// drawProgressBar((j * 100) / MAX_TRY); //this prevents WiFi connection due to electromagnetic noise ?
#endif
delay(500);
#ifdef VERBOSE
Serial.print(".");
#endif
}
#ifdef VERBOSE
Serial.println(" can not connect to WiFi AP");
#endif
}
return (-1);
}
int initWiFi_retry() {
#ifdef VERBOSE
Serial.print("initWiFi_retry() WiFi connecting to "); Serial.println(ssidArray[AP]);
#endif
// Serial.print(" "); Serial.print(passwordArray[AP]);
WiFi.mode(WIFI_OFF);
WiFi.begin(ssidArray[AP], passwordArray[AP]);
int j;
for (j = 0; j < MAX_TRY; j++) {
if (WiFi.status() == WL_CONNECTED) {
#ifdef VERBOSE
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
#endif
#ifdef OLDE
display.clear();
display.display();
#endif
configTime(TIMEZONE * 3600L, 0, "time.google.com", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
return (AP);
}
delay(500);
#ifdef VERBOSE
Serial.print(".");
#endif
}
#ifdef VERBOSE
Serial.println(" can not connect to WiFi AP");
#endif
#ifdef OLED
displayWiFioff();
#endif
return (-1);
}
// get CPU core temperature and translate into estimated ambient temperature
extern "C" {
uint8_t temprature_sens_read();
}
float getTemperature()
{
float temp;
// Serial.printf("Temperature(temperature_sens_read()): % d\n", temprature_sens_read());
#ifdef CELSIUS
// temp = temperatureRead() - 26.0; //get ESP32 core temperature in Celsius and convert it to ambient temperature
//modify offset to adopt for your ambient temperature
#ifdef BMP180
temp = pressure.readTemperature() - 3.0 ; //need calibration for your BMP180
#else
temp = mhtempC; // MH-Z14A temperature
#endif
#else //F
// temp = temperature_sens_read();
#ifdef BMP180
float t;
t = pressure.readTemperature();
temp = t * 1.8 + 32;
#else
temp = mhtempF; // MH-Z14A temperature
#endif
#endif
#ifdef VERBOSE
// Serial.printf("Temperature: % 2.1f\n", temp);
#endif
return (temp);
}
float GetHumidity()
{
#ifdef HUMIDITY
float H = SHT21.getHumidity();
// Serial.println(H);
return (H);
#else
return (33.0 + (float)random(1, 4)); //dummy
#endif
}
uint8_t command[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
uint8_t reset[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
//http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf
// i'm not sure following ABC(automatic base correction) command for MH-Z19B can work for MH-Z14A
uint8_t disableABC[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86};
uint8_t enableABC[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6};
void initCO2() {
uint8_t c;
while (Co2Sensor.available()) {
c = Co2Sensor.read();
}
}
// assume MH-Z14A has the same ABC enable/disable function as MH-Z19
void disableABCcommand() {
initCO2();
Co2Sensor.write(disableABC, sizeof(disableABC));
}
void enableABCcommand() {
initCO2();
Co2Sensor.write(enableABC, sizeof(enableABC));
}
void resetCO2() {
Co2Sensor.write(reset, sizeof(reset));
}
int getCO2() {
int returnnum = 0;
uint16_t co2 = 0;
uint8_t readBuffer[9];
int i; uint8_t checksum = 0;
initCO2();
Co2Sensor.write(command, sizeof(command));
delay(100);
Co2Sensor.setTimeout(300);//set 300msec timeout
returnnum = Co2Sensor.readBytes(readBuffer, sizeof(readBuffer));
// if ( (readBuffer[0] == 0xFF) && (readBuffer[1] == 0x86)) {
if ( (readBuffer[0] == 0xFF) ) {
for ( i = 1; i < 8; i++)
{
checksum += readBuffer[i];
}
checksum = 0xff - checksum;
checksum += 1;
#define DEBUG
#ifdef DEBUG
for (i = 0; i < sizeof(readBuffer); i++) {
Serial.print(readBuffer[i], HEX);
Serial.print(" ");
} Serial.print(":"); Serial.println(checksum, HEX);
#endif
if (readBuffer[8] != checksum) {
Serial.println("MH-Z14A communication check sum error");
initCO2(); // in case something wrong happens, then clear data from MH-Z14A
return (9999);
}
switch ( readBuffer[1]) {
case 0x86:
co2 = (uint16_t)readBuffer[2] << 8;
co2 += readBuffer[3];
mhtempF = (float)readBuffer[4]; // MH-Z14A internal temperature (not documented in specification)
mhtempC = ( mhtempF - 32.0) / 1.8 + 4; // 4 is a offset for my sensor , needs calibration
return (co2);
case 0x79:
Serial.println("ABC disable command is issued.");
initCO2(); // clear data from MH-Z14A
return (returnnum);
default:
initCO2(); // clear data from MH-Z14A
return (returnnum);
}
}
initCO2(); // clear data from MH-Z14A
return (returnnum);
}
int dustbase = 0;
void initDust() {
pinMode(GP2YLED, OUTPUT);
dustbase = getDustbase();
if ( dustbase > 0) dustbase = 0;
#ifdef VERBOSE
Serial.print("dustbase = "); Serial.println(dustbase);
#endif
}
#define LEARNING 100
int getDustbase() {
int i;
long int d = 0;
for (i = 0; i < LEARNING; i++) {
d = d + getDustSub();
}
return (d / LEARNING);
}
#define COUNTER 10
int getDust() {
int i;
long int d = 0;
for (i = 0; i < COUNTER; i++) {
d = d + getDustSub();
}
if (d < 0) d = 0;
return (d / COUNTER);
}
//this function assumes LED pin of GP2Y1014AU0F is driven by NPN transister such as 2N5551
//, asserting digitalWrite(GP2YLED, HIGH); first
int getDustSub() { // ug/m3
#define NPNTRANSITER
#ifdef NPNTRANSITER
digitalWrite(GP2YLED, HIGH);
delayMicroseconds(280);
int v = analogRead(GPIO_NUM_33);
delayMicroseconds(40);
digitalWrite(GP2YLED, LOW);
#else
digitalWrite(GP2YLED, LOW);
delayMicroseconds(280);
int v = analogRead(GPIO_NUM_33);
delayMicroseconds(40);
digitalWrite(GP2YLED, HIGH);
#endif
delayMicroseconds(10000 - 280 - 40); // see specsheet of GP2Y10