A simple radio link between two Pyboards

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

A simple radio link between two Pyboards

Post by pythoncoder » Wed Jan 13, 2016 11:16 am

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
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

A simple radio link between two Pyboards

Post by pythoncoder » Sun Jun 07, 2020 3:52 pm

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:

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.

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: A simple radio link between two Pyboards

Post by jcw » Sun Jun 07, 2020 5:46 pm

Very nice! Some setup involved, but it looks like I have everything needed - looking forward to try out the stream version.

User avatar
jcw
Posts: 37
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: A simple radio link between two Pyboards

Post by jcw » Mon Jun 08, 2020 11:32 am

Ok, got your setup going on two F407 boards. One thing I had to change is the connection rate:

Code: Select all

        radio = NRF24L01(config.spi, config.csn, config.ce, config.channel, 32)
        radio.set_power_speed(0x06, 0x08)
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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: A simple radio link between two Pyboards

Post by pythoncoder » Mon Jun 08, 2020 5:52 pm

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.
Peter Hinch
Index to my micropython libraries.

Post Reply