136 lines
2.9 KiB
C++
136 lines
2.9 KiB
C++
#include <Arduino.h>
|
|
#include "MHZ19.h"
|
|
#include "Adafruit_AHTX0.h"
|
|
#include "LiquidCrystal_I2C.h"
|
|
|
|
MHZ19 myMHZ19;
|
|
Adafruit_AHTX0 aht;
|
|
LiquidCrystal_I2C LCD(0x27,2,1,0,4,5,6,7,3,POSITIVE);
|
|
|
|
#define PIN_LED_GREEN 25
|
|
#define PIN_LED_YELLOW 33
|
|
#define PIN_LED_RED 32
|
|
|
|
int backlightState = HIGH;
|
|
|
|
volatile bool interruptFired = false;
|
|
unsigned long lastInterrupt = 0;
|
|
unsigned long lastUpdated = 0;
|
|
|
|
unsigned const int updateTimer = 5000;
|
|
unsigned const int interruptTimer = 500;
|
|
|
|
int ready = 0;
|
|
int cal_time = 5; //minutes
|
|
|
|
void led(int red, int green, int yellow)
|
|
{
|
|
analogWrite(PIN_LED_RED, red);
|
|
analogWrite(PIN_LED_GREEN, green);
|
|
analogWrite(PIN_LED_YELLOW, yellow);
|
|
}
|
|
|
|
void IRAM_ATTR BacklightToggle() {
|
|
if (!interruptFired)
|
|
interruptFired = true;
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
Serial2.begin(9600, SERIAL_8N1, 16, 17);
|
|
|
|
myMHZ19.begin(Serial2);
|
|
myMHZ19.autoCalibration();
|
|
myMHZ19.setRange(2000);
|
|
|
|
Serial.println("MHZ19 init:");
|
|
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.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");
|
|
|
|
if (!aht.begin()) {
|
|
Serial.println("Could not find AHT? Check wiring");
|
|
while (true);
|
|
}
|
|
|
|
LCD.begin(16, 2);
|
|
LCD.clear();
|
|
LCD.setBacklight(backlightState);
|
|
pinMode(23, INPUT_PULLUP);
|
|
attachInterrupt(23, BacklightToggle, FALLING);
|
|
}
|
|
|
|
void loop() {
|
|
if (interruptFired) {
|
|
if (millis() - lastInterrupt > interruptTimer || lastInterrupt == 0) {
|
|
backlightState = !backlightState;
|
|
Serial.printf("Setting backlight to %i \n", backlightState);
|
|
LCD.setBacklight(backlightState);
|
|
lastInterrupt = millis();
|
|
}
|
|
|
|
interruptFired = false;
|
|
}
|
|
|
|
if (millis() - lastUpdated > updateTimer || lastUpdated == 0) {
|
|
lastUpdated = millis();
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
sensors_event_t humidity, temp;
|
|
aht.getEvent(&humidity, &temp);
|
|
LCD.setCursor(0, 0);
|
|
LCD.print(temp.temperature, 1);
|
|
LCD.print(" C ");
|
|
LCD.setCursor(9, 0);
|
|
LCD.print(humidity.relative_humidity, 1);
|
|
LCD.print("%rH");
|
|
|
|
if (millis() >= (cal_time * 60000) && !ready) {
|
|
LCD.setCursor(0, 1);
|
|
LCD.print(" Ready.");
|
|
ready = true;
|
|
}
|
|
|
|
int co2 = myMHZ19.getCO2(false);
|
|
if (myMHZ19.errorCode != RESULT_OK) {
|
|
Serial.print("Error. Response Code: ");
|
|
Serial.println(myMHZ19.errorCode);
|
|
return;
|
|
}
|
|
LCD.setCursor(0, 1);
|
|
LCD.print(co2);
|
|
LCD.print("ppm ");
|
|
|
|
if (co2 < 1000)
|
|
led(0, 1, 0);
|
|
else if (co2 < 1500)
|
|
led(0, 0, 5);
|
|
else
|
|
led(5, 0, 0);
|
|
|
|
if (millis() < (cal_time * 60000)) {
|
|
{
|
|
LCD.setCursor(8, 1);
|
|
LCD.printf("Cal..%2lum", ((cal_time * 60000) - millis()) / 60000);
|
|
}
|
|
}
|
|
}
|