and follow the prompts.
1"""
2A simple example of sending data from 1 nRF24L01 transceiver to another.
3This example was written to be used on 2 devices acting as 'nodes'.
4
5See documentation at https://nRF24.github.io/RF24
6"""
7
8import time
9import struct
10from RF24 import RF24, RF24_PA_LOW, RF24_DRIVER
11
12print(__file__)
13
14
20CSN_PIN = 0
21if RF24_DRIVER == "MRAA":
22 CE_PIN = 15
23elif RF24_DRIVER == "wiringPi":
24 CE_PIN = 3
25else:
26 CE_PIN = 22
27radio =
RF24(CE_PIN, CSN_PIN)
28
29
30if not radio.begin():
31 raise RuntimeError("radio hardware is not responding")
32
33
34
35address = [b"1Node", b"2Node"]
36
37
38
39
40
41
42
43radio_number = bool(
44 int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
45)
46
47
48
49radio.setPALevel(RF24_PA_LOW)
50
51
52radio.stopListening(address[radio_number])
53
54
55radio.openReadingPipe(1, address[not radio_number])
56
57
58
59
60radio.payloadSize = struct.calcsize("f")
61
62
63
64
65
66
67
68
69
70payload = [0.0]
71
72
73def master():
74 """Transmits an incrementing float every second"""
75 radio.stopListening()
76 failures = 0
77 while failures < 6:
78
79
80 buffer = struct.pack("<f", payload[0])
81 start_timer = time.monotonic_ns()
82 result = radio.write(buffer)
83 end_timer = time.monotonic_ns()
84 if not result:
85 print("Transmission failed or timed out")
86 failures += 1
87 else:
88 print(
89 "Transmission successful! Time to Transmit:",
90 f"{(end_timer - start_timer) / 1000} us. Sent: {payload[0]}",
91 )
92 payload[0] += 0.01
93 time.sleep(1)
94 print(failures, "failures detected. Leaving TX role.")
95
96
97def slave(timeout=6):
98 """Listen for any payloads and print the transaction
99
100 :param int timeout: The number of seconds to wait (with no transmission)
101 until exiting function.
102 """
103 radio.startListening()
104
105 start_timer = time.monotonic()
106 while (time.monotonic() - start_timer) < timeout:
107 has_payload, pipe_number = radio.available_pipe()
108 if has_payload:
109
110 buffer = radio.read(radio.payloadSize)
111
112
113
114 payload[0] = struct.unpack("<f", buffer[:4])[0]
115
116 print(
117 f"Received {radio.payloadSize} bytes",
118 f"on pipe {pipe_number}: {payload[0]}",
119 )
120 start_timer = time.monotonic()
121
122 print("Nothing received in", timeout, "seconds. Leaving RX role")
123
124 radio.stopListening()
125
126
127def set_role() -> bool:
128 """Set the role using stdin stream. Timeout arg for slave() can be
129 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
130
131 :return:
132 - True when role is complete & app should continue running.
133 - False when app should exit
134 """
135 user_input = (
136 input(
137 "*** Enter 'R' for receiver role.\n"
138 "*** Enter 'T' for transmitter role.\n"
139 "*** Enter 'Q' to quit example.\n"
140 )
141 or "?"
142 )
143 user_input = user_input.split()
144 if user_input[0].upper().startswith("R"):
145 if len(user_input) > 1:
146 slave(int(user_input[1]))
147 else:
148 slave()
149 return True
150 if user_input[0].upper().startswith("T"):
151 master()
152 return True
153 if user_input[0].upper().startswith("Q"):
154 radio.powerDown()
155 return False
156 print(user_input[0], "is an unrecognized input. Please try again.")
157 return set_role()
158
159
160if __name__ == "__main__":
161 try:
162 while set_role():
163 pass
164 except KeyboardInterrupt:
165 print(" Keyboard Interrupt detected. Powering down radio.")
166 radio.powerDown()
167else:
168 print(" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.