|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
use std::convert::TryInto;
|
|
2
|
2
|
use std::sync::mpsc;
|
|
3
|
3
|
use std::thread;
|
|
|
4
|
+use std::thread::sleep;
|
|
4
|
5
|
use std::time::{Duration, Instant};
|
|
5
|
6
|
|
|
6
|
7
|
use embedded_nrf24l01::{Configuration, CrcMode, DataRate, NRF24L01};
|
|
|
@@ -70,9 +71,9 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
70
|
71
|
nrf24.set_rf(DataRate::R2Mbps, 3)?;
|
|
71
|
72
|
nrf24.set_crc(Some(CrcMode::OneByte))?;
|
|
72
|
73
|
nrf24.set_auto_retransmit(250, 3)?;
|
|
73
|
|
- nrf24.set_pipes_rx_enable(&[true, false, false, false, false, false])?;
|
|
|
74
|
+ nrf24.set_pipes_rx_enable(&[false, true, false, false, false, false])?; // TODO enable pipe 0 once the base station receives messages
|
|
74
|
75
|
nrf24
|
|
75
|
|
- .set_rx_addr(0, &[0x56, 0x34, 0x12, 0x00, 0x00])
|
|
|
76
|
+ .set_rx_addr(1, &[0xB3, 0xB3, 0xB3, 0xB3, 0x00])
|
|
76
|
77
|
.unwrap();
|
|
77
|
78
|
nrf24.flush_rx().unwrap();
|
|
78
|
79
|
nrf24.flush_tx().unwrap();
|
|
|
@@ -88,6 +89,7 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
88
|
89
|
let mut nrf24 = nrf24.rx().unwrap();
|
|
89
|
90
|
info!("Starting to receive:");
|
|
90
|
91
|
loop {
|
|
|
92
|
+ sleep(Duration::from_millis(1));
|
|
91
|
93
|
if let Some(pipe) = nrf24.can_read().unwrap() {
|
|
92
|
94
|
let payload = nrf24.read().unwrap();
|
|
93
|
95
|
info!(
|
|
|
@@ -96,25 +98,27 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
96
|
98
|
payload.as_ref(),
|
|
97
|
99
|
payload.len()
|
|
98
|
100
|
);
|
|
99
|
|
- if payload.len() != 12 {
|
|
|
101
|
+ if payload.len() != 13 {
|
|
100
|
102
|
continue;
|
|
101
|
103
|
}
|
|
102
|
104
|
|
|
103
|
|
- let pressure = u32::from_le_bytes(payload.as_ref()[0..4].try_into().unwrap());
|
|
|
105
|
+ let sensor_id = payload.as_ref()[0];
|
|
|
106
|
+
|
|
|
107
|
+ let pressure = u32::from_le_bytes(payload.as_ref()[1..5].try_into().unwrap());
|
|
104
|
108
|
|
|
105
|
109
|
if pressure < 50000 || pressure > 150000 {
|
|
106
|
110
|
info!("pressure outside of range: {}", pressure);
|
|
107
|
111
|
continue;
|
|
108
|
112
|
}
|
|
109
|
113
|
|
|
110
|
|
- let temperature = i32::from_le_bytes(payload.as_ref()[4..8].try_into().unwrap());
|
|
|
114
|
+ let temperature = i32::from_le_bytes(payload.as_ref()[5..9].try_into().unwrap());
|
|
111
|
115
|
|
|
112
|
116
|
if temperature < -50 * 100 || temperature > 100 * 100 {
|
|
113
|
117
|
info!("temperature outside of range: {}", temperature);
|
|
114
|
118
|
continue;
|
|
115
|
119
|
}
|
|
116
|
120
|
|
|
117
|
|
- let humidity = u32::from_le_bytes(payload.as_ref()[8..12].try_into().unwrap());
|
|
|
121
|
+ let humidity = u32::from_le_bytes(payload.as_ref()[9..13].try_into().unwrap());
|
|
118
|
122
|
if humidity > 100 * 1024 {
|
|
119
|
123
|
info!("humidity outside of range: {}", humidity);
|
|
120
|
124
|
continue;
|
|
|
@@ -123,9 +127,11 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
123
|
127
|
let pressure = pressure as f32 / 100.0;
|
|
124
|
128
|
let temperature = temperature as f32 / 100.0;
|
|
125
|
129
|
let humidity = humidity as f32 / 1024.0;
|
|
|
130
|
+ info!("sensor id: 0x{:x}", sensor_id);
|
|
126
|
131
|
info!("pressure: {}", pressure);
|
|
127
|
132
|
info!("temperature: {}", temperature);
|
|
128
|
133
|
info!("humidity: {}", humidity);
|
|
|
134
|
+ /*
|
|
129
|
135
|
updates
|
|
130
|
136
|
.send(SensorUpdate {
|
|
131
|
137
|
location: 0,
|
|
|
@@ -136,6 +142,7 @@ fn radio_thread(updates: &mpsc::Sender<SensorUpdate>) -> Result<(), Error> {
|
|
136
|
142
|
],
|
|
137
|
143
|
})
|
|
138
|
144
|
.unwrap();
|
|
|
145
|
+ */
|
|
139
|
146
|
let end = Instant::now();
|
|
140
|
147
|
let elapsed = end.duration_since(start);
|
|
141
|
148
|
info!("Debug: {:?}", elapsed);
|