Giter Club home page Giter Club logo

adafruit_circuitpython_nrf24l01's Introduction

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.

adafruit_circuitpython_nrf24l01's People

Contributors

2bndy5 avatar kattni avatar rhthomas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.