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

Python • NRF24L01 Pi to Pi communication

$
0
0
I'm using a modified version of the slave and master file from this tutorial: https://thezanshow.com/electronics-tuto ... rial-32-33 I am trying to modify the master file so that I can use take user input and transmit it through the NRF24L01 module. Using the codes below I am able to send data without a problem when it's formatted as text in the message list, but when trying to add anything inside of the message list besides just regular text it seems it doesn't want to transmit the message. How can I modify the message list so that it can hold something like a string instead of just text, as trying to convert strings to text doesn't work.

Master File

Code:

import RPi.GPIO as GPIOfrom lib_nrf24 import NRF24import timeimport spidevGPIO.cleanup()GPIO.setmode(GPIO.BCM)pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]radio = NRF24(GPIO, spidev.SpiDev())radio.begin(0, 17)radio.setRetries(15, 15)radio.setPayloadSize(32)radio.setChannel(0x60)radio.setDataRate(NRF24.BR_2MBPS)radio.setPALevel(NRF24.PA_MIN)radio.setAutoAck(True)radio.enableDynamicPayloads()radio.openWritingPipe(pipes[1])radio.openReadingPipe(1, pipes[0])radio.printDetails()while True:    message = list("Y")    #Only works when text is inside of list        radio.write(message)    print("Sent: {}".format(message))        time.sleep(0.5)
Slave File

Code:

import RPi.GPIO as GPIOfrom lib_nrf24 import NRF24import timeimport spidevGPIO.cleanup()GPIO.setmode(GPIO.BCM)pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]radio = NRF24(GPIO, spidev.SpiDev())radio.begin(0, 17)radio.setPayloadSize(32)radio.setChannel(0x60)radio.setDataRate(NRF24.BR_2MBPS)radio.setPALevel(NRF24.PA_MIN)radio.setAutoAck(True)radio.enableDynamicPayloads()radio.openWritingPipe(pipes[0])radio.openReadingPipe(1, pipes[1])radio.printDetails()radio.startListening()while True:    while not radio.available(0):        time.sleep(1/100)    receivedMessage = []    radio.read(receivedMessage, radio.getDynamicPayloadSize())    print("Received: {}".format(receivedMessage))    string = ""    for n in receivedMessage:        if 32 <= n <= 126:            string += chr(n)    print("Translated: {}".format(string))    if string == "Y":        print("PASS received Y")    else:        print("ERROR not Y")

Statistics: Posted by rowblocks — Mon Feb 17, 2025 2:02 am



Viewing all articles
Browse latest Browse all 1264

Trending Articles