-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindoorairsensor.ino
650 lines (610 loc) · 16.1 KB
/
indoorairsensor.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
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BME280.h>
#include <U8x8lib.h>
#include <Adafruit_CCS811.h>
#include <Preferences.h>
String version="2022-03-20";
/* hardware pins Gx */
int ledred = 4;
int ledyellow = 0;
int ledgreen = 2;
int keypin1 = 34;
int keypin2 = 35;
/* hardware settings */
Preferences preferences; // prepare flash
Adafruit_CCS811 ccs; // I2C
Adafruit_BME280 bme; // I2C
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); // display
int baudrate = 115200; // serial interface
int initcounter;
int ccswarmup = 300; //co2 sensor needs 5+ minutes to warm up
int ledredchannel = 0; // PWM channel
int ledyellowchannel = 1;
int ledgreenchannel = 2;
int ledredbright = 240; // default brightness
int ledyellowbright = 255;
int ledgreenbright = 35;
const int freq = 1000; // PWM frequency
const int resolution = 8; // PWM resolution
/* defaults, can be overwritten from flash */
boolean serialout = true; // default: serial on
boolean ledon = true; // default: LEDs on
int everysec = 2; // default: read and update every 2 seconds
int ccsdrivemode = 10; // default: 10 sec readings from the ccs
boolean powersave = false; // default: start with powersave off
/* floating average for smoother changes */
float ratio1 = 0.75;
float ratio2 = 0.25;
/* thresholds */
int co2l1 = 1000; // green below
int co2l2 = 1400;
int co2l3 = 2000;
int co2l4 = 5000; // red above
int humidl1 = 35; // too low below
int humidl2 = 40;
int humidl3 = 60;
int humidl4 = 70; // too high above
/* keys */
boolean key1;
boolean lastkey1;
int keyduration1;
boolean key2;
boolean lastkey2;
int keyduration2;
int longkey = 3; //seconds to enter menu
int co2; //actual measure
int avg_co2 = 400; //floating average
int tvoc;
int avg_tvoc;
float temp;
float avg_temp;
float press;
float avg_press;
float humid;
float avg_humid;
String text_humid; //warnings
String text_co2; //warnings
char rtemp[7]; //output formatting
char rpress[7];
char rhumid[7];
char rco2[7];
char rtvoc[7];
int co2level = 0; //0-4 for warnings, 5 warmup
String command; //for the serial interface
void setup() {
/* hardware */
pinMode(keypin1, INPUT); //down button
pinMode(keypin2, INPUT); //up button
pinMode(ledgreen, OUTPUT);
pinMode(ledyellow, OUTPUT);
pinMode(ledred, OUTPUT);
/* LEDs */
ledcSetup(ledredchannel, freq, resolution);
ledcSetup(ledyellowchannel, freq, resolution);
ledcSetup(ledgreenchannel, freq, resolution);
ledcAttachPin(ledred, ledredchannel);
ledcAttachPin(ledyellow, ledyellowchannel);
ledcAttachPin(ledgreen, ledgreenchannel);
/* flash */
preferences.begin("sensor", false); // open the sensor namespace rw
bool flash = preferences.getBool("flashavailable"); // values available?
/* display */
u8x8.begin();
u8x8.setPowerSave(0);
splash();
ledplay();
/* serial interface */
Serial.begin(baudrate);
Serial.setTimeout(2000);
ledplay();
Serial.print("bme280-ccs811-096oled ");
Serial.println(version);
/* read defaults from flash, if available */
if ( flash == true ) {
Serial.println("flash available, reading values");
loadvars();
}
else {
Serial.println("no flash, using defaults");
}
/* init ccs */
if(!ccs.begin()){
Serial.println("Failed to start ccs811. Halt.");
while(1);
}
initcounter = ccswarmup / everysec; // make it 5 minutes
while(!ccs.available()); //give it a sec
setccsdrivemode();
Serial.println(F("ccs811 init done"));
/* init BME */
unsigned status;
status = bme.begin(0x76); //AZ-Delivery's 280 is on 0x76, not 0x77
if (!status) {
Serial.println("Failed to start BME280. Halt.");
while (1) delay(10);
}
bme.setSampling(Adafruit_BME280::MODE_NORMAL,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF,
Adafruit_BME280::STANDBY_MS_0_5);
Serial.println("BME280 init done");
/* start serial comms */
if ( serialout == true ) {
Serial.print("start sending values, every ");
Serial.print(everysec);
Serial.println(" sec");
}
else {
Serial.println("sending values disabled");
}
Serial.println("");
/* set display and averages to start the loop */
firstrun(); //set floating averages to actuals
u8x8.setPowerSave(powersave);
prepdisplay();
}
void setccsdrivemode() {
if ( ccsdrivemode == 1 ){
ccs.setDriveMode(CCS811_DRIVE_MODE_1SEC);
}
else if ( ccsdrivemode == 10 ){
ccs.setDriveMode(CCS811_DRIVE_MODE_10SEC);
}
else if ( ccsdrivemode == 60 ) {
ccs.setDriveMode(CCS811_DRIVE_MODE_60SEC);
}
}
void ledplay() {
ledcWrite(ledgreenchannel, ledgreenbright);
delay(200);
ledcWrite(ledyellowchannel, ledyellowbright);
delay(200);
ledcWrite(ledgreenchannel, 0);
delay(200);
ledcWrite(ledredchannel, ledredbright);
delay(200);
ledcWrite(ledyellowchannel, 0);
delay(200);
ledcWrite(ledredchannel, 0);
}
void firstrun() {
readbme();
readccs();
avg_temp = temp;
avg_press = press;
avg_humid = humid;
avg_co2 = 400;
avg_tvoc = tvoc;
}
void fbig() {
u8x8.setFont(u8x8_font_px437wyse700a_2x2_f); //big font
}
void fsmall() {
u8x8.setFont(u8x8_font_amstrad_cpc_extended_f); //small font
}
void prepdisplay() { // names and units only, static
fsmall();
u8x8.clearDisplay();
u8x8.setCursor(0,0);
u8x8.drawUTF8(0,0, "Temp °C ");
u8x8.setCursor(0,1);
u8x8.print("Press hPa");
u8x8.setCursor(0,2);
u8x8.print("Humid % ");
u8x8.setCursor(0,3);
u8x8.print("CO2 ppm");
u8x8.setCursor(0,4);
u8x8.print("TVOC ppb");
}
void valuesdisplay() { // values updates only, dynamic
dtostrf(avg_temp, 6, 1, rtemp);
dtostrf(avg_press, 6, 1, rpress);
dtostrf(avg_humid, 6, 1, rhumid);
dtostrf(avg_co2, 6, 0, rco2);
dtostrf(avg_tvoc, 6, 0, rtvoc);
fsmall();
u8x8.setCursor(6,0);
u8x8.print(rtemp);
u8x8.setCursor(6,1);
u8x8.print(rpress);
u8x8.setCursor(6,2);
u8x8.print(rhumid);
u8x8.setCursor(4,3);
if ( avg_co2 >400 ) { // BME can't go below 400
u8x8.print(rco2);
}
else {
u8x8.print(" min");
}
u8x8.setCursor(4,4);
u8x8.print(rtvoc);
u8x8.setCursor(0,6);
u8x8.print(text_humid);
u8x8.setCursor(0,7);
u8x8.print(text_co2);
}
void serial_out() {
if ( serialout == true ) {
Serial.print(F("Temperature = "));
Serial.print(temp);
Serial.println(" °C");
Serial.print(F("Pressure = "));
Serial.print(press);
Serial.println(" hPa ");
Serial.print("Humidity = ");
Serial.print(humid);
Serial.println(" %");
Serial.print("CO2 = ");
Serial.print(co2);
Serial.println(" ppm");
Serial.print("TVOC = ");
Serial.print(tvoc);
Serial.println(" ppb");
}
}
void readbme() {
temp = bme.readTemperature();
press = bme.readPressure()/100;
humid = bme.readHumidity();
}
void readccs() {
ccs.setEnvironmentalData(humid, temp); //to compensate
if(ccs.available()){
if(!ccs.readData()){
co2 = ccs.geteCO2(); //int
tvoc = ccs.getTVOC(); //int
}
else{
Serial.println("ccs ERROR!");
}
}
}
void floatingaverage() {
avg_temp = (ratio1*avg_temp)+(ratio2*temp);
avg_press = (ratio1*avg_press)+(ratio2*press);
avg_humid = (ratio1*avg_humid)+(ratio2*humid);
if ( co2 == 400 ) {
avg_co2 = 400;
}
avg_co2 = (ratio1*avg_co2)+(ratio2*co2);
avg_tvoc = (ratio1*avg_tvoc)+(ratio2*tvoc);
}
void serialhelp() {
Serial.println("available commands:");
Serial.println("exit - end the setup");
Serial.println("serialon - output actual values");
Serial.println("serialoff - don't output values");
Serial.println("displayon - start with oled on");
Serial.println("displayoff - start with oled off");
Serial.println("ledon - start with LEDs on");
Serial.println("ledoff - start with LEDs off");
Serial.println("rate1 - 1 second refresh");
Serial.println("rate2 - 2 seconds refresh");
Serial.println("rate5 - 5 seconds refresh");
Serial.println("rate10 - 10 seconds refresh");
Serial.println("ccsdrivemode1 - ccs reads every second");
Serial.println("ccsdrivemode10 - ccs reads every 10 seconds");
Serial.println("ccsdrivemode60 - ccs reads every 60 seconds");
Serial.println("list - list variables in use");
Serial.println("save - save variables to flash");
Serial.println("load - load variables from flash");
Serial.println("clear - clear flash");
Serial.println("help - this help");
}
void sack() {
Serial.println("OK");
}
void snack() {
Serial.println("unknown command");
}
void serialconfig() {
u8x8.clearDisplay();
u8x8.setPowerSave(0); // force display on
u8x8.setCursor(0,0);
u8x8.print("Serial port open");
u8x8.setCursor(0,1);
u8x8.print("waiting for conf");
u8x8.setCursor(0,2);
u8x8.print("baudrate ");
u8x8.setCursor(9,2);
u8x8.print(baudrate);
while ( true ) {
command = Serial.readStringUntil('\n');
command.trim();
u8x8.setCursor(0,4);
u8x8.clearLine(4);
u8x8.print(command);
if ( command != "" ) {
Serial.println(command);
}
if ( command == "serialon" ) {
serialout = true;
sack();
}
else if ( command == "serialoff" ) {
serialout = false;
sack();
}
else if ( command == "displayon" ) {
powersave = 0;
sack();
}
else if ( command == "displayoff" ) {
powersave = 1;
sack();
}
else if ( command == "ledon" ) {
ledon = true;
sack();
}
else if ( command == "ledoff" ) {
ledon = false;
sack();
}
else if ( command == "rate1" ) {
everysec = 1;
sack();
}
else if ( command == "rate2" ) {
everysec = 2;
sack();
}
else if ( command == "rate5" ) {
everysec = 5;
sack();
}
else if ( command == "rate10" ) {
everysec = 10;
sack();
}
else if ( command == "ccsdrivemode1" ) {
ccsdrivemode = 1;
setccsdrivemode();
sack();
}
else if ( command == "ccsdrivemode10" ) {
ccsdrivemode = 10;
setccsdrivemode();
sack();
}
else if ( command == "ccsdrivemode60" ) {
ccsdrivemode = 60;
setccsdrivemode();
sack();
}
else if ( command == "list" ) {
listvars();
sack();
}
else if ( command == "save" ) {
savevars();
sack();
}
else if ( command == "load" ) {
loadvars();
sack();
}
else if ( command == "clear" ) {
preferences.clear();
sack();
}
else if ( command == "help" ) {
serialhelp();
sack();
}
else if (( command == "exit" ) or ( digitalRead(keypin1) == true)) {
Serial.println("exit config");
//beep();
u8x8.setCursor(0,4);
u8x8.clearLine(4);
u8x8.print("exiting ...");
delay(2000);
prepdisplay();
return;
}
else if ( command != "" ) {
snack();
}
}
delay(1000); // leaving the serial mode
u8x8.clearDisplay();
u8x8.setPowerSave(powersave); // restore previous
prepdisplay();
}
void listvars() {
Serial.print("serial ");
Serial.println(serialout);
Serial.print("oledoff ");
Serial.println(powersave);
Serial.print("LED on ");
Serial.println(ledon);
Serial.print("rate ");
Serial.println(everysec);
Serial.print("ccsdrivemode ");
Serial.println(ccsdrivemode);
}
void savevars() {
preferences.putBool("serial", serialout);
preferences.putBool("powersave", powersave);
preferences.putBool("ledon", ledon);
preferences.putInt("rate", everysec);
preferences.putInt("ccsdrivemode", ccsdrivemode);
preferences.putBool("flashavailable", true); // read from flash available on boot
}
void loadvars() {
serialout = preferences.getBool("serial");
ledon = preferences.getBool("ledon");
everysec = preferences.getInt("rate");
ccsdrivemode = preferences.getInt("ccsdrivemode");
powersave = preferences.getBool("powersave");
}
void splash() {
u8x8.clearDisplay();
fsmall();
u8x8.setCursor(0,0);
u8x8.print("Initialize ...");
u8x8.setCursor(0,1);
u8x8.print("ver ");
u8x8.setCursor(4,1);
u8x8.print(version);
u8x8.setCursor(0,3);
u8x8.print("BME280");
u8x8.setCursor(0,4);
u8x8.print(" temp, pressure");
u8x8.setCursor(0,5);
u8x8.print(" humidity");
u8x8.setCursor(0,6);
u8x8.print("CCS811");
u8x8.setCursor(0,7);
u8x8.print(" CO2, TVOC");
}
void warnings() {
if ( avg_humid < humidl1 ) {
text_humid = "Humidity too low";
}
else if ( avg_humid < humidl2 ) {
text_humid = "Humidity low ";
}
else if ( avg_humid < humidl3 ) {
text_humid = " ";
}
else if ( avg_humid < humidl4 ) {
text_humid = "Humidity high ";
}
else if ( avg_humid >= humidl4 ) {
text_humid = "Humidity extreme";
}
else {
text_humid = "Humidity undef ";
}
if ( initcounter > 0 ) { // ccs not yet reliable, no messages
text_co2 = "CO2 warmup ";
text_co2.concat(initcounter);
text_co2 = text_co2 + " ";
initcounter --;
co2level = 5;
}
else if ( avg_co2 < co2l1 ) {
text_co2 = " ";
co2level = 0;
}
else if ( avg_co2 < co2l2 ) {
text_co2 = "CO2 high ";
co2level = 1;
}
else if ( avg_co2 < co2l3 ) {
text_co2 = "CO2 higher ";
co2level = 2;
}
else if ( avg_co2 < co2l4 ) {
text_co2 = "CO2 too high ";
co2level = 3;
}
else if ( avg_co2 >= co2l4 ) {
text_co2 = "CO2 extreme ";
co2level = 4;
}
else {
text_co2 = "CO2 undefined ";
}
}
void ledout() {
if ( ledon == false ) {
ledcWrite(ledgreenchannel, 0);
ledcWrite(ledyellowchannel, 0);
ledcWrite(ledredchannel, 0);
return;
}
if ( co2level == 0 ) {
ledcWrite(ledgreenchannel, ledgreenbright);
ledcWrite(ledyellowchannel, 0);
ledcWrite(ledredchannel, 0);
}
else if ( co2level == 1 ) {
ledcWrite(ledgreenchannel, ledgreenbright/2);
ledcWrite(ledyellowchannel, ledyellowbright/2);
ledcWrite(ledredchannel, 0);
}
else if ( co2level == 2 ) {
ledcWrite(ledgreenchannel, 0);
ledcWrite(ledyellowchannel, ledyellowbright);
ledcWrite(ledredchannel, 0);
}
else if ( co2level == 3 ) {
ledcWrite(ledgreenchannel, 0);
ledcWrite(ledyellowchannel, ledyellowbright/2);
ledcWrite(ledredchannel, ledredbright/2);
}
else if ( co2level == 4 ) {
ledcWrite(ledgreenchannel, 0);
ledcWrite(ledyellowchannel, 0);
ledcWrite(ledredchannel, ledredbright);
}
else {
ledcWrite(ledgreenchannel, ledgreenbright/30);
ledcWrite(ledyellowchannel, ledyellowbright/30);
ledcWrite(ledredchannel, ledredbright/30);
}
}
void checkkey() {
for (int count = 0; count < (everysec * 100 - 5); count++) { //overall timing without using interrupts
key1 = digitalRead(keypin1);
key2 = digitalRead(keypin2);
if ( key1 == true) {
keyduration1++;
if ( keyduration1 > ( 100 * longkey )) {
Serial.println("enter config, try help");
powersave = !powersave;
serialconfig();
lastkey1 = 0;
key1 = 0;
}
}
else {
keyduration1 = 0;
}
if (key1 != lastkey1) { //key change?
if ( key1 > lastkey1 ) { //toggle only when key down
powersave = !powersave;
//beep();
}
lastkey1 = key1;
u8x8.setPowerSave(powersave);
}
if ( key2 == true) {
keyduration2++;
if ( keyduration2 > ( 100 * longkey )) {
Serial.println("key2 long press, not implemented");
ledon = !ledon;
lastkey2 = 0;
key2 = 0;
}
}
else {
keyduration2 = 0;
}
if (key2 != lastkey2) { //key change?
if ( key2 > lastkey2 ) { //toggle only when key down
ledon = !ledon;
ledout();
//beep();
}
lastkey2 = key2;
}
delay(10);
}
}
void loop() {
readbme(); // read bme sensor
readccs(); // read ccs sensor
floatingaverage(); // smooth value changes
warnings(); // evaluate values
serial_out(); // serial values out
ledout(); // set the LEDs
valuesdisplay(); // update the oled display
checkkey(); // check keys, pause for next cycle
}