|
|
@@ -1,7 +1,7 @@
|
|
1
|
1
|
use std::convert::TryInto;
|
|
|
2
|
+use std::sync::mpsc;
|
|
2
|
3
|
use std::thread;
|
|
3
|
4
|
use std::time::{Duration, Instant};
|
|
4
|
|
-use std::sync::mpsc;
|
|
5
|
5
|
|
|
6
|
6
|
use embedded_nrf24l01::{Configuration, CrcMode, DataRate, NRF24L01};
|
|
7
|
7
|
use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
|
|
|
@@ -11,7 +11,7 @@ use log::{error, info};
|
|
11
|
11
|
|
|
12
|
12
|
use crate::spi::EmbeddedHalSpidev;
|
|
13
|
13
|
use crate::Error;
|
|
14
|
|
-use crate::{SensorUpdate, SensorData};
|
|
|
14
|
+use crate::{SensorData, SensorUpdate};
|
|
15
|
15
|
|
|
16
|
16
|
pub fn start(updates: mpsc::Sender<SensorUpdate>) {
|
|
17
|
17
|
// TODO: Channels for sensor readings.
|
|
|
@@ -70,9 +70,7 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
70
|
70
|
nrf24.set_rf(DataRate::R2Mbps, 3)?;
|
|
71
|
71
|
nrf24.set_crc(Some(CrcMode::OneByte))?;
|
|
72
|
72
|
nrf24.set_auto_retransmit(250, 3)?;
|
|
73
|
|
- nrf24
|
|
74
|
|
- .set_pipes_rx_enable(&[true, false, false, false, false, false])
|
|
75
|
|
- ?;
|
|
|
73
|
+ nrf24.set_pipes_rx_enable(&[true, false, false, false, false, false])?;
|
|
76
|
74
|
nrf24
|
|
77
|
75
|
.set_rx_addr(0, &[0x56, 0x34, 0x12, 0x00, 0x00])
|
|
78
|
76
|
.unwrap();
|
|
|
@@ -92,20 +90,52 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
92
|
90
|
loop {
|
|
93
|
91
|
if let Some(pipe) = nrf24.can_read().unwrap() {
|
|
94
|
92
|
let payload = nrf24.read().unwrap();
|
|
95
|
|
- info!("packet received on pipe {}: {:?}, {}", pipe, payload.as_ref(), payload.len());
|
|
|
93
|
+ info!(
|
|
|
94
|
+ "packet received on pipe {}: {:?}, {}",
|
|
|
95
|
+ pipe,
|
|
|
96
|
+ payload.as_ref(),
|
|
|
97
|
+ payload.len()
|
|
|
98
|
+ );
|
|
|
99
|
+ if payload.len() != 12 {
|
|
|
100
|
+ continue;
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
96
|
103
|
let pressure = u32::from_le_bytes(payload.as_ref()[0..4].try_into().unwrap());
|
|
97
|
|
- let temperature = u32::from_le_bytes(payload.as_ref()[4..8].try_into().unwrap());
|
|
|
104
|
+
|
|
|
105
|
+ if pressure < 50000 || pressure > 150000 {
|
|
|
106
|
+ info!("pressure outside of range: {}", pressure);
|
|
|
107
|
+ continue;
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ let temperature = i32::from_le_bytes(payload.as_ref()[4..8].try_into().unwrap());
|
|
|
111
|
+
|
|
|
112
|
+ if temperature < -50 * 100 || temperature > 100 * 100 {
|
|
|
113
|
+ info!("temperature outside of range: {}", temperature);
|
|
|
114
|
+ continue;
|
|
|
115
|
+ }
|
|
|
116
|
+
|
|
98
|
117
|
let humidity = u32::from_le_bytes(payload.as_ref()[8..12].try_into().unwrap());
|
|
|
118
|
+ if humidity > 100 * 1024 {
|
|
|
119
|
+ info!("humidity outside of range: {}", humidity);
|
|
|
120
|
+ continue;
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
99
|
123
|
let pressure = pressure as f32 / 100.0;
|
|
100
|
124
|
let temperature = temperature as f32 / 100.0;
|
|
101
|
125
|
let humidity = humidity as f32 / 1024.0;
|
|
102
|
126
|
info!("pressure: {}", pressure);
|
|
103
|
127
|
info!("temperature: {}", temperature);
|
|
104
|
128
|
info!("humidity: {}", humidity);
|
|
105
|
|
- updates.send(SensorUpdate{
|
|
106
|
|
- location: 0,
|
|
107
|
|
- data: vec![SensorData::Temperature(temperature), SensorData::Pressure(pressure), SensorData::Humidity(humidity)],
|
|
108
|
|
- }).unwrap();
|
|
|
129
|
+ updates
|
|
|
130
|
+ .send(SensorUpdate {
|
|
|
131
|
+ location: 0,
|
|
|
132
|
+ data: vec![
|
|
|
133
|
+ SensorData::Temperature(temperature),
|
|
|
134
|
+ SensorData::Pressure(pressure),
|
|
|
135
|
+ SensorData::Humidity(humidity),
|
|
|
136
|
+ ],
|
|
|
137
|
+ })
|
|
|
138
|
+ .unwrap();
|
|
109
|
139
|
let end = Instant::now();
|
|
110
|
140
|
let elapsed = end.duration_since(start);
|
|
111
|
141
|
info!("Debug: {:?}", elapsed);
|