Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1303 lines (1131 loc) · 40.5 KB
/
Copy pathmain.cpp
File metadata and controls
1303 lines (1131 loc) · 40.5 KB
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
#include <Arduino.h>
#include <Wire.h>
#include "LittleFS.h"
#include "WiFiManager.h"
#include "webServer.h"
#include "updater.h"
#include "fetch.h"
#include "configManager.h"
#include "dashboard.h"
#include "timeSync.h"
#include <TimeLib.h>
#include <TZ.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <Adafruit_SSD1306.h>
#include "MHZ19.h"
#include <SoftwareSerial.h>
#include <NeoPixelBrightnessBus.h> // instead of NeoPixelBus.h
#include <NeoPixelAnimator.h>
#include <Adafruit_BMP280.h> // BMP280 lib
#include <Adafruit_BME280.h> // BME280 lib
#include "Adafruit_SHT31.h" // SHT3x lib
#include <ESP8266httpUpdate.h> // Web Updater online
// I2C Scanner
uint8_t portArray[] = {5, 4};
//String portMap[] = {"D1", "D2"}; //for Wemos
String portMap[] = {"GPIO5", "GPIO4"};
// BME280 definitions
Adafruit_BME280 bme; // use I2C interface
Adafruit_Sensor *bme_temp = bme.getTemperatureSensor();
Adafruit_Sensor *bme_pressure = bme.getPressureSensor();
Adafruit_Sensor *bme_humidity = bme.getHumiditySensor();
// BMP280 definitions
Adafruit_BMP280 bmp; // use I2C interface
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
// SHT3x definitions
//Adafruit_SHT31 SHTSensor = Adafruit_SHT31();
Adafruit_SHT31 sht31 = Adafruit_SHT31();
// WS2812 definitions
const uint16_t PixelCount = 40; // this example assumes 4 pixels, making it smaller will cause a failure
const uint8_t PixelPin = 3; // make sure to set this to the correct pin, ignored for Esp8266
//const RgbColor CylonEyeColor(HtmlColor(0x7f0000));
const RgbColor CylonEyeColor(0,0,255);
#define colorSaturation 255 // saturation of color constants
RgbColor blue(0, 0, colorSaturation);
NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
//NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod>* strip = NULL;
NeoPixelAnimator animations(2); // only ever need 2 animations
uint16_t lastPixel = 0; // track the eye position
int8_t moveDir = 1; // track the direction of movement
// uncomment one of the lines below to see the effects of
// changing the ease function on the movement animation
AnimEaseFunction moveEase =
// NeoEase::Linear;
// NeoEase::QuadraticInOut;
// NeoEase::CubicInOut;
NeoEase::QuarticInOut;
// NeoEase::QuinticInOut;
// NeoEase::SinusoidalInOut;
// NeoEase::ExponentialInOut;
// NeoEase::CircularInOut;
// NeoPixelBus TEST ENDE
// MH-Z19B definitions
#define RX_PIN 13 // Pin13 D7 Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN 15 // Pin15 D8 Tx pin which the MHZ19 Rx pin is attached to
#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
MHZ19 myMHZ19; // Constructor for library
SoftwareSerial mySerial(RX_PIN, TX_PIN); // (Uno example) create device to MH-Z19 serial
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// no more delay with interval
unsigned long previousMillis = 0;
const long interval = 100; //
byte periode = 0;
//--- BEGIN MQTT ------------------------------------
#include <PubSubClient.h>
boolean mqttconnected = false;
int mqttPublishTime; // last publish time in seconds
// MQTT definitions
WiFiClient espclient;
PubSubClient MQTTclient(espclient);
#define MQTT_ID "CO2Light"
char buf[40];
#define MSG_BUFFER_SIZE (20)
char result[MSG_BUFFER_SIZE];
// Update client
WiFiClient updateclient;
// Tasks
struct task
{
unsigned long rate;
unsigned long previous;
};
task taskA = { .rate = 1000, .previous = 0 }; // 1 second
task taskC = { .rate = 15000, .previous = 0 }; // 15 seconds
task taskB = { .rate = 60000, .previous = 0 }; // 1 minute
// Global definitions
#define BUILTIN_LED 2 // On board LED
const int buzzer = D3; // Buzzer
bool piep = false; // helper Buzzer
bool alarm = false; // helper Buzzer
bool reboot = false; // Flag to reboot device
int seconds = 0;
bool isCaptive = false; // Captive portal active
unsigned long timeElapse = 0;
bool calibrationStarted = false;
int prevseconds;
int displayPtr = 1; // pointer for display items
bool played = false; // helper for display
bool updatePending = false; // Update pending
int smoothAnalog = 0; // debounce analo value
int CursorX; // Cursor of display
int CursorY; // Cursor of display
boolean blinking = false; // helper for blinking
int intensity = 30; // LED intensity
String co2Colour; // LED colour
int temper = 0; // Temperatur integer
// JSON stuff
StaticJsonDocument<512> doc;
char language[512];
// SUBROUTINES
//*************************************************************************************
// I2C Portscanner
void check_if_exist_I2C() {
byte error, address;
int nDevices;
nDevices = 0;
for (address = 1; address < 127; address++ ) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print(F("[DEBUG] I2C device found at address 0x"));
if (address < 16) {
Serial.print(F("0"));
if (configManager.data.displayType == 0) {
display.print("0");
}
}
Serial.print(address, HEX);
Serial.println(F(" !"));
if (configManager.data.displayType == 0) {
display.print("0x");
display.print(address, HEX);
CursorY += 24;
display.setCursor(0, CursorY);
}
nDevices++;
} else if (error == 4) {
Serial.print(F("[DEBUG] Unknow error at address 0x"));
if (address < 16)
Serial.print(F("0"));
Serial.println(address, HEX);
}
}
if (configManager.data.displayType == 0) {
display.display();
}
if (nDevices == 0)
Serial.println(F("[DEBUG] No I2C devices found"));
else
Serial.println(F("[DEBUG] **********************************\n"));
}
void scanPorts() {
for (uint8_t i = 0; i < sizeof(portArray); i++) {
for (uint8_t j = 0; j < sizeof(portArray); j++) {
if (i != j){
Serial.println("[DEBUG] Scanning (SDA : SCL) - " + portMap[i] + " : " + portMap[j]);
Wire.begin(portArray[i], portArray[j]);
check_if_exist_I2C();
}
}
}
}
// NEOPIXELBUS ANIMATION BEGIN
void FadeAll(uint8_t darkenBy)
{
RgbColor color;
for (uint16_t indexPixel = 0; indexPixel < configManager.data.numofpixels; indexPixel++)
{
color = strip.GetPixelColor(indexPixel);
color.Darken(darkenBy);
strip.SetPixelColor(indexPixel, color);
}
}
void FadeAnimUpdate(const AnimationParam& param)
{
if (param.state == AnimationState_Completed)
{
FadeAll(10);
animations.RestartAnimation(param.index);
}
}
void MoveAnimUpdate(const AnimationParam& param)
{
// apply the movement animation curve
float progress = moveEase(param.progress);
// use the curved progress to calculate the pixel to effect
uint16_t nextPixel;
if (moveDir > 0)
{
nextPixel = progress * configManager.data.numofpixels;
}
else
{
nextPixel = (1.0f - progress) * configManager.data.numofpixels;
}
// if progress moves fast enough, we may move more than
// one pixel, so we update all between the calculated and
// the last
if (lastPixel != nextPixel)
{
for (uint16_t i = lastPixel + moveDir; i != nextPixel; i += moveDir)
{
strip.SetPixelColor(i, CylonEyeColor);
}
}
strip.SetPixelColor(nextPixel, CylonEyeColor);
lastPixel = nextPixel;
if (param.state == AnimationState_Completed)
{
// reverse direction of movement
moveDir *= -1;
// done, time to restart this position tracking animation/timer
animations.RestartAnimation(param.index);
}
}
void SetupAnimations()
{
// fade all pixels providing a tail that is longer the faster
// the pixel moves.
animations.StartAnimation(0, 5, FadeAnimUpdate);
// take several seconds to move eye fron one side to the other
animations.StartAnimation(1, 2000, MoveAnimUpdate);
}
// NEOPIXELBUS END
void setLED(uint8_t R, uint8_t G, uint8_t B) {
RgbColor RGB(R,G,B);
if (!configManager.data.ledSegments) {
for(uint8_t i=0; i<configManager.data.numofpixels; i++) { // For each pixel...
strip.SetPixelColor(i, RGB);
strip.Show();
}
} else {
for(uint8_t i=0; i<configManager.data.firstSegment; i++) {
strip.SetPixelColor(i, RGB);
strip.Show();
}
}
}
void colorWipe(uint8_t R, uint8_t G, uint8_t B) {
RgbColor RGB(R,G,B);
if (!configManager.data.ledSegments) {
for(uint8_t i=0; i<configManager.data.numofpixels; i++) { // For each pixel...
strip.SetPixelColor(i, RGB);
strip.Show();
delay(20);
}
} else {
for(uint8_t i=0; i<configManager.data.firstSegment; i++) {
strip.SetPixelColor(i, RGB);
strip.Show();
delay(20);
}
}
}
void colorTemp(uint8_t R, uint8_t G, uint8_t B) {
RgbColor RGB(R,G,B);
for(uint8_t i=configManager.data.firstSegment; i < (configManager.data.firstSegment+configManager.data.secondSegment); i++) { // For each pixel...
strip.SetPixelColor(i, RGB);
strip.Show();
delay(20);
}
}
void colorMQ(uint8_t R, uint8_t G, uint8_t B) {
RgbColor RGB(R,G,B);
for(uint8_t i=(configManager.data.firstSegment + configManager.data.secondSegment); i < (configManager.data.firstSegment+configManager.data.secondSegment+configManager.data.thirdSegment); i++) { // For each pixel...
strip.SetPixelColor(i, RGB);
strip.Show();
delay(20);
}
}
void saveCallback() {
intensity = configManager.data.matrixIntensity;
strip.SetBrightness(intensity);
}
void MqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.print(F("[INFO] Message arrived ["));
Serial.print(topic);
Serial.print(F("] "));
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// convert payload to intuff
payload[length] = '\0';
String s = String((char*)payload);
intensity = s.toInt();
configManager.data.matrixIntensity = intensity;
Serial.print(F("[INFO] Intensity: "));
Serial.println(intensity);
strip.SetBrightness(intensity);
}
// function to crate HTML Colour
void array_to_string(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
void setLedCO2Colors(void) {
char str[32] = "";
if (dash.data.CO2 <= configManager.data.boarderGreen ) { // green
colorWipe(configManager.data.colorGreen[0],configManager.data.colorGreen[1],configManager.data.colorGreen[2]);
blinking = false;
// convert to HTML colour
array_to_string(configManager.data.colorGreen, 3, str);
co2Colour = "#";
co2Colour += String(str);
//Serial.println(co2Colour);
}
else if (( dash.data.CO2 > configManager.data.boarderGreen ) && ( dash.data.CO2 < configManager.data.boarderYellow)) { // yellow
colorWipe(configManager.data.colorYellow[0],configManager.data.colorYellow[1],configManager.data.colorYellow[2]);
blinking = false;
// convert to HTML colour
array_to_string(configManager.data.colorYellow, 3, str);
co2Colour = "#";
co2Colour += String(str);
//Serial.println(co2Colour);
}
else if (( dash.data.CO2 >= configManager.data.boarderYellow) && ( dash.data.CO2 < configManager.data.boarderOrange)) { // orange
colorWipe(configManager.data.colorOrange[0],configManager.data.colorOrange[1],configManager.data.colorOrange[2]);
blinking = false;
// convert to HTML colour
array_to_string(configManager.data.colorOrange, 3, str);
co2Colour = "#";
co2Colour += String(str);
//Serial.println(co2Colour);
}
else if (( dash.data.CO2 >= configManager.data.boarderOrange) && ( dash.data.CO2 < configManager.data.boarderRed)) { // orange
colorWipe(configManager.data.colorRed[0],configManager.data.colorRed[1],configManager.data.colorRed[2]);
blinking = false;
// convert to HTML colour
array_to_string(configManager.data.colorRed, 3, str);
co2Colour = "#";
co2Colour += String(str);
//Serial.println(co2Colour);
}
else {
setLED(configManager.data.colorRed[0],configManager.data.colorRed[1],configManager.data.colorRed[2]); // red
blinking = true;
}
}
void setLedTempColors(void) {
char str[32] = "";
if (temper <= configManager.data.tempBoarderKalt ) {
colorTemp(configManager.data.TempColorKalt[0],configManager.data.TempColorKalt[1],configManager.data.TempColorKalt[2]);
}
else if (( temper > configManager.data.tempBoarderKalt ) && ( temper < configManager.data.tempBoarderNormal)) {
colorTemp(configManager.data.TempColorNormal[0],configManager.data.TempColorNormal[1],configManager.data.TempColorNormal[2]);
}
else if (( temper >= configManager.data.tempBoarderNormal) && ( temper < configManager.data.tempBoarderComfort)) {
colorTemp(configManager.data.TempColorNormal[0],configManager.data.TempColorNormal[1],configManager.data.TempColorNormal[2]);
}
else if (( temper >= configManager.data.tempBoarderComfort) && ( temper < configManager.data.tempBoarderWarm)) {
colorTemp(configManager.data.TempColorComfort[0],configManager.data.TempColorComfort[1],configManager.data.TempColorComfort[2]);
}
else if (temper >= configManager.data.tempBoarderWarm) {
colorTemp(configManager.data.TempColorHot[0],configManager.data.TempColorHot[1],configManager.data.TempColorHot[2]);
}
}
void setLedMQColors(void) {
char str[32] = "";
if (smoothAnalog <= configManager.data.MqBoarderNormal ) {
colorMQ(configManager.data.MqColorNormal[0],configManager.data.MqColorNormal[1],configManager.data.MqColorNormal[2]);
}
else if (( smoothAnalog > configManager.data.MqBoarderNormal ) && ( smoothAnalog < configManager.data.MqBoarderWarning)) {
colorMQ(configManager.data.MqColorWarning[0],configManager.data.MqColorWarning[1],configManager.data.MqColorWarning[2]);
}
else if (smoothAnalog >= configManager.data.MqBoarderWarning) {
colorMQ(configManager.data.MqColorCritical[0],configManager.data.MqColorCritical[1],configManager.data.MqColorCritical[2]);
}
}
void PublishMQTT(void) { //MQTTclient.publish
// Publish CO2 value
String topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/CO2";
dtostrf(dash.data.CO2, 5, 0, result);
MQTTclient.publish(topic.c_str(), result);
// Publish Intensity
topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/Brightness";
dtostrf(configManager.data.matrixIntensity, 5, 0, result);
MQTTclient.publish(topic.c_str(), result);
// Publish CO2 Colour
topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/CO2Colour";
MQTTclient.publish( topic.c_str(), co2Colour.c_str() );
// Publish Temperature
topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/Temperature";
MQTTclient.publish(topic.c_str(), dash.data.Temperature);
// Publish Humidity
if (configManager.data.sensorType > 1) {
topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/Humidity";
MQTTclient.publish(topic.c_str(), dash.data.Humidity);
}
// Publish Pressure
if ((configManager.data.sensorType == 1) || (configManager.data.sensorType == 2)) {
topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/Pressure";
MQTTclient.publish(topic.c_str(), dash.data.Pressure);
}
// Publish MQ-Sensor
topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic +"/MQ-Sensor";
dtostrf(dash.data.MQ_Sensor, 4, 0, result);
MQTTclient.publish(topic.c_str(), result);
}
void reconnect(void) {
// reconnect to MQTT Server
if (!MQTTclient.connected()) {
dash.data.MQTT_Connected = false;
Serial.println("Attempting MQTT connection...");
// Create a random client ID
String clientId = "CO2Light-";
clientId += String(WiFi.macAddress());
//clientId += String(random(0xffff), HEX);
// Attempt to connect
if (MQTTclient.connect(clientId.c_str(),configManager.data.mqtt_user,configManager.data.mqtt_password)) {
Serial.println("connected");
dash.data.MQTT_Connected = true;
// Once connected, publish an announcement...
PublishMQTT();
// ... and resubscribe
String topic = "CO2Light/";
topic = topic + configManager.data.place;
topic = topic + "/Brightness";
MQTTclient.subscribe(topic.c_str());
//MQTTclient.subscribe((char*) topic.c_str());
} else {
Serial.print("failed, rc=");
Serial.print(MQTTclient.state());
Serial.println(" try again in one minute");
}
}
}
void verifyRange(int range); // Need for calibration
void calibrationActive() { // Calibration in progress
if (dash.data.CO2_Calibration) {
dash.data.CO2_Calibration = false;
seconds = 0;
calibrationStarted = true; // starting calibration mode
Serial.print(F("[INFO] calibrationStarted "));
Serial.println(calibrationStarted);
myMHZ19.autoCalibration(false); // make sure auto calibration is off
Serial.print("[DEBUG] ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF"); // now print it's status
Serial.println("[INFO] Waiting 20 minutes to stabalise...");
// Info on display
if (configManager.data.displayType == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
String Dcalibrate = doc["Dcalibrate"];
display.print(Dcalibrate);
display.setCursor(0,24);
display.setTextSize(2);
String Dstarted = doc["Dstarted"];
display.print(Dstarted);
display.setCursor(0,48);
display.setTextSize(1);
String Dpwait = doc["Dpwait"];
display.print(Dpwait);
display.display();
}
delay(4000);
}
if (seconds < 1200) { // 20 minutes = 1200 seconds
if (seconds > prevseconds) {
prevseconds = seconds;
int remaining = 1200 - seconds;
if (configManager.data.displayType == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
String Dcalibrate = doc["Dcalibrate"];
display.print(Dcalibrate);
display.setCursor(0,30);
display.setTextSize(2);
display.print(remaining);
display.setCursor(0,48);
display.setTextSize(1);
String DsecondsRamaining = doc["DsecondsRamaining"];
display.print(DsecondsRamaining);
display.display();
}
Serial.print(F("[DEBUG] Calibration"));
Serial.print(remaining);
Serial.println(F(" seconds remaining"));
}
} else {
myMHZ19.calibrate(); // Take a reading which be used as the zero point for 400 ppm
myMHZ19.autoCalibration(configManager.data.autoCalibration); // make sure auto calibration is on
calibrationStarted = false;
seconds = 0;
}
}
void printCO2() {
if (configManager.data.displayType == 0) {
// clear display
display.clearDisplay();
// display co2
display.setCursor(0,0);
display.setTextSize(2);
display.print("CO2");
display.setCursor(0, 36);
display.setTextSize(4);
if (dash.data.CO2 == 0 ) {
display.print("----");
} else {
display.print(dash.data.CO2);
}
display.setTextSize(1);
display.print(" ppm");
display.display();
}
Serial.print(F("[INFO] CO2 : "));
Serial.print(dash.data.CO2);
Serial.println(F(" ppm"));
}
void printTemperature() {
if (configManager.data.displayType == 0) {
// clear display
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
String DTemperature = doc["DTemperature"];
display.print(DTemperature);
display.setCursor(0, 36);
display.setTextSize(3);
display.print(dash.data.Temperature);
display.setTextSize(1);
display.cp437(true);
display.write(521);
display.setTextSize(2);
display.print("C");
display.display();
}
Serial.print(F("[INFO] Temperature : "));
Serial.print(dash.data.Temperature);
Serial.println(F(" °C"));
}
void printPressure() {
if (configManager.data.displayType == 0) {
// clear display
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
String DPressure = doc["DPressure"];
display.print(DPressure);
display.setCursor(0, 36);
display.setTextSize(3);
if (dash.data.Pressure == 0 ) {
display.print("----");
} else {
display.print(dash.data.Pressure);
}
display.setTextSize(1);
display.print(" hPa");
display.display();
}
}
void printHumidity() {
if (configManager.data.displayType == 0) {
// clear display
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
String DHumidity = doc["DHumidity"];
display.print(DHumidity);
display.setCursor(0, 36);
display.setTextSize(3);
if (dash.data.Humidity == 0 ) {
display.print("----");
} else {
display.print(dash.data.Humidity);
}
display.setTextSize(2);
display.print(" %");
display.display();
}
}
void printWifimode() {
if (configManager.data.displayType == 0) {
display.clearDisplay();
if (isCaptive) {
display.setCursor(0,0);
display.setTextSize(2);
String DCaptive = doc["DCaptive"];
display.print(DCaptive);
display.setCursor(0,16);
String DPortal = doc["DPortal"];
display.print(DPortal);
display.setCursor(0,40);
display.setTextSize(1);
String DIPAddr = doc["DIPAddr"];
display.print(DIPAddr);
display.setCursor(0,56);
display.setTextSize(1);
display.print("192.168.4.1");
display.display();
} else {
display.setCursor(0,0);
display.setTextSize(2);
String DClient = doc["DClient"];
display.print(DClient);
display.setCursor(0,16);
String DMode = doc["DMode"];
display.print(DMode);
display.setCursor(0,40);
display.setTextSize(1);
String DIPAddr = doc["DIPAddr"];
display.print(DIPAddr);
display.setCursor(0,56);
display.setTextSize(1);
display.print(WiFi.localIP());
display.display();
}
}
}
void DisplayValues (void) {
played = false;
seconds = 0;
if (displayPtr == 1) {
Serial.println(F("[DEBUG] show CO2"));
printCO2();
played = true;
} else if (displayPtr == 2) {
Serial.println(F("[DEBUG] show Temperature"));
printTemperature();
played = true;
} else if ((displayPtr == 3) && ((configManager.data.sensorType == 1) || (configManager.data.sensorType ==2))) {
Serial.println(F("[DEBUG] show Pressure"));
printPressure();
played = true;
} else if ((displayPtr == 4 ) && (configManager.data.sensorType > 1)) {
Serial.println(F("[DEBUG] show Humidity"));
printHumidity();
played = true;
} else if (displayPtr == 5) {
Serial.println(F("[DEBUG] show Wifi Mode"));
printWifimode();
played = true;
} else {
displayPtr++;
}
if (displayPtr >=6) {
displayPtr = 1;
}
if (played) {
displayPtr++;
}
}
void getCO2() {
// MH-Z19B reading
// note: getCO2() default is command "CO2 Unlimited". This returns the correct CO2 reading even
// if below background CO2 levels or above range (useful to validate sensor). You can use the
// usual documented command with getCO2(false)
Serial.println(F("[DEBUG] read CO2 value"));
uint16_t co2value = myMHZ19.getCO2(true); // Request CO2 (as ppm)
if (configManager.data.sensorType == 0) {
temper = myMHZ19.getTemperature(); // Request Temperature
dtostrf(myMHZ19.getTemperature(), 2, 1, dash.data.Temperature); // float to char
}
if(co2value !=0) // Response from filter
{
dash.data.CO2 = co2value + configManager.data.Co2Offset;
Serial.print(F("[DEBUG] co2 Value successfully Recieved: "));
Serial.println(dash.data.CO2);
Serial.print(F("[DEBUG] Response Code: "));
Serial.println(myMHZ19.errorCode); // Get the Error Code value
} else {
Serial.println(F("[DEBUG] Failed to recieve CO2 value - Error"));
Serial.print(F("[DEBUG] Response Code: "));
Serial.println(myMHZ19.errorCode); // Get the Error Code value
dash.data.CO2 = 0;
}
}
void getSensor() {
// Read Sensors
if (configManager.data.sensorType == 1) {
sensors_event_t temp_event, pressure_event;
bmp_temp->getEvent(&temp_event);
bmp_pressure->getEvent(&pressure_event);
Serial.print(F("[INFO] Temperature = "));
Serial.print(temp_event.temperature);
Serial.println(" *C");
Serial.print(F("[INFO] Pressure = "));
Serial.print(pressure_event.pressure);
Serial.println(" hPa");
dtostrf(temp_event.temperature, 2, 1, dash.data.Temperature); // float to char
temper = temp_event.temperature;
dtostrf(pressure_event.pressure, 5, 1, dash.data.Pressure); // float to char
}
if (configManager.data.sensorType == 2) {
sensors_event_t temp_event, pressure_event, humidity_event;
bme_temp->getEvent(&temp_event);
bme_pressure->getEvent(&pressure_event);
bme_humidity->getEvent(&humidity_event);
Serial.print(F("[INFO] Temperature = "));
Serial.print(temp_event.temperature);
Serial.println(" *C");
Serial.print(F("[INFO] Humidity = "));
Serial.print(humidity_event.relative_humidity);
Serial.println(" %");
Serial.print(F("[INFO] Pressure = "));
Serial.print(pressure_event.pressure);
Serial.println(" hPa");
dtostrf(temp_event.temperature, 2, 1, dash.data.Temperature); // float to char
temper = temp_event.temperature;
dtostrf(pressure_event.pressure, 5, 1, dash.data.Pressure); // float to char
dtostrf(humidity_event.relative_humidity, 5, 1, dash.data.Humidity); //float to char
}
if (configManager.data.sensorType == 3) {
// DHT3x Sensor
dtostrf(sht31.readTemperature(), 5, 1, dash.data.Temperature); //float to char
temper = sht31.readTemperature();
dtostrf(sht31.readHumidity(), 5, 1, dash.data.Humidity); //float to char
Serial.print(F("[INFO] Temperature = "));
Serial.print(dash.data.Temperature);
Serial.println(" *C");
Serial.print(F("[INFO] Humidity = "));
Serial.print(dash.data.Humidity);
Serial.println(" %");
}
if (configManager.data.ledSegments) {
setLedTempColors();
}
}
void update_started() {
if (configManager.data.displayType == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
display.print("Update");
display.setCursor(0, 36);
display.setTextSize(2);
display.print("started");
display.display();
}
Serial.println(F("[INFO] HTTP update process started"));
}
void update_finished() {
if (configManager.data.displayType == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
display.print("Update");
display.setCursor(0, 36);
display.setTextSize(2);
display.print("done");
display.display();
}
Serial.println(F("[INFO] HTTP update process finished"));
}
void update_progress(int cur, int total) {
char progressString[10];
float percent = ((float)cur / (float)total ) * 100;
//sprintf(progressString, " %s", String(percent).c_str() );
sprintf(progressString, " %s", String(percent,0).c_str() );
if (configManager.data.displayType == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
display.print("Update");
display.setCursor(0, 36);
display.setTextSize(3);
display.print(progressString);
display.print(" %");
display.display();
}
Serial.printf("[INFO] HTTP update process at %d of %d bytes...\n", cur, total);
}
void update_error(int err) {
char errorString[8];
sprintf(errorString, "Err %d", err);
if (configManager.data.displayType == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
display.print("Update");
display.setCursor(0, 36);
display.setTextSize(2);
display.print(errorString);
display.display();
}
Serial.printf("[INFO] HTTP update fatal error code %d\n", err);
}
void updateFirmware() {
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
// Add optional callback notifiers
ESPhttpUpdate.onStart(update_started);
ESPhttpUpdate.onEnd(update_finished);
ESPhttpUpdate.onProgress(update_progress);
ESPhttpUpdate.onError(update_error);
t_httpUpdate_return ret = ESPhttpUpdate.update(updateclient, "http://firmware.kidbuild.de/CO2light/firmware.bin");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("[ERROR] HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println(F("[INFO] HTTP_UPDATE_NO_UPDATES"));
break;
case HTTP_UPDATE_OK:
Serial.println(F("[INFO] HTTP_UPDATE_OK"));
break;
}
}
void setup() {
Serial.begin(115200);
LittleFS.begin();
GUI.begin();
configManager.begin();
WiFiManager.begin(configManager.data.projectName);
configManager.setConfigSaveCallback(saveCallback);
dash.begin(500);
// Language
File langFile = LittleFS.open(configManager.data.DLang, "r");
if (!langFile) {
Serial.println(F("[INFO] No language file found!"));
} else {
int x = 0;
Serial.println(F("[INFO] Reading language file"));
Serial.print(F("File name: "));
Serial.println(langFile);
//Data from file
for (int i=0; i<langFile.size(); i++) {
language[x] = langFile.read(); // read byte
x++; // inc x
language[x] = '\0'; // Null termination
}
langFile.close();
deserializeJson(doc, language);
String Dcalibrate = doc["Dcalibrate"];
String Dstarted = doc["Dstarted"];
String Dpwait = doc["Dpwait"];
String DsecondsRamaining = doc["DsecondsRamaining"];
String DTemperature = doc["DTemperature"];
String DPressure = doc["DPressure"];
String DHumidity = doc["DHumidity"];
String DCaptive = doc["DCaptive"];
String DPortal = doc["DPortal"];
String DIPAddr = doc["DIPAddr"];
String DClient = doc["DClient"];
String DMode = doc["DMode"];
Serial.println(Dcalibrate);
Serial.println(Dstarted);
You can’t perform that action at this time.
