Exemple #1
0
import busio

addressLength = 5 # available for int [3,5]
rxPipeNum = 0
transmitInterval = 0.2

ce = dio.DigitalInOut(board.D18)
csn = dio.DigitalInOut(board.D16)

# config.txt -> dtoverlay=spi1-1cs NEEDED?
board.MOSI = 20 #raspi4b+ spi1
board.SCK = 21 #raspi4b+ spi1
board.MISO = 19 #raspi4b+ spi1

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
nrf = RF24(spi, csn, ce, address_length=addressLength)

startCode = "@"

def encodeData(value, code):
    result = startCode + code+ " "
    result = result.encode("utf-8")
    for letter in str(value):
        result += letter.encode("utf-8")
    return result

def decodeData(value):
    try :
        result = value.decode("utf-8")
        print(result)
        if result[0] != "@":
Exemple #2
0
from circuitpython_nrf24l01 import RF24

# addresses needs to be in a buffer protocol object (bytearray)
address = b'1Node'

# change these (digital output) pins accordingly
ce = dio.DigitalInOut(board.D4)
csn = dio.DigitalInOut(board.D5)

# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI()  # init spi bus object

# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)

# lets create a list of payloads to be streamed to the nRF24L01 running slave()
buffers = []
SIZE = 32  # we'll use SIZE for the number of payloads in the list and the payloads' length
for i in range(SIZE):
    buff = b''
    for j in range(SIZE):
        buff += bytes([(j >= SIZE / 2 + abs(SIZE / 2 - i) or j <
                        SIZE / 2 - abs(SIZE / 2 - i)) + 48])
    buffers.append(buff)
    del buff

def master(count=1):  # count = 5 will transmit the list 5 times
    """Transmits a massive buffer of payloads"""
    # set address of RX node into a TX pipe
import digitalio as dio
from circuitpython_nrf24l01 import RF24

# addresses needs to be in a buffer protocol object (bytearray)
address = [b'1Node', b'2Node']

# change these (digital output) pins accordingly
ce = dio.DigitalInOut(board.D4)
csn = dio.DigitalInOut(board.D5)

# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI()  # init spi bus object

# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce, ask_no_ack=False)
nrf.dynamic_payloads = False # this is the default in the TMRh20 arduino library

# set address of TX node into a RX pipe
nrf.open_rx_pipe(1, address[1])
# set address of RX node into a TX pipe
nrf.open_tx_pipe(address[0])

def master(count=5):  # count = 5 will only transmit 5 packets
    """Transmits an arbitrary unsigned long value every second. This method
    will only try to transmit (count) number of attempts"""

    # for the "HandlingData" part of the test from the TMRh20 library example
    float_value = 0.01
    while count:
        nrf.listen = False # ensures the nRF24L01 is in TX mode
