Skip to content

Latest commit

 

History

History
113 lines (80 loc) · 3.68 KB

README.rst

File metadata and controls

113 lines (80 loc) · 3.68 KB

Adafruit_CircuitPython_NRF24L01

Build Status Documentation Status

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Usage Example

See examples/ for an example of how to use the library.

To run the example, open a python terminal in the example folder and run the following:

>>> from nrf24l01_simpletest import *

    NRF24L01 test module.
    Pinout:
        CE on D5
        CS on D6
        SPI pins on SPI1

    Run slave() on receiver, and master() on transmitter.

>>> master()
Sending:  0
Sending:  1

Firstly import the necessary packages for your application.

import time
import struct # transmitted packet must be a byte array
import board
import digitalio as dio
from busio import SPI
from adafruit_circuitpython_nrf24l01 import NRF24L01 # this library

Define the communication pipes (effectively device addresses/IDs) and the SPI connections to the radio.

pipes = (b'\x01\x02\x03\x04\x00', b'\x01\x02\x03\x04\x01') # tx, rx node ID's

ce = dio.DigitalInOut(board.D5)
ce.direction = dio.Direction.OUTPUT
cs = dio.DigitalInOut(board.D6)
cs.direction = dio.Direction.OUTPUT

spi = SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) # create instance of spi port
nrf = NRF24L01(spi, cs, ce, channel=0, payload_size=1) # create instance of the radio

To transmit firstly open the TX and RX pipes, stop listening (puts radio in transmit mode) and send your packet (buf).

def radio_tx():
    nrf.open_tx_pipe(pipes[0])
    nrf.open_rx_pipe(1, pipes[1])
    nrf.stop_listening()

    i = 0

    while True:
        try:
            print("Sending: ", i)
            nrf.send(struct.pack('i', i)) # be sure to pack data in byte array
        except OSError:
            pass
        time.sleep(1) # send every 1s

To receive this data, again open the TX and RX pipes and start listening for data. The nerf.any() method returns true when there is data ready to be received.

def radio_rx():
    nrf.open_tx_pipe(pipes[1])
    nrf.open_rx_pipe(1, pipes[0])
    nrf.start_listening()

    while True:
        if nrf.any():
            while nrf.any():
                buf = nrf.recv()
                i = struct.unpack('i', buf) # byte array formats (`i`) must maadafruit_bus_device.spi_device.SPIDeviceadafruit_bus_device.spi_device.SPIDeviceadafruit_bus_device.spi_device.SPIDeviceadafruit_bus_device.spi_device.SPIDevicetch
                print("Received: ", i)
                time.sleep(0.5) # poll every 0.5s for new data

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.