Optimized high speed nRF24L01+ driver class documentation v1.4.10
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
examples_linux/getting_started.py

Written by 2bndy5 in 2020

This is a simple example of using the RF24 class on a Raspberry Pi.

Remember to install the Python wrapper, then navigate to the "RF24/examples_linux" folder.
To run this example, enter

python3 getting_started.py

and follow the prompts.

Note
this example requires python v3.7 or newer because it measures transmission time with time.monotonic_ns().
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__) # print example name
13
14
20CSN_PIN = 0 # GPIO8 aka CE0 on SPI bus 0: /dev/spidev0.0
21if RF24_DRIVER == "MRAA":
22 CE_PIN = 15 # for GPIO22
23elif RF24_DRIVER == "wiringPi":
24 CE_PIN = 3 # for GPIO22
25else:
26 CE_PIN = 22
27radio = RF24(CE_PIN, CSN_PIN)
28
29# initialize the nRF24L01 on the spi bus
30if not radio.begin():
31 raise RuntimeError("radio hardware is not responding")
32
33# For this example, we will use different addresses
34# An address need to be a buffer protocol object (bytearray)
35address = [b"1Node", b"2Node"]
36# It is very helpful to think of an address as a path instead of as
37# an identifying device destination
38
39
40# to use different addresses on a pair of radios, we need a variable to
41# uniquely identify which address this radio will use to transmit
42# 0 uses address[0] to transmit, 1 uses address[1] to transmit
43radio_number = bool(
44 int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
45)
46
47# set the Power Amplifier level to -12 dBm since this test example is
48# usually run with nRF24L01 transceivers in close proximity of each other
49radio.setPALevel(RF24_PA_LOW) # RF24_PA_MAX is default
50
51# set the TX address of the RX node into the TX pipe
52radio.openWritingPipe(address[radio_number]) # always uses pipe 0
53
54# set the RX address of the TX node into a RX pipe
55radio.openReadingPipe(1, address[not radio_number]) # using pipe 1
56
57# To save time during transmission, we'll set the payload size to be only
58# what we need. A float value occupies 4 bytes in memory using
59# struct.pack(); "f" means an unsigned float
60radio.payloadSize = struct.calcsize("f")
61
62# for debugging, we have 2 options that print a large block of details
63# (smaller) function that prints raw register values
64# radio.printDetails()
65# (larger) function that prints human readable data
66# radio.printPrettyDetails()
67
68# using the python keyword global is bad practice. Instead we'll use a 1 item
69# list to store our float number for the payloads sent/received
70payload = [0.0]
71
72
73def master():
74 """Transmits an incrementing float every second"""
75 radio.stopListening() # put radio in TX mode
76 failures = 0
77 while failures < 6:
78 # use struct.pack() to packet your data into the payload
79 # "<f" means a single little endian (4 byte) float value.
80 buffer = struct.pack("<f", payload[0])
81 start_timer = time.monotonic_ns() # start timer
82 result = radio.write(buffer)
83 end_timer = time.monotonic_ns() # end timer
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() # put radio in RX mode
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 # fetch 1 payload from RX FIFO
110 buffer = radio.read(radio.payloadSize)
111 # use struct.unpack() to convert the buffer into usable data
112 # expecting a little endian float, thus the format string "<f"
113 # buffer[:4] truncates padded 0s in case payloadSize was not set
114 payload[0] = struct.unpack("<f", buffer[:4])[0]
115 # print details about the received packet
116 print(
117 f"Received {radio.payloadSize} bytes",
118 f"on pipe {pipe_number}: {payload[0]}",
119 )
120 start_timer = time.monotonic() # reset the timeout timer
121
122 print("Nothing received in", timeout, "seconds. Leaving RX role")
123 # recommended behavior is to keep in TX mode while idle
124 radio.stopListening() # put the radio in TX mode
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 # continue example until 'Q' is entered
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.
Definition RF24.h:116