Exemple #4
0
display settings after changing contexts ( & thus configurations)
"""
import board
import digitalio as dio
from circuitpython_nrf24l01 import RF24

# change these (digital output) pins accordingly
ce = dio.DigitalInOut(board.D4)
csn = dio.DigitalInOut(board.D5)

# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI()  # init spi bus object

# initialize the nRF24L01 objects on the spi bus object
nrf = RF24(spi, csn, ce, ack=True)
# the first object will have all the features enabled
# including the option to use custom ACK payloads

# the second object has most features disabled/altered
# disabled dynamic_payloads, but still using enabled auto_ack
# the IRQ pin is configured to only go active on "data fail"
# using a different channel: 2 (default is 76)
# CRC is set to 1 byte long
# data rate is set to 2 Mbps
# payload length is set to 8 bytes
# NOTE address length is set to 3 bytes
# RF power amplifier is set to -12 dbm
# automatic retry attempts is set to 15 (maximum allowed)
# automatic retry delay (between attempts) is set to 1000 microseconds
basicRF = RF24(spi, csn, ce,
Exemple #5
0
from circuitpython_nrf24l01 import RF24

# addresses needs to be in a buffer protocol object (bytearray)
address = b'1Node'

# change these (digital output) pins accordingly
ce = dio.DigitalInOut(board.D8)
csn = dio.DigitalInOut(board.D17)

# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI()  # init spi bus object

# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce, ard=1000, arc=15, data_rate=1)


def generateSHA1Checksum(l, len=30):
    h = hashlib.new('sha1')
    v = ''

    #Add each value in l in string form
    for s in l:
        v = v + str(s)

    #Put into bytearray for hashing
    h.update(bytes(v.encode('ASCII')))
    l.append('Checksum')
    l.append(h.hexdigest()[0:len])
    return l
def initializeHardware(display_diagnostics=False,
                       ce_pin=board.D8,
                       csn_pin=board.D17,
                       button_pin=16,
                       ch=76):
    if cfg.config['has_radio']:
        from circuitpython_nrf24l01 import RF24
        import digitalio as dio
        global address, spi, nrf
        try:
            address = b'1Node'
            ce = dio.DigitalInOut(ce_pin)
            csn = dio.DigitalInOut(csn_pin)
            spi = board.SPI()  # init spi bus object

            #Initialize the nRF24L01 on the spi bus object
            nrf = RF24(spi,
                       csn,
                       ce,
                       ard=2000,
                       arc=15,
                       data_rate=1,
                       auto_ack=True,
                       channel=ch)

            if display_diagnostics:
                nrf.what_happened(True)
            else:
                printOK("Radio Initialized")
        except:
            printCRIT("Radio required to proceed. Exiting.")
            quit()
    else:
        printCRIT("Radio required to proceed. Exiting.")
        quit()

    if cfg.config['has_accel']:
        import adafruit_lsm303_accel
        import adafruit_lsm303dlh_mag
        import busio
        global mag, accel, i2c
        try:
            i2c = busio.I2C(board.SCL, board.SDA)
            mag = adafruit_lsm303dlh_mag.LSM303DLH_Mag(i2c)
            accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
            calibrateAccel(cycles=25)
            printOK("Accel Initialized")
        except:
            printERR("Accel Error, Check Connections")
    else:
        printBYP("No Accel Installed... Bypassing")

    if cfg.config['has_GPS']:
        import serial
        import adafruit_gps
        global gps
        try:
            uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=10)
            gps = adafruit_gps.GPS(uart, debug=False)
            gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
            gps.send_command(b'PMTK220,200')
            if display_diagnostics:
                gps.update()
                getGPSLock()
            else:
                printOK("GPS Initialized")
        except:
            printERROR("GPS Error, Check Connections")
    else:
        printBYP("No GPS Installed... Bypassing")

    if cfg.config['has_button']:
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        if display_diagnostics:
            print(interupt())
        else:
            printOK("Button Initialized")
    else:
        printBYP("No Button Installed... Bypassing")
    printOK("System Ready")
    print("=" * 40)
Exemple #7
0
from circuitpython_nrf24l01 import RF24

# addresses needs to be in a buffer protocol object (bytearray)
address = b'ttech'

# change these (digital output) pins accordingly
ce = dio.DigitalInOut(board.D5)
csn = dio.DigitalInOut(board.D6)

# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI()  # init spi bus object

# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce, channel=106, data_rate=250)


def master(count=5):  # count = 5 will only transmit 5 packets
    """Transmits an incrementing integer every second"""
    # set address of RX node into a TX pipe
    nrf.open_tx_pipe(address)
    # ensures the nRF24L01 is in TX mode
    nrf.listen = False

    while count:
        # use struct.pack to packetize your data
        # into a usable payload
        buffer = struct.pack('<i', count)
        # 'i' means a single 4 byte int value.
        # '<' means little endian byte order. this may be optional
Exemple #8
0
"""
Example of library usage receiving commands via an
nRF24L01 transceiver to control a Mecanum drivetrain.
"""
import board
from digitalio import DigitalInOut as Dio
from circuitpython_nrf24l01 import RF24
from drivetrain import Mecanum, BiMotor, NRF24L01rx

# instantiate transceiver radio on the SPI bus
nrf = RF24(board.SPI(), Dio(board.D5), Dio(board.D4))

# instantiate motors for a Mecanum drivetrain in the following order
# Front-Right, Rear-Right, Rear-Left, Front-Left
motors = [
    BiMotor([board.RX, board.TX]),
    BiMotor([board.D13, board.D12]),
    BiMotor([board.D11, board.D10]),
    BiMotor([board.D2, board.D7])
    ]
# NOTE there are no more PWM pins available

# instantiate receiving object for a Mecanum drivetrain
d = NRF24L01rx(nrf, Mecanum(motors))

while True: # this runs forever
    d.sync()
# doing a keyboard interupt will most likely leave the SPI bus in an
# undesirable state. You must do a hard-reset of the circuitoython MCU to
# reset the SPI bus for continued use. This code assumes power is lost on exit.