Optimized High Speed NRF24L01+ Driver Class Documenation  V1.0
TMRh20 2014 - Optimized Fork of NRF24L01+ Driver
pingpair_dyn.py

This is a python example for RPi of how to use payloads of a varying (dynamic) size.

1 #!/usr/bin/env python
2 
3 #
4 # Example using Dynamic Payloads
5 #
6 # This is an example of how to use payloads of a varying (dynamic) size.
7 #
8 
9 from __future__ import print_function
10 import time
11 from RF24 import *
12 import RPi.GPIO as GPIO
13 
14 irq_gpio_pin = None
15 
16 ########### USER CONFIGURATION ###########
17 # See https://github.com/TMRh20/RF24/blob/master/RPi/pyRF24/readme.md
18 
19 # CE Pin, CSN Pin, SPI Speed
20 
21 # Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
22 #radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)
23 
24 #RPi B
25 # Setup for GPIO 15 CE and CE1 CSN with SPI Speed @ 8Mhz
26 #radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)
27 
28 #RPi B+
29 # Setup for GPIO 22 CE and CE0 CSN for RPi B+ with SPI Speed @ 8Mhz
30 #radio = RF24(RPI_BPLUS_GPIO_J8_15, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
31 
32 # RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
33 radio = RF24(22, 0);
34 
35 
36 # Setup for connected IRQ pin, GPIO 24 on RPi B+; uncomment to activate
37 #irq_gpio_pin = RPI_BPLUS_GPIO_J8_18
38 #irq_gpio_pin = 24
39 
40 ##########################################
41 def try_read_data(channel=0):
42  if radio.available():
43  while radio.available():
44  len = radio.getDynamicPayloadSize()
45  receive_payload = radio.read(len)
46  print('Got payload size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
47  # First, stop listening so we can talk
48  radio.stopListening()
49 
50  # Send the final one back.
51  radio.write(receive_payload)
52  print('Sent response.')
53 
54  # Now, resume listening so we catch the next packets.
55  radio.startListening()
56 
57 pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2]
58 min_payload_size = 4
59 max_payload_size = 32
60 payload_size_increments_by = 1
61 next_payload_size = min_payload_size
62 inp_role = 'none'
63 send_payload = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012'
64 millis = lambda: int(round(time.time() * 1000))
65 
66 print('pyRF24/examples/pingpair_dyn/')
67 radio.begin()
68 radio.enableDynamicPayloads()
69 radio.setRetries(5,15)
70 radio.printDetails()
71 
72 print(' ************ Role Setup *********** ')
73 while (inp_role !='0') and (inp_role !='1'):
74  inp_role = str(input('Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) '))
75 
76 if inp_role == '0':
77  print('Role: Pong Back, awaiting transmission')
78  if irq_gpio_pin is not None:
79  # set up callback for irq pin
80  GPIO.setmode(GPIO.BCM)
81  GPIO.setup(irq_gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
82  GPIO.add_event_detect(irq_gpio_pin, GPIO.FALLING, callback=try_read_data)
83 
84  radio.openWritingPipe(pipes[1])
85  radio.openReadingPipe(1,pipes[0])
86  radio.startListening()
87 else:
88  print('Role: Ping Out, starting transmission')
89  radio.openWritingPipe(pipes[0])
90  radio.openReadingPipe(1,pipes[1])
91 
92 # forever loop
93 while 1:
94  if inp_role == '1': # ping out
95  # The payload will always be the same, what will change is how much of it we send.
96 
97  # First, stop listening so we can talk.
98  radio.stopListening()
99 
100  # Take the time, and send it. This will block until complete
101  print('Now sending length {} ... '.format(next_payload_size), end="")
102  radio.write(send_payload[:next_payload_size])
103 
104  # Now, continue listening
105  radio.startListening()
106 
107  # Wait here until we get a response, or timeout
108  started_waiting_at = millis()
109  timeout = False
110  while (not radio.available()) and (not timeout):
111  if (millis() - started_waiting_at) > 500:
112  timeout = True
113 
114  # Describe the results
115  if timeout:
116  print('failed, response timed out.')
117  else:
118  # Grab the response, compare, and send to debugging spew
119  len = radio.getDynamicPayloadSize()
120  receive_payload = radio.read(len)
121 
122  # Spew it
123  print('got response size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
124 
125  # Update size for next time.
126  next_payload_size += payload_size_increments_by
127  if next_payload_size > max_payload_size:
128  next_payload_size = min_payload_size
129  time.sleep(0.1)
130  else:
131  # Pong back role. Receive each packet, dump it out, and send it back
132 
133  # if there is data ready
134  if irq_gpio_pin is None:
135  # no irq pin is set up -> poll it
136  try_read_data()
137  else:
138  # callback routine set for irq pin takes care for reading -
139  # do nothing, just sleeps in order not to burn cpu by looping
140  time.sleep(1000)
141