-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.py
199 lines (173 loc) · 6.84 KB
/
daemon.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from datetime import datetime
from app import db, Log, app
import time
import struct
from pyrf24 import RF24, RF24_PA_HIGH, RF24_250KBPS
########### USER CONFIGURATION ###########
# CE Pin uses GPIO number with RPi and SPIDEV drivers, other drivers use
# their own pin numbering
# CS Pin corresponds the SPI bus number at /dev/spidev<a>.<b>
# ie: radio = RF24(<ce_pin>, <a>*10+<b>)
# where CS pin for /dev/spidev1.0 is 10, /dev/spidev1.1 is 11 etc...
CSN_PIN = 0 # GPIO8 aka CE0 on SPI bus 0: /dev/spidev0.0
CE_PIN = 22
radio = RF24(CE_PIN, CSN_PIN)
# initialize the nRF24L01 on the spi bus
if not radio.begin():
raise RuntimeError("radio hardware is not responding")
# For this example, we will use different addresses
# An address need to be a buffer protocol object (bytearray)
address = [b"1Node", b"2Node"]
# It is very helpful to think of an address as a path instead of as
# an identifying device destination
# to use different addresses on a pair of radios, we need a variable to
# uniquely identify which address this radio will use to transmit
# 0 uses address[0] to transmit, 1 uses address[1] to transmit
# radio_number = bool(
# int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
# )
radio_number = True
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity of each other
radio.setPALevel(RF24_PA_HIGH) # RF24_PA_MAX is default
# set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[radio_number]) # always uses pipe 0
# set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[not radio_number]) # using pipe 1
# To save time during transmission, we'll set the payload size to be only
# what we need. A float value occupies 4 bytes in memory using
# struct.pack(); "f" means an unsigned float
payload_size = 8 * 2 + 4
radio.payloadSize = payload_size
radio.channel = 108
radio.data_rate = RF24_250KBPS
# for debugging, we have 2 options that print a large block of details
# (smaller) function that prints raw register values
# radio.printDetails()
# (larger) function that prints human readable data
radio.printPrettyDetails()
# using the python keyword global is bad practice. Instead we'll use a 1 item
# list to store our float number for the payloads sent/received
payload = [0.0]
def master():
"""Transmits an incrementing float every second"""
radio.stopListening() # put radio in TX mode
failures = 0
while failures < 6:
# use struct.pack() to packet your data into the payload
# "<f" means a single little endian (4 byte) float value.
buffer = struct.pack("<f", payload[0])
start_timer = time.monotonic_ns() # start timer
result = radio.write(buffer)
end_timer = time.monotonic_ns() # end timer
if not result:
print("Transmission failed or timed out")
failures += 1
else:
print(
"Transmission successful! Time to Transmit:",
f"{(end_timer - start_timer) / 1000} us. Sent: {payload[0]}",
)
payload[0] += 0.01
time.sleep(1)
print(failures, "failures detected. Leaving TX role.")
def slave(timeout=3600):
"""Listen for any payloads and print the transaction
:param int timeout: The number of seconds to wait (with no transmission)
until exiting function.
"""
radio.startListening() # put radio in RX mode
start_timer = time.monotonic()
while (time.monotonic() - start_timer) < timeout:
has_payload, pipe_number = radio.available_pipe()
if has_payload:
# fetch 1 payload from RX FIFO
# buffer = radio.read(radio.payloadSize)
# use struct.unpack() to convert the buffer into usable data
# expecting a little endian float, thus the format string "<f"
# buffer[:4] truncates padded 0s in case payloadSize was not set
# payload[0] = struct.unpack("<f", buffer[:4])[0]
buffer = radio.read(payload_size)
data = struct.unpack("=hfhhhhhhh", buffer)
# print details about the received packet
print(data)
(
id,
pressure,
humidity,
temperature,
rain,
light,
wind_speed,
battery,
battery_level,
) = data
humidity = humidity / 100.0
temperature = temperature / 100.0
rain = rain / 100.0
light = light / 100.0
battery = battery / 1000.0
wind_speed = wind_speed / 100.0
# print(
# f"Received {radio.payloadSize} bytes",
# f"on pipe {pipe_number}: {payload[0]}",
# )
start_timer = time.monotonic() # reset the timeout timer
# save data
with app.app_context():
log = Log(
sensor_id=id,
temperature=temperature,
humidity=humidity,
pressure=pressure,
rain=rain,
wind_speed=wind_speed,
light=light,
battery=battery,
battery_level=battery_level,
timestamp=datetime.now(),
)
db.session.add(log)
db.session.commit()
print("Nothing received in", timeout, "seconds. Leaving RX role")
# recommended behavior is to keep in TX mode while idle
radio.stopListening() # put the radio in TX mode
def set_role() -> bool:
"""Set the role using stdin stream. Timeout arg for slave() can be
specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
:return:
- True when role is complete & app should continue running.
- False when app should exit
"""
user_input = (
input(
"*** Enter 'R' for receiver role.\n"
"*** Enter 'T' for transmitter role.\n"
"*** Enter 'Q' to quit example.\n"
)
or "?"
)
user_input = user_input.split()
if user_input[0].upper().startswith("R"):
if len(user_input) > 1:
slave(int(user_input[1]))
else:
slave()
return True
if user_input[0].upper().startswith("T"):
master()
return True
if user_input[0].upper().startswith("Q"):
radio.powerDown()
return False
print(user_input[0], "is an unrecognized input. Please try again.")
return set_role()
if __name__ == "__main__":
try:
while True:
slave()
except KeyboardInterrupt:
print(" Keyboard Interrupt detected. Powering down radio.")
radio.powerDown()
else:
print(" Run slave() on receiver\n Run master() on transmitter")