This commit is contained in:
Michael Biermann 2020-11-23 23:52:15 +01:00
commit 2d6464fa7d
3 changed files with 78 additions and 1670 deletions

File diff suppressed because it is too large Load Diff

View File

@ -17,3 +17,4 @@ lib_deps =
adafruit/Adafruit NeoPixel@^1.6.0
wifwaf/MH-Z19@^1.5.2
squix78/ESP8266_SSD1306@^4.1.0
monitor_speed = 9600

View File

@ -2,13 +2,15 @@
#include "MHZ19.h"
#include "SSD1306Wire.h"
#include <Adafruit_NeoPixel.h>
#include "fonts-custom.h"
#include <Preferences.h>
// Maximum CO² levels for green and yellow, everything above is considered red.
#define GREEN_CO2 800
#define YELLOW_CO2 1000
// Measurement interval in miliseconds
#define INTERVAL 60000
#define INTERVAL 15000
// Pins for MH-Z19
#define RX_PIN 16
@ -24,6 +26,8 @@
// number of LEDs connected
#define NUMPIXELS 8
Preferences preferences;
MHZ19 myMHZ19;
HardwareSerial mySerial(1);
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);
@ -32,19 +36,76 @@ Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LED_PIN, NEO_RGB + NEO_K
unsigned long getDataTimer = 0;
int lastvals[120];
int dheight;
String ampelversion = "0.1";
int safezone = 1;
int tocalibrateornot;
void setup() {
Serial.begin(9600);
Serial.println("boot...");
preferences.begin("co2", false);
tocalibrateornot = preferences.getUInt("cal",69); // wir lesen unser flag ein, 23 = reboot vor safezone, wir wollen kalibrieren, 42 = reboot nach safezone, wir tun nichts
preferences.putUInt("cal", 23); // wir sind gerade gestartet
Serial.println(ampelversion);
mySerial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
myMHZ19.begin(mySerial);
pixels.clear();
display.init();
display.setFont(Cousine_Regular_54);
display.setContrast(255);
delay(1000);
delay(500);
display.clear();
display.flipScreenVertically();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64 ,0 , String(ampelversion));
display.display();
dheight = display.getHeight();
myMHZ19.autoCalibration();
// myMHZ19.autoCalibration();
Serial.print("read EEPROM value: ");
Serial.println(tocalibrateornot);
if (tocalibrateornot == 23){
Serial.println("brace yourself, calibration starting! things either be better or all fucked up beyond this point...");
display.drawString(64, 20, "CAL!");
display.display();
for(int i=60; i > 0; i--){
display.clear();
display.drawString(64, 0, String(i));
display.display();
delay(1000);
}
myMHZ19.setRange(5000);
delay(500);
myMHZ19.calibrateZero();
delay(500);
myMHZ19.autoCalibration(false);
delay(500);
display.clear();
display.drawString(64, 0, "DONE");
display.display();
char myVersion[4];
myMHZ19.getVersion(myVersion);
Serial.print("\nFirmware Version: ");
for(byte i = 0; i < 4; i++)
{
Serial.print(myVersion[i]);
if(i == 1)
Serial.print(".");
}
Serial.println("");
Serial.print("Range: ");
Serial.println(myMHZ19.getRange());
Serial.print("Background CO2: ");
Serial.println(myMHZ19.getBackgroundCO2());
Serial.print("Temperature Cal: ");
Serial.println(myMHZ19.getTempAdjustment());
Serial.println("calibration done");
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");
}
else if (tocalibrateornot ==42){
Serial.println("fake news, nobody has the intention to do calibration....");
}
// Fill array of last measurements with -1
for (int x = 0; x <= 119; x = x + 1) {
lastvals[x] = -1;
@ -82,6 +143,14 @@ void set_led_color(int co2) {
}
void loop() {
if (safezone == 1){
if (millis() == 10000) {
Serial.println(" safe zone, sensor stayed up longer than 10s");
preferences.putUInt("cal", 42); // wir haben die safe zone erreicht, beim naechsten boot nicht kalibrieren!
safezone = 0;
}
}
if (millis() - getDataTimer >= INTERVAL) {
// Get new CO² value.
int CO2 = myMHZ19.getCO2();
@ -111,7 +180,11 @@ void loop() {
display.display();
// Debug output
Serial.print("CO2 (ppm): ");
Serial.println(CO2);
Serial.print(CO2);
Serial.print(" Background CO2: ");
Serial.print(myMHZ19.getBackgroundCO2());
Serial.print(" uptime (seconds): ");
Serial.println(millis()/1000);
getDataTimer = millis();
}
}