Robin2:
Sorry, but long experience here has made me doubtful about that sort of response.
I am using the same program on both arduinos with the same counterpart on the pi so I thought I basically ruled out a bad configuration.
But yeah, I get it here you go, freshly commented and everything
(if you dont want to read everything i summarized the important stuff at the end):
Arduino:
#include<SPI.h>
#include<RF24.h>
RF24 radio(7,8); //RF24 radio(48,53) for MEGA RF24 radio(ce_pin,cs_pin)
int i = 0; //Counter to add to my message
void setup() {
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0x76); //Setup stuffs if i did something wrong its probably here as I dont fully understand all of it.
radio.openWritingPipe(0xF0F0F0F0E1LL);
const uint64_t pipe = 0xE8E8F0F0E1LL;
radio.openReadingPipe(1,pipe);
radio.enableDynamicPayloads();
radio.powerUp();
radio.startListening();
}
void loop() {
Serial.println("Starting loop. Radio on.");
char receivedMessage[32] = {0};
if(radio.available()){ //A message was received
radio.read(receivedMessage, sizeof(receivedMessage)); //Writes the received message to my char array
Serial.println(receivedMessage); //writes it to the monitor
Serial.println("Turning off radio"); //getting ready for the response
radio.stopListening();
String stringMessage(receivedMessage);
Serial.println(receivedMessage); //turns it to a string to compare with known commands in the next step
if(stringMessage == "GETSTRING"){ //if its a certain keyword...
String returnMsg = "SensorDaten: ";
returnMsg.concat(i);
// Serial.println(i);
const char text[returnMsg.length()+ 1] ;// = returnMsg;
returnMsg.toCharArray(text,returnMsg.length() + 1);
radio.write(&text,sizeof(text)); //respond with an everchanging integer i and constant string "Sensordaten"
Serial.println("Sent String"); //for the monitor
}
radio.startListening();
}
//radio.stopListening(); //THESE 4 LINES WILL MAKE THE ARDUINO WRITE TO THE PI NO MATTER WHAT
//const char text[] = "Hello Pi"; //THIS IS HOW I KNOW THAT THE ARDUINO IS CAPABLE OF WRITING AS I CAN RECEIVE THE MESSAGE ON MY PI
//radio.write(&text,sizeof(text));
//radio.startListening();
delay(100);
i++;
}
Pi (Python):
import spidev
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
GPIO.setmode(GPIO.BCM)
#weird setup Stuffs
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1],[0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
#RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0,17) #22/8
radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MAX)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1,pipes[1])
radio.printDetails()
#radio.startListening() #finished setup stuffs
message = list("GETSTRING") #the command to write to the arduino
while len(message) < 32:
message.append(0) #adding zeros to the rest of the 32 bytes for weird behaviour?
while True:
start = time.time() #timeout to wait for a response
radio.write(message)
print("Sent the message: {}".format(message)) #wrote the command
radio.startListening()
while not radio.available(0): #waiting for a response or basically any message
time.sleep(1/100)
# print("Geht Nich")
if time.time() - start > 2: #wait for a maximum of 2 seconds and then time out
print("Timed out.")
break
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize()) //received message is written to a variable
print("Received: {}".format(receivedMessage)) #outputs the received message
print("Translating into unicode...")
string = ""
for n in receivedMessage:
if (n >=32 and n<= 255): #turns the whole thing into ascii characters
string += chr(n)
print("Received Message decodes to: {}".format(string))
radio.stopListening()
time.sleep(1) #wait a second
So the Pi sends the command "GETSTRING" to my arduino every second. This command is instantly printed to the serial Monitor (only on the uno). The Arduino then responds with ("Sensordaten: " + integer) and this is printed to the pi console. Since the MEGA doesn't enter this part of the program at all I added 4 comments at the bottom part of the arduino program which will constantly send the message "Hello Pi" and since the pi is dumb it thinks that's the response to the GETSTRING command and prints this to the console.