1- """Test for nrf24l01 module."""
1+ """Test for nrf24l01 module. Portable between MicroPython targets. """
22
3- import struct
3+ import sys
4+ import ustruct as struct
45import utime
56from machine import Pin , SPI
67from nrf24l01 import NRF24L01
8+ from micropython import const
9+
10+ # Slave pause between receiving data and checking for further packets.
11+ _RX_POLL_DELAY = const (15 )
12+ # Slave pauses an additional _SLAVE_SEND_DELAY ms after receiving data and before
13+ # transmitting to allow the (remote) master time to get into receive mode. The
14+ # master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
15+ _SLAVE_SEND_DELAY = const (10 )
16+
17+ if sys .platform == 'pyboard' :
18+ cfg = {'spi' : 2 , 'miso' : 'Y7' , 'mosi' : 'Y8' , 'sck' : 'Y6' , 'csn' : 'Y5' , 'ce' : 'Y4' }
19+ elif sys .platform == 'esp8266' : # Hardware SPI
20+ cfg = {'spi' : 1 , 'miso' : 12 , 'mosi' : 13 , 'sck' : 14 , 'csn' : 4 , 'ce' : 5 }
21+ elif sys .platform == 'esp32' : # Software SPI
22+ cfg = {'spi' : - 1 , 'miso' : 32 , 'mosi' : 33 , 'sck' : 25 , 'csn' : 26 , 'ce' : 27 }
23+ else :
24+ raise ValueError ('Unsupported platform {}' .format (sys .platform ))
725
826pipes = (b'\xf0 \xf0 \xf0 \xf0 \xe1 ' , b'\xf0 \xf0 \xf0 \xf0 \xd2 ' )
927
1028def master ():
11- nrf = NRF24L01 (SPI (2 ), Pin ('Y5' ), Pin ('Y4' ), payload_size = 8 )
29+ csn = Pin (cfg ['csn' ], mode = Pin .OUT , value = 1 )
30+ ce = Pin (cfg ['ce' ], mode = Pin .OUT , value = 0 )
31+ if cfg ['spi' ] == - 1 :
32+ spi = SPI (- 1 , sck = Pin (cfg ['sck' ]), mosi = Pin (cfg ['mosi' ]), miso = Pin (cfg ['miso' ]))
33+ nrf = NRF24L01 (spi , csn , ce , payload_size = 8 )
34+ else :
35+ nrf = NRF24L01 (SPI (cfg ['spi' ]), csn , ce , payload_size = 8 )
1236
1337 nrf .open_tx_pipe (pipes [0 ])
1438 nrf .open_rx_pipe (1 , pipes [1 ])
@@ -60,7 +84,13 @@ def master():
6084 print ('master finished sending; successes=%d, failures=%d' % (num_successes , num_failures ))
6185
6286def slave ():
63- nrf = NRF24L01 (SPI (2 ), Pin ('Y5' ), Pin ('Y4' ), payload_size = 8 )
87+ csn = Pin (cfg ['csn' ], mode = Pin .OUT , value = 1 )
88+ ce = Pin (cfg ['ce' ], mode = Pin .OUT , value = 0 )
89+ if cfg ['spi' ] == - 1 :
90+ spi = SPI (- 1 , sck = Pin (cfg ['sck' ]), mosi = Pin (cfg ['mosi' ]), miso = Pin (cfg ['miso' ]))
91+ nrf = NRF24L01 (spi , csn , ce , payload_size = 8 )
92+ else :
93+ nrf = NRF24L01 (SPI (cfg ['spi' ]), csn , ce , payload_size = 8 )
6494
6595 nrf .open_tx_pipe (pipes [1 ])
6696 nrf .open_rx_pipe (1 , pipes [0 ])
@@ -69,7 +99,6 @@ def slave():
6999 print ('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)' )
70100
71101 while True :
72- machine .idle ()
73102 if nrf .any ():
74103 while nrf .any ():
75104 buf = nrf .recv ()
@@ -81,8 +110,10 @@ def slave():
81110 else :
82111 led .off ()
83112 led_state >>= 1
84- utime .sleep_ms (15 )
113+ utime .sleep_ms (_RX_POLL_DELAY )
85114
115+ # Give master time to get into receive mode.
116+ utime .sleep_ms (_SLAVE_SEND_DELAY )
86117 nrf .stop_listening ()
87118 try :
88119 nrf .send (struct .pack ('i' , millis ))
@@ -99,9 +130,9 @@ def slave():
99130
100131print ('NRF24L01 test module loaded' )
101132print ('NRF24L01 pinout for test:' )
102- print (' CE on Y4' )
103- print (' CSN on Y5' )
104- print (' SCK on Y6' )
105- print (' MISO on Y7' )
106- print (' MOSI on Y8' )
133+ print (' CE on' , cfg [ 'ce' ] )
134+ print (' CSN on' , cfg [ 'csn' ] )
135+ print (' SCK on' , cfg [ 'sck' ] )
136+ print (' MISO on' , cfg [ 'miso' ] )
137+ print (' MOSI on' , cfg [ 'mosi' ] )
107138print ('run nrf24l01test.slave() on slave, then nrf24l01test.master() on master' )
0 commit comments