Make things async and allow for backlight toggle
This commit is contained in:
@@ -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() {
|
||||
unsigned long currentMillis = millis();
|
||||
|
||||
if (currentMillis - previousMillis >= interval) {
|
||||
previousMillis = currentMillis;
|
||||
StaticJsonDocument<100> jsonBuffer;
|
||||
|
||||
float temperature = avg("temp");
|
||||
float pressure = avg("pres");
|
||||
float humidity = avg("humi");
|
||||
|
||||
// Don't print to LCD if backlight is off
|
||||
if (led == 1) {
|
||||
printLCD(temperature, humidity, pressure);
|
||||
}
|
||||
|
||||
// Get data and send it over serial
|
||||
jsonBuffer["temperature"] = temperature;
|
||||
jsonBuffer["humidity"] = humidity;
|
||||
jsonBuffer["pressure"] = pressure;
|
||||
|
||||
serializeJson(jsonBuffer, Serial);
|
||||
Serial.println();
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user