From d8b6ffec7e69b75eedd3e3d975a1afabe31d43d6 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Sat, 9 Oct 2021 12:07:21 +0200 Subject: [PATCH] Sanity-check sensor readings, display time --- src/main.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 602ca9e..ace539e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,17 +135,19 @@ fn main() { let mut chip = Chip::new("/dev/gpiochip0").unwrap(); let line = chip.get_line(26).unwrap(); for _attempt in 0..5 { - let time = std::time::SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); + let time = std::time::SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); if let Ok((rh, temp)) = raspi_oled::am2302_reading(&line) { - database.execute("INSERT INTO sensor_readings (time, humidity, celsius) VALUES (?1, ?2, ?3)", params![time, rh, temp]).unwrap(); - display_on_ssd1306(rh, temp); - break; + if rh > 0 && temp < 500 { + database.execute("INSERT INTO sensor_readings (time, humidity, celsius) VALUES (?1, ?2, ?3)", params![time.as_secs(), rh, temp]).unwrap(); + display_on_ssd1306(rh, temp, time); + break; + } } } } -fn display_on_ssd1306(rh: u16, temp: u16) { +fn display_on_ssd1306(rh: u16, temp: u16, time: Duration) { let i2c = I2cdev::new("/dev/i2c-1").unwrap(); let interface = I2CDisplayInterface::new(i2c); let mut disp = Ssd1306::new( @@ -165,6 +167,11 @@ fn display_on_ssd1306(rh: u16, temp: u16) { Text::new(&text, Point::new(0, 10), text_style) .draw(&mut disp) .unwrap(); + let secs = time.as_secs(); + let time = format!("{:02}:{:02} Uhr", (secs / 3600 + 2) % 24, secs / 60 % 60); + Text::new(&time, Point::new(0, 32), text_style) + .draw(&mut disp) + .unwrap(); disp.flush().unwrap(); /* sleep(Duration::from_secs(2));