Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4625

Networking and servers • nRF24L01 Communication between Pico and Pi Zero

$
0
0
I'm trying to create a setup where a Pi Zero can transmit commands to a Pico via some nRF24L01+PA+LNA modules. I think the wirings are correct, since the hardware is responding to register commands, but the sender code is timing out.

Pi Zero (nrf24 module):

Code:

import timeimport tracebackimport pigpioimport spidevimport nrf24import structfrom datetime import datetimefrom nrf24 import NRF24pipes = [b"\x01\x02\x03\x04\x05", b"\xF0\xF0\xF0\xF0\xE1"]pi = pigpio.pi("localhost", 8888)assert(pi.connected)radio = NRF24(pi, ce=6, channel=46, payload_size=20, pa_level=nrf24.RF24_PA.MIN, data_rate=nrf24.RF24_DATA_RATE.RATE_250KBPS, crc_bytes=nrf24.RF24_CRC.BYTES_2)# Check that the hardware is responding, by writing to a register and then# reading from it again.radio._nrf_write_reg(radio.SETUP_AW, 0b11)value = radio._nrf_read_reg(radio.SETUP_AW, 1)assert(value[0] == 0b11)radio.set_address_bytes(5)radio.open_writing_pipe(pipes[0])radio.open_reading_pipe(nrf24.RF24_RX_ADDR.P1, pipes[0])radio.show_registers()try:    while True:        payload = struct.pack("20s", b"Hello")        radio.reset_packages_lost()        radio.send(payload)        try:            radio.wait_until_sent()            print("sent")        except TimeoutError:            print('Timeout waiting for transmission to complete.')            time.sleep(5)            continue        if radio.get_packages_lost() == 0:            print(f"Success: lost={radio.get_packages_lost()}, retries={radio.get_retries()}")        else:            print(f"Error: lost={radio.get_packages_lost()}, retries={radio.get_retries()}")        time.sleep(10)except:    traceback.print_exc()    radio.power_down()    pi.stop()
Pico's code (MicroPython's nrf24l01 driver):

Code:

import nrf24l01from nrf24l01 import NRF24L01from machine import SPI, Pinfrom time import sleepimport structcsn = Pin(14, mode=Pin.OUT, value=1) # Chip Select Notce = Pin(17, mode=Pin.OUT, value=0)  # Chip Enableled = Pin("LED", Pin.OUT)                  # Onboard LEDpayload_size = 20role = "receive"def setup():    print("Initialising the nRF24L0+ Module")    nrf = NRF24L01(SPI(0), csn, ce, channel=46, payload_size=payload_size)    nrf.open_tx_pipe(b"\x01\x02\x03\x04\x05") # Swap these if role == "send"    nrf.open_rx_pipe(1, b"\x01\x02\x03\x04\x05")    nrf.set_power_speed(nrf24l01.POWER_0, nrf24l01.SPEED_250K)    nrf.start_listening() if role == "receive" else None    nrf.set_crc(2)    return nrfdef flash_led(times:int=None):    for _ in range(times):        led.on()        sleep(0.15)        led.off()        sleep(0.01)def send(nrf, msg):    print("sending message.", msg)    nrf.stop_listening()    for n in range(len(msg)):        try:            encoded_string = msg[n].encode()            byte_array = bytearray(encoded_string)            buf = struct.pack("s", byte_array)            nrf.send(buf)            # print(role,"message",msg[n],"sent")            flash_led(1)        except OSError as e:            print(role, "Sorry message not sent", e)    nrf.send("\n")    nrf.start_listening()# main code loopflash_led(1)nrf = setup()nrf.start_listening()msg_string = ""while True:    msg = ""    if role == "send":        send(nrf, "Yello world")    else:        # Check for Messages        if nrf.any():            package = nrf.recv()                     message = struct.unpack("s",package)            msg = message[0].decode()            print("Got ", msg)            flash_led(1)            # Check for the new line character            if (msg == "\n") and (len(msg_string) <= 20):                print("full message",msg_string, msg)                msg_string = ""            else:                if len(msg_string) <= 20:                    msg_string = msg_string + msg                else:                    msg_string = ""
I've already checked the parameters of each module, and as far as I can tell the transmission rate, power level, addresses, channel, etc are all the same. I'm pretty new to RF modules though, so I'm sure I'm making a silly mistake somewhere.

Any help will be greatly appreciated.

Statistics: Posted by unencoded — Fri Dec 20, 2024 5:40 pm — Replies 0 — Views 7



Viewing all articles
Browse latest Browse all 4625

Trending Articles