Browse Source

Change the address of the nrf24l01 and the packet format

Bernd Gottschlag 5 years ago
parent
commit
65f5893bc4
1 changed files with 13 additions and 6 deletions
  1. 13
    6
      base-station/software/src/radio.rs

+ 13
- 6
base-station/software/src/radio.rs View File

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

Loading…
Cancel
Save