From adcb8f4e9e03e710a64c6edb4a42f684bb635d37 Mon Sep 17 00:00:00 2001 From: Nik Rozman Date: Mon, 24 Oct 2022 23:14:34 +0200 Subject: [PATCH] Make things async and allow for backlight toggle --- vremenska-postaja.ino | 98 ++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/vremenska-postaja.ino b/vremenska-postaja.ino index 4118e17..426eadc 100644 --- a/vremenska-postaja.ino +++ b/vremenska-postaja.ino @@ -6,33 +6,18 @@ LiquidCrystal_I2C lcd(0x27, 16, 2); static TEWeatherShield weatherShield; +int command = 0; +int led = 1; +bool mode = true; +unsigned long previousMillis = 0; +const long interval = 10000; + void setup() { Serial.begin(9600); weatherShield.begin(); lcd.init(); 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 @@ -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) { if (data == "temp") { 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() { - StaticJsonDocument<100> jsonBuffer; - - float temperature = avg("temp"); - float pressure = avg("pres"); - float humidity = avg("humi"); + unsigned long currentMillis = millis(); - printLCD(temperature, humidity, pressure); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + StaticJsonDocument<100> jsonBuffer; - jsonBuffer["temperature"] = temperature; - jsonBuffer["humidity"] = humidity; - jsonBuffer["pressure"] = pressure; + float temperature = avg("temp"); + float pressure = avg("pres"); + float humidity = avg("humi"); - serializeJson(jsonBuffer, Serial); - Serial.println(); + // Don't print to LCD if backlight is off + 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 + } } \ No newline at end of file