Communicate with raspberry PI 3 and raspberry Pico using NRF24L01

The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.

Post Reply
User avatar
/RaspberryPi
Corporate
Posts: 2449
Joined: Wed Jun 05, 2019 1:29 am

Communicate with raspberry PI 3 and raspberry Pico using NRF24L01

Post by /RaspberryPi »

Hey guys. Noob here. Basically the title. The raspberry pi 3 act as the master and the pico is the slave.
I'm using following drivers for NRF24L01.
Master Code
import RPi.GPIO as GPIO from lib_nrf24 import NRF24 import time import spidev import json ''' pin connections: NRF24L01 Raspberry Pi 3 VCC 1 | 3.3V GND 6 | GND CE 24| GPIO 8 | SPI_CE0_N CSN 11| GPIO 17 | GPIO_GEN0 MOSI 19| GPIO 10 | SPI_MOSI MISO 21| GPIO 9 | SPI_MISO SCK 23| GPIO 11 | SPI_CLK ARQ ''' GPIO.setmode(GPIO.BCM) pipes = [[0xe7,0xe7,0xe7,0xe7,0xe7],[0xc2,0xc2,0xc2,0xc2,0xc2]] radio = NRF24(GPIO,spidev.SpiDev()) #ce,csn pins radio.begin(0,17) radio.setPayloadSize(32) radio.setChannel(0x60) radio.setDataRate(NRF24.BR_250KBPS) radio.setPALevel(NRF24.PA_MIN) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openReadingPipe(1,pipes[0]) radio.openWritingPipe(pipes[1]) radio.printDetails() #radio.startListening() sample_payload = json.dumps({"id":1}) print(sample_payload) while True: #acknowledgement payload message = list("Hello World\n") radio.write(message) print("message sent: {}".format(message)) if radio.isAckPayloadAvailable(): returnedPL = [] radio.read(returnedPL,radio.getDynamicPayloadSize()) print("returned payload:{}".format(returnedPL)) else: print("No payload") Slave Code
from machine import SPI,Pin from nrf24l01 import NRF24L01 from time import sleep import struct import json ''' Pin diagrams: NRF24L01 | PICO GND | 38 GND VCC | 36 3v3OUT CE | 19 GPIO14 CSN | 20 GPIO15 SCK | 9 SPI0SCK MISO | 6 SPI0RX MOSI | 10 SPI0TX ''' pipes = [b"\xe7\xe7\xe7\xe7\xe7",b"\xc2\xc2\xc2\xc2\xc2"] #write to first pipe #radio.openReadingPipe(1,pipes[1]) #chip select not csn = Pin(15,mode=Pin.OUT,value=1) #chip enable ce = Pin(14,mode = Pin.OUT,value=0) led = Pin(25,Pin.OUT) payload_size = 32 channel = 0x60 node_ID = 1 def setup(): print("initializing") nrf = NRF24L01(SPI(0),csn,ce,payload_size=payload_size,channel=channel) #tx -> send, rx->recieve nrf.open_tx_pipe(pipes[0]) nrf.open_rx_pipe(1,pipes[1]) nrf.start_listening() print("Everything works fine") return nrf def flash_led(times:int=5): for _ in range(times): led.on() sleep(0.1) led.off() sleep(0.1) def send(nrf,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(msg[n],"sent") flash_led(1) except: print("Error") nrf.send("\n") nrf.start_listening() def decode(msg_string): #we have a string type json payload = json.loads(msg_string) if payload["id"] == node_ID: print(payload) flash_led(10) nrf = setup() nrf.start_listening() msg_string = "" while True: msg = "" if nrf.any(): package = nrf.recv() message = struct.unpack("s",package) msg = message[0].decode() flash_led(1) if msg=="\n" and len(msg_string)
Post Reply

Return to “Raspberry Pi Forum”