2020-10-31 16:06:36 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "MHZ19.h"
|
|
|
|
#include "SSD1306Wire.h"
|
|
|
|
#include <Adafruit_NeoPixel.h>
|
2020-11-19 13:13:10 +01:00
|
|
|
#include "fonts-custom.h"
|
2020-11-23 22:50:05 +01:00
|
|
|
#include <Preferences.h>
|
2020-11-25 21:33:28 +01:00
|
|
|
#include "uptime_formatter.h"
|
2020-10-31 16:06:36 +01:00
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Grenzwerte für die CO2 Werte für grün und gelb, alles überhalb davon bedeutet rot
|
2020-10-31 16:06:36 +01:00
|
|
|
#define GREEN_CO2 800
|
2020-11-12 19:46:23 +01:00
|
|
|
#define YELLOW_CO2 1000
|
2020-10-31 16:06:36 +01:00
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// CO2 Messintervall in Milisekunden
|
|
|
|
#define INTERVAL 15*1000
|
|
|
|
// Dauer der Kalibrierungsphase in Milisekunden
|
|
|
|
#define CALINTERVAL 180*1000
|
2020-10-31 16:06:36 +01:00
|
|
|
|
2020-11-26 17:35:41 +01:00
|
|
|
// Boot-Mode Konstanten
|
|
|
|
#define BOOT_NORMAL 42
|
|
|
|
#define BOOT_CALIBRATE 23
|
|
|
|
#define BOOT_UNKNOWN 63
|
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Pins für den MH-Z19b
|
2020-10-31 16:06:36 +01:00
|
|
|
#define RX_PIN 16
|
|
|
|
#define TX_PIN 17
|
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Pins für das SD1306 OLED-Display
|
2020-10-31 16:06:36 +01:00
|
|
|
#define SDA_PIN 21
|
|
|
|
#define SCL_PIN 22
|
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Pin für den LED-Ring
|
2020-10-31 16:06:36 +01:00
|
|
|
#define LED_PIN 4
|
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Anzahl der angeschlossenen LEDs am Ring
|
|
|
|
#define NUMPIXELS 8
|
2020-11-12 19:46:23 +01:00
|
|
|
|
2020-11-23 22:50:05 +01:00
|
|
|
Preferences preferences;
|
2020-10-31 16:06:36 +01:00
|
|
|
MHZ19 myMHZ19;
|
|
|
|
HardwareSerial mySerial(1);
|
|
|
|
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);
|
2020-11-29 18:25:14 +01:00
|
|
|
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
|
2020-10-31 16:06:36 +01:00
|
|
|
|
2020-11-26 17:35:41 +01:00
|
|
|
String ampelversion = "0.12";
|
2020-10-31 16:06:36 +01:00
|
|
|
unsigned long getDataTimer = 0;
|
2020-11-26 21:12:48 +01:00
|
|
|
unsigned long calibrationStart = 0;
|
2020-11-25 23:22:48 +01:00
|
|
|
int countdown = 0;
|
2020-10-31 16:06:36 +01:00
|
|
|
int lastvals[120];
|
|
|
|
int dheight;
|
2020-11-26 17:35:41 +01:00
|
|
|
int safezone = false;
|
2020-11-26 19:02:56 +01:00
|
|
|
int currentBootMode;
|
2020-11-24 01:19:10 +01:00
|
|
|
|
2020-11-26 19:02:56 +01:00
|
|
|
void setBootMode(int bootMode) {
|
|
|
|
if(bootMode == BOOT_NORMAL) {
|
|
|
|
Serial.println("Startmodus nächster Reboot: Messmodus");
|
|
|
|
preferences.putUInt("cal", bootMode);
|
|
|
|
}
|
|
|
|
else if(bootMode == BOOT_CALIBRATE) {
|
|
|
|
Serial.println("Startmodus nächster Reboot: Kalibrierungsmodus");
|
|
|
|
preferences.putUInt("cal", bootMode);
|
|
|
|
} else {
|
|
|
|
Serial.println("Unerwarteter Boot-Mode soll gespeichert werden. Abgebrochen.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggleBootMode(int bootMode) {
|
|
|
|
switch (bootMode){
|
2020-11-26 17:35:41 +01:00
|
|
|
case BOOT_CALIBRATE:
|
2020-11-26 19:02:56 +01:00
|
|
|
setBootMode(BOOT_NORMAL); break;
|
2020-11-26 17:35:41 +01:00
|
|
|
case BOOT_NORMAL:
|
2020-11-26 19:02:56 +01:00
|
|
|
setBootMode(BOOT_CALIBRATE); break;
|
2020-11-26 17:35:41 +01:00
|
|
|
case BOOT_UNKNOWN:
|
2020-11-26 19:02:56 +01:00
|
|
|
Serial.println("Bootmode Unbekannt! Neue Ampel? Nächster Start wird Messmodus.");
|
|
|
|
setBootMode(BOOT_NORMAL); break;
|
2020-11-25 21:33:28 +01:00
|
|
|
default:
|
2020-11-26 19:02:56 +01:00
|
|
|
Serial.print("Unerwarteter Bootmode-Wert: "); Serial.println(bootMode);
|
|
|
|
Serial.println("Nächster Start wird Messmodus.");
|
|
|
|
setBootMode(BOOT_NORMAL); break;
|
2020-11-25 21:33:28 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 22:50:05 +01:00
|
|
|
|
2020-10-31 16:06:36 +01:00
|
|
|
void setup() {
|
2020-11-25 21:33:28 +01:00
|
|
|
Serial.begin(115200);
|
2020-11-26 15:52:07 +01:00
|
|
|
Serial.println("Starte...");
|
|
|
|
Serial.print("CO2-Ampel Firmware: ");Serial.println(ampelversion);
|
2020-11-26 16:07:29 +01:00
|
|
|
|
|
|
|
// Ab hier Bootmodus initialisieren und festlegen
|
2020-11-23 22:50:05 +01:00
|
|
|
preferences.begin("co2", false);
|
2020-11-26 19:02:56 +01:00
|
|
|
currentBootMode = preferences.getUInt("cal", BOOT_UNKNOWN); // Aktuellen Boot-Mode lesen und speichern
|
2020-11-26 21:12:48 +01:00
|
|
|
|
2020-11-26 19:02:56 +01:00
|
|
|
switch(currentBootMode){
|
2020-11-26 17:35:41 +01:00
|
|
|
case BOOT_CALIBRATE:
|
2020-11-26 16:07:29 +01:00
|
|
|
Serial.println("Startmodus Aktuell: Kalibrierungsmodus");
|
2020-11-26 21:12:48 +01:00
|
|
|
toggleBootMode(currentBootMode); // beim nächsten boot ggfs. im anderen modus starten, wird später nach 10 Sekunden zurückgesetzt
|
2020-11-26 16:07:29 +01:00
|
|
|
break;
|
2020-11-26 17:35:41 +01:00
|
|
|
case BOOT_NORMAL:
|
2020-11-26 16:07:29 +01:00
|
|
|
Serial.println("Startmodus Aktuell: Messmodus");
|
2020-11-26 21:12:48 +01:00
|
|
|
toggleBootMode(currentBootMode); // beim nächsten boot ggfs. im anderen modus starten, wird später nach 10 Sekunden zurückgesetzt
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial.println("Startmodus Aktuell: Unbekannt oder Ungültig");
|
|
|
|
Serial.println("Nächster Start im Messmodus");
|
|
|
|
setBootMode(BOOT_NORMAL);
|
2020-11-26 16:07:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ab hier Display einrichten
|
2020-10-31 16:06:36 +01:00
|
|
|
display.init();
|
2020-11-23 23:50:36 +01:00
|
|
|
display.setFont(Cousine_Regular_54);
|
2020-10-31 16:06:36 +01:00
|
|
|
display.setContrast(255);
|
2020-11-23 22:50:05 +01:00
|
|
|
delay(500);
|
2020-10-31 16:06:36 +01:00
|
|
|
display.clear();
|
2020-11-12 19:46:23 +01:00
|
|
|
display.flipScreenVertically();
|
2020-11-23 22:50:05 +01:00
|
|
|
display.setTextAlignment(TEXT_ALIGN_CENTER);
|
|
|
|
display.drawString(64 ,0 , String(ampelversion));
|
|
|
|
display.display();
|
2020-10-31 16:06:36 +01:00
|
|
|
dheight = display.getHeight();
|
2020-11-25 21:33:28 +01:00
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Ab hier Sensor einrichten
|
2020-11-26 15:52:07 +01:00
|
|
|
mySerial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
|
|
|
|
myMHZ19.begin(mySerial);
|
2020-11-26 16:07:29 +01:00
|
|
|
myMHZ19.autoCalibration(false); // "Automatic Baseline Calibration" (ABC) erstmal aus
|
2020-11-25 21:33:28 +01:00
|
|
|
char myVersion[4];
|
|
|
|
myMHZ19.getVersion(myVersion);
|
2020-11-26 15:52:07 +01:00
|
|
|
Serial.print("\nMH-Z19b Firmware Version: ");
|
|
|
|
Serial.print(myVersion[0]);Serial.print(myVersion[1]);;Serial.print(".");Serial.print(myVersion[2]);Serial.println(myVersion[3]);
|
|
|
|
Serial.print("Range: "); Serial.println(myMHZ19.getRange());
|
|
|
|
Serial.print("Background CO2: "); Serial.println(myMHZ19.getBackgroundCO2());
|
|
|
|
Serial.print("Temperature Cal: "); Serial.println(myMHZ19.getTempAdjustment());
|
2020-11-25 21:33:28 +01:00
|
|
|
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");
|
2020-11-26 19:02:56 +01:00
|
|
|
Serial.print("read EEPROM value: "); Serial.println(currentBootMode);
|
2020-11-24 01:19:10 +01:00
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Liste der Messwerte mit "-1" befüllen ("-1" wird beinm Graph nicht gezeichnet)
|
2020-10-31 16:06:36 +01:00
|
|
|
for (int x = 0; x <= 119; x = x + 1) {
|
|
|
|
lastvals[x] = -1;
|
|
|
|
}
|
2020-11-12 19:46:23 +01:00
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Ab hier LED-Ring konfigurien
|
2020-10-31 16:06:36 +01:00
|
|
|
pixels.begin();
|
2020-11-26 15:52:07 +01:00
|
|
|
pixels.clear();
|
2020-11-26 16:13:16 +01:00
|
|
|
pixels.fill(pixels.Color(0,0,0));
|
|
|
|
pixels.show();
|
|
|
|
|
2020-10-31 16:06:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int calc_vpos_for_co2(int co2val, int display_height) {
|
|
|
|
return display_height - int((float(display_height) / 3000) * co2val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_led_color(int co2) {
|
|
|
|
if (co2 < GREEN_CO2) {
|
2020-11-29 18:25:14 +01:00
|
|
|
pixels.fill(pixels.Color(0,0,0)); // Grün
|
|
|
|
pixels.setPixelColor(4,pixels.Color(0,2,0));
|
2020-10-31 16:06:36 +01:00
|
|
|
} else if (co2 < YELLOW_CO2) {
|
2020-11-29 18:25:14 +01:00
|
|
|
pixels.fill(pixels.Color(40,30,0)); // Gelb
|
2020-10-31 16:06:36 +01:00
|
|
|
} else {
|
2020-11-26 21:19:38 +01:00
|
|
|
pixels.fill(pixels.Color(90,0,0)); // Rot
|
2020-10-31 16:06:36 +01:00
|
|
|
}
|
|
|
|
pixels.show();
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:50:05 +01:00
|
|
|
|
2020-11-24 01:19:10 +01:00
|
|
|
void rainbow(int wait) {
|
|
|
|
for(long firstPixelHue = 0; firstPixelHue < 65536; firstPixelHue += 256) {
|
|
|
|
for(int i=0; i<NUMPIXELS; i++) {
|
|
|
|
int pixelHue = firstPixelHue + (i * 65536L / NUMPIXELS);
|
|
|
|
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
|
|
|
|
}
|
|
|
|
pixels.show();
|
|
|
|
delay(wait);
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 21:33:28 +01:00
|
|
|
|
2020-11-26 16:33:32 +01:00
|
|
|
void calibrateCO2() {
|
|
|
|
display.setFont(ArialMT_Plain_24);
|
|
|
|
display.clear(); display.drawString(64, 0, "Kalibriere!"); display.display();
|
|
|
|
Serial.println("Kalibrierung startet nun");
|
|
|
|
|
|
|
|
myMHZ19.setRange(5000);
|
|
|
|
delay(500);
|
|
|
|
myMHZ19.calibrateZero();
|
|
|
|
delay(500);
|
|
|
|
myMHZ19.autoCalibration(false);
|
|
|
|
delay(500);
|
|
|
|
|
|
|
|
display.clear(); display.drawString(64, 0, "Fertig!"); display.display();
|
2020-11-26 17:35:41 +01:00
|
|
|
preferences.putUInt("cal", BOOT_NORMAL);
|
2020-11-26 16:33:32 +01:00
|
|
|
delay(2000);
|
|
|
|
|
|
|
|
display.clear(); display.setFont(Cousine_Regular_54);
|
2020-11-25 21:33:28 +01:00
|
|
|
}
|
2020-11-26 16:33:32 +01:00
|
|
|
|
|
|
|
void readCO2(){
|
2020-10-31 16:06:36 +01:00
|
|
|
if (millis() - getDataTimer >= INTERVAL) {
|
2020-11-26 16:07:29 +01:00
|
|
|
// Neuen CO2 Wert lesen
|
2020-10-31 16:06:36 +01:00
|
|
|
int CO2 = myMHZ19.getCO2();
|
2020-11-26 16:07:29 +01:00
|
|
|
// Alle Werte in der Messwertliste um eins verschieben
|
2020-10-31 16:06:36 +01:00
|
|
|
for (int x = 1; x <= 119; x = x + 1) {
|
|
|
|
lastvals[x - 1] = lastvals[x];
|
|
|
|
}
|
2020-11-26 16:07:29 +01:00
|
|
|
// Aktuellen Messer am Ende einfügen
|
2020-10-31 16:06:36 +01:00
|
|
|
lastvals[119] = CO2;
|
2020-11-26 16:07:29 +01:00
|
|
|
// Display löschen und alles neu schreiben/zeichnen
|
2020-10-31 16:06:36 +01:00
|
|
|
display.clear();
|
|
|
|
for (int h = 1; h < 120; h = h + 1) {
|
|
|
|
int curval = lastvals[h];
|
|
|
|
if (curval > 0) {
|
|
|
|
int vpos = calc_vpos_for_co2(lastvals[h], dheight);
|
|
|
|
int vpos_last = calc_vpos_for_co2(lastvals[h - 1], dheight);
|
|
|
|
display.drawLine(h - 1, vpos_last, h, vpos);
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 16:07:29 +01:00
|
|
|
// Farbe des LED-Rings setzen
|
2020-11-26 19:02:56 +01:00
|
|
|
if (currentBootMode == BOOT_NORMAL) { set_led_color(CO2); }
|
2020-11-26 21:12:48 +01:00
|
|
|
|
|
|
|
// Aktuellen CO2 Wert ausgeben
|
2020-11-25 21:33:28 +01:00
|
|
|
//display.setLogBuffer(1, 30);
|
2020-11-12 19:46:23 +01:00
|
|
|
display.setFont(Cousine_Regular_54);
|
|
|
|
display.setTextAlignment(TEXT_ALIGN_CENTER);
|
|
|
|
display.drawString(64 ,0 , String(CO2));
|
2020-11-25 21:33:28 +01:00
|
|
|
//display.drawLogBuffer(0, 0);
|
2020-10-31 16:06:36 +01:00
|
|
|
display.display();
|
2020-11-26 21:12:48 +01:00
|
|
|
|
2020-11-26 16:07:29 +01:00
|
|
|
// Ein wenig Debug-Ausgabe
|
2020-11-26 16:33:32 +01:00
|
|
|
Serial.print("Neue Messung - Aktueller CO2-Wert: ");
|
2020-11-19 13:13:10 +01:00
|
|
|
Serial.print(CO2);
|
2020-11-26 16:33:32 +01:00
|
|
|
Serial.print("; Background CO2: " + String(myMHZ19.getBackgroundCO2()));
|
|
|
|
Serial.print("; Temperatur: " + String(myMHZ19.getTemperature()) + " Temperature Adjustment: " + String(myMHZ19.getTempAdjustment()));
|
|
|
|
Serial.println("; uptime: " + uptime_formatter::getUptime());
|
2020-11-25 21:33:28 +01:00
|
|
|
|
2020-10-31 16:06:36 +01:00
|
|
|
getDataTimer = millis();
|
|
|
|
}
|
2020-11-12 19:46:23 +01:00
|
|
|
}
|
2020-11-24 01:19:10 +01:00
|
|
|
|
|
|
|
void loop() {
|
2020-11-26 21:12:48 +01:00
|
|
|
// Nur für die ersten 10 Sekunden wichtig,
|
2020-11-26 19:02:56 +01:00
|
|
|
if ( (!safezone) & (millis() > 10000) ) {
|
2020-11-26 21:12:48 +01:00
|
|
|
Serial.println("=== 10 Sekunden im Betrieb, nächster Boot im Messmodus ===");
|
2020-11-26 19:02:56 +01:00
|
|
|
setBootMode(BOOT_NORMAL); //
|
2020-11-26 17:35:41 +01:00
|
|
|
safezone = true;
|
|
|
|
}
|
2020-11-26 16:33:32 +01:00
|
|
|
|
2020-11-26 21:12:48 +01:00
|
|
|
if (safezone) {
|
2020-11-26 19:02:56 +01:00
|
|
|
if (currentBootMode == BOOT_CALIBRATE){
|
2020-11-26 21:12:48 +01:00
|
|
|
if (millis() - calibrationStart <= CALINTERVAL) {
|
2020-11-26 17:35:41 +01:00
|
|
|
rainbow(10);
|
|
|
|
display.clear();
|
|
|
|
display.setTextAlignment(TEXT_ALIGN_CENTER);
|
2020-11-26 21:12:48 +01:00
|
|
|
countdown = ((calibrationStart + CALINTERVAL) - millis()) / 1000;
|
2020-11-26 17:35:41 +01:00
|
|
|
Serial.println("Countdown: " + String(countdown));
|
|
|
|
display.drawString(64, 0, String(countdown));
|
|
|
|
display.display();
|
2020-11-24 01:19:10 +01:00
|
|
|
}
|
2020-11-26 21:12:48 +01:00
|
|
|
else if (millis() - calibrationStart >= CALINTERVAL) {
|
2020-11-26 17:35:41 +01:00
|
|
|
calibrateCO2();
|
2020-11-26 21:12:48 +01:00
|
|
|
calibrationStart = millis();
|
2020-11-24 01:19:10 +01:00
|
|
|
}
|
2020-11-26 16:33:32 +01:00
|
|
|
}
|
2020-11-24 01:19:10 +01:00
|
|
|
}
|
2020-11-25 21:33:28 +01:00
|
|
|
|
2020-11-26 16:33:32 +01:00
|
|
|
readCO2();
|
2020-11-24 01:19:10 +01:00
|
|
|
}
|
|
|
|
|