1

We are trying to send a message using NRF24L01, raspberry pi as transmitter and arduino uno as receiver. Our concern is, the receiver arduino might not be working because we are not getting the proper output message on the serial monitor. Code for the transmitter side (python code):

import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)

radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)

radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
#radio.startListening()

message = list("GETSTRING")
while len(message) < 32:
    message.append(0)

while True:
    start = time.time()
    radio.write(message)
    print("Sent the message: {}".format(message))
    radio.startListening()

    while not radio.available(0):
        time.sleep(1/100)
        if time.time() - start > 2:
            print("Timed out.")
            break

    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
    print("Received: {}".format(receivedMessage))

    print("Translating our received Message into unicode characters...")
    string = ""

    for n in receivedMessage:
        if (n >= 32 and n <= 126):
            string += chr(n)
    print("Our received message decodes to: {}".format(string))

    radio.stopListening()
    time.sleep(1)

Code used for the receiver side (arduino):

#include<SPI.h>
#include<RF24.h>

// ce, csn pins
RF24 radio(9, 10) ;

void setup(void){
  while (!Serial) ;
  Serial.begin(9600) ;

  radio.begin() ;
  radio.setPALevel(RF24_PA_MAX) ;
  radio.setChannel(0x76) ;
  radio.openWritingPipe(0xF0F0F0F0E1LL) ;
  const uint64_t pipe = 0xE8E8F0F0E1LL ;
  radio.openReadingPipe(1, pipe) ;

  radio.enableDynamicPayloads() ;
  radio.powerUp() ;

}

void loop(void){
  radio.startListening() ;
  Serial.println("Starting loop. Radio on.") ;
  char receivedMessage[32] = {0} ;
  if (radio.available()){
    radio.read(receivedMessage, sizeof(receivedMessage));
    Serial.println(receivedMessage) ;
    Serial.println("Turning off the radio.") ;
    radio.stopListening() ;

    String stringMessage(receivedMessage) ;

    if (stringMessage == "GETSTRING"){
      Serial.println("Looks like they want a string!") ;
      const char text[] = "Hello World!" ;
      radio.write(text, sizeof(text)) ;
      Serial.println("We sent our message.") ;
    }

  }
  delay(100) ;
}
  • why would you single out the receiver as being the cause of the problem? – jsotola Mar 3 at 8:50
  • I am getting the output in raspberry pi as successfully sent the message. But the message is not showing in arduino serial monitor. That's why I think the problem is with the receiver side.Maybe the receiver is not decoding the message properly. – Rangit Ray Mar 3 at 17:34
  • What is "Proper output" you're expecting with your code? – Avamander Mar 3 at 23:10
  • I am trying to send the message "GETSTRING" in the form of a list from raspberry pi which I want to get in the serial monitor of the arduino. In the arduino I am giving the command Serial.println(receivedMessage), but it is not getting printed. – Rangit Ray Mar 4 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.