-
Notifications
You must be signed in to change notification settings - Fork 0
/
take_measurement.rs
64 lines (61 loc) · 1.52 KB
/
take_measurement.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::time::{Duration, SystemTime};
use rusqlite::{params, Connection};
fn main() {
let args = std::env::args().collect::<Vec<_>>();
if args.len() < 2 {
panic!("missing argument: database path");
}
let database = Connection::open(&args[1]).expect("failed to open database");
database
.execute(
"
CREATE TABLE IF NOT EXISTS sensor_readings(
time INTEGER PRIMARY KEY,
humidity INTEGER NOT NULL,
celsius INTEGER NOT NULL
)",
[],
)
.unwrap();
let mut attempts = 0;
let mut temps = vec![];
let mut rhs = vec![];
let time = std::time::SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
while temps.len() < 5 && attempts < 10 {
if let Ok((rh, temp)) = raspi_oled::am2302_reading() {
// TODO: try out gpio_am2302_rs!
//if let Ok(reading) = gpio_am2302_rs::try_read(26) {
//let rh = reading.humidity as i64;
//let temp = (reading.temperature * 10.0) as i64;
if rh > 0 && temp < 500 {
rhs.push(rh);
temps.push(temp);
}
}
std::thread::sleep(Duration::from_secs(5));
attempts += 1;
}
if !temps.is_empty() {
// median = hopefully no faulty readings
temps.sort();
rhs.sort();
let rh = rhs[rhs.len() / 2];
let temp = temps[temps.len() / 2];
println!(
"info: acquired {} readings (temps {:?}, rhs {:?}), using rh {} and temp {}",
temps.len(),
temps,
rhs,
rh,
temp
);
database
.execute(
"INSERT INTO sensor_readings (time, humidity, celsius) VALUES (?1, ?2, ?3)",
params![time.as_secs(), rh, temp],
)
.unwrap();
}
}