My main.py code is as below:
Code: Select all
from nrf24l01 import NRF24L01
from machine import SPI, Pin
from time import sleep
import struct
csn = Pin(14, mode=Pin.OUT, value=1) # Chip Select Not
ce = Pin(17, mode=Pin.OUT, value=0) # Chip Enable
led = Pin(25, Pin.OUT) # Onboard LED
payload_size = 20
role = "receive"
if role == "send":
send_pipe = b"\xe1\xf0\xf0\xf0\xf0"
receive_pipe = b"\xd2\xf0\xf0\xf0\xf0"
else:
send_pipe = b"\xd2\xf0\xf0\xf0\xf0"
receive_pipe = b"\xe1\xf0\xf0\xf0\xf0"
def setup():
print("Initialising the nRF24L0+ Module")
nrf = NRF24L01(SPI(0), csn, ce, payload_size=payload_size)
nrf.open_tx_pipe(send_pipe)
nrf.open_rx_pipe(1, receive_pipe)
nrf.start_listening()
return nrf
def flash_led(times:int=None):
''' Flashed the built in LED the number of times defined in the times parameter '''
for _ in range(times):
led.value(1)
sleep(0.01)
led.value(0)
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:
print(role,"Sorry message not sent")
nrf.send("\n")
nrf.start_listening()
# main code loop
flash_led(1)
nrf = setup()
nrf.start_listening()
msg_string = ""
while True:
msg = ""
if role == "send":
send(nrf, "Yello world")
send(nrf, "Test")
else:
# Check for Messages
if nrf.any():
package = nrf.recv()
message = struct.unpack("s",package)
msg = message[0].decode()
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 am using the driver from this link: https://github.com/micropython/micropyt ... rf24l01.py
My transmitter's main.py code is the same except for the role being set to "send". It too is having the same error.
Traceback:
Initialising the nRF24L0+ Module
Traceback (most recent call last):
File "<stdin>", line 58, in <module>
File "<stdin>", line 26, in setup
File "nrf24l01.py", line 75, in __init__
OSError: nRF24L01+ Hardware not responding
Jumper connections are:
GND to i/o 38
VCC to 3v3(OUT)
ce to GP 17
csn to GP 14
MOSI GP 4
sck GP 6
MISO GP 7
Any ideas as to where i could have gone wrong? New to Raspberrypi Pico here.