Make things async and allow for backlight toggle

This commit is contained in:
Nik Rozman
2022-10-24 23:14:34 +02:00
parent 9fc44c7337
commit adcb8f4e9e

View File

@@ -6,33 +6,18 @@
LiquidCrystal_I2C lcd(0x27, 16, 2); LiquidCrystal_I2C lcd(0x27, 16, 2);
static TEWeatherShield weatherShield; static TEWeatherShield weatherShield;
int command = 0;
int led = 1;
bool mode = true;
unsigned long previousMillis = 0;
const long interval = 10000;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
weatherShield.begin(); weatherShield.begin();
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
lcd.println("Vremenska postaja");
lcd.clear();
}
// Print temperature and humidity to LCD
void printLCD(float temperature, float humidity, float pressure) {
lcd.clear();
String temp = "T: " + (String)temperature + " C";
lcd.setCursor(2, 0);
lcd.print(temp);
String hum = "H: " + (String)humidity + " %RH";
lcd.setCursor(2, 1);
lcd.print(hum);
delay(10000);
String pres = "P: " + (String)pressure + " hPa";
lcd.setCursor(2, 1);
lcd.print(pres);
} }
// Measure from HTU21D // Measure from HTU21D
@@ -143,6 +128,27 @@ float measure_tsd305(String data) {
} }
} }
// Print temperature and humidity to LCD
void printLCD(float temperature, float humidity, float pressure) {
lcd.clear();
String temp = "T: " + (String)temperature + " C";
lcd.setCursor(2, 0);
lcd.print(temp);
if (mode == true) {
String hum = "H: " + (String)humidity + " %RH";
lcd.setCursor(2, 1);
lcd.print(hum);
mode = false;
} else {
String pres = "P: " + (String)pressure + " hPa";
lcd.setCursor(2, 1);
lcd.print(pres);
mode = true;
}
}
float avg(String data) { float avg(String data) {
if (data == "temp") { if (data == "temp") {
float avgTemp = (measure_htu21d("temp") + measure_ms5637("temp") + measure_ms8607("temp") + measure_tsys01("temp") + measure_tsd305("temp")) / 5; float avgTemp = (measure_htu21d("temp") + measure_ms5637("temp") + measure_ms8607("temp") + measure_tsys01("temp") + measure_tsd305("temp")) / 5;
@@ -156,21 +162,47 @@ float avg(String data) {
} }
} }
void toggleLed() {
if (led == 0) {
lcd.setBacklight(HIGH);
led = 1;
} else {
lcd.setBacklight(LOW);
led = 0;
}
}
void loop() { void loop() {
StaticJsonDocument<100> jsonBuffer; unsigned long currentMillis = millis();
float temperature = avg("temp");
float pressure = avg("pres");
float humidity = avg("humi");
printLCD(temperature, humidity, pressure); if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
StaticJsonDocument<100> jsonBuffer;
jsonBuffer["temperature"] = temperature; float temperature = avg("temp");
jsonBuffer["humidity"] = humidity; float pressure = avg("pres");
jsonBuffer["pressure"] = pressure; float humidity = avg("humi");
serializeJson(jsonBuffer, Serial); // Don't print to LCD if backlight is off
Serial.println(); if (led == 1) {
printLCD(temperature, humidity, pressure);
}
delay(2000); // Get data and send it over serial
jsonBuffer["temperature"] = temperature;
jsonBuffer["humidity"] = humidity;
jsonBuffer["pressure"] = pressure;
serializeJson(jsonBuffer, Serial);
Serial.println();
}
Serial.flush();
command = Serial.read();
// 84 = T
if (command == 84) {
toggleLed();
//Serial.println("LED toggled");
//Make this return some JSON state of the LED to Home Assistant
}
} }