This uses the nRF24L01+ radio module. It is based on the driver in the official source tree but extends it to provide a means of exchanging arbitrary Python objects between a pair of Pyboards. The objects can change in size and type at runtime. It is designed to be very easy to use. This simplicity and versatility is at some cost in speed and range compared to protocols based on fixed length records.
https://github.com/peterhinch/micropython-radio.git
A simple radio link between two Pyboards
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
A simple radio link between two Pyboards
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
A simple radio link between two Pyboards
I have rewritten this. The repo contains two drivers, both of which are cross-platform. Both support a bidirectional link between a pair of radios.
radio_fast This is a minimal wrapper for the official driver. Its purpose is to help ensure that both radios are configured in a mutually compatible way. It is synchronous, supporting fixed length records with a maximum size of 32 bytes.
A stream interface This is asynchronous and requires uasyncio V3. It enables a pair of radios to support the same stream interface as a UART or socket. Arbitrary Python objects may be communicated using JSON. The interface is symmetrical and full duplex: either node can initiate message transmission at any time. A high level of data integrity is maintained through radio link outages - messages are necessarily delayed, but will be received when connectivity resumes. As a stream interface there are no restrictions on message length, structure or contents. Sample usage:
radio_fast This is a minimal wrapper for the official driver. Its purpose is to help ensure that both radios are configured in a mutually compatible way. It is synchronous, supporting fixed length records with a maximum size of 32 bytes.
A stream interface This is asynchronous and requires uasyncio V3. It enables a pair of radios to support the same stream interface as a UART or socket. Arbitrary Python objects may be communicated using JSON. The interface is symmetrical and full duplex: either node can initiate message transmission at any time. A high level of data integrity is maintained through radio link outages - messages are necessarily delayed, but will be received when connectivity resumes. As a stream interface there are no restrictions on message length, structure or contents. Sample usage:
Code: Select all
async def sender(device):
ds = [0, 0] # Data object for transmission
swriter = asyncio.StreamWriter(device, {})
while True:
# Update the data object ds (e.g. from sensors)
s = ''.join((ujson.dumps(ds), '\n'))
swriter.write(s.encode()) # convert to bytes
await swriter.drain()
await asyncio.sleep(2)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: A simple radio link between two Pyboards
Very nice! Some setup involved, but it looks like I have everything needed - looking forward to try out the stream version.
Re: A simple radio link between two Pyboards
Ok, got your setup going on two F407 boards. One thing I had to change is the connection rate:
For some reason, my nRF24L01+ modules refuse to operate at 250 kbps, so I set them to 2 Mbps.
Apart from that and after getting the hardware set up, it all works like a charm.
Code: Select all
radio = NRF24L01(config.spi, config.csn, config.ce, config.channel, 32)
radio.set_power_speed(0x06, 0x08)
Apart from that and after getting the hardware set up, it all works like a charm.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: A simple radio link between two Pyboards
Interesting. I've only ever used the default 250Kbps.
I wonder if your chips might be clones? A few years ago I bought some cheap nRF24l01 boards which would barely communicate at all with a maximum range of a couple of metres. Since then I've used Sparkfun boards without issue. Four of mine have been running for several years.
Thanks for testing. I think being able to use them like UARTs greatly simplifies their application.
I wonder if your chips might be clones? A few years ago I bought some cheap nRF24l01 boards which would barely communicate at all with a maximum range of a couple of metres. Since then I've used Sparkfun boards without issue. Four of mine have been running for several years.
Thanks for testing. I think being able to use them like UARTs greatly simplifies their application.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.