I have been testing communication between two nrf24l01 modules for the past days and I have recentely started developing my own code for them, I was inspired by a video from HowToMechatronics.
My setup is the same as DroneBot Workshop's video and I use airspayce's RadioHead library.
My goal with the following code is to create an easy and fast way to switch between transmitter and receiver code.
I also plan on expanding this project so that I can integrate it in the RC transmitter.
The problem is that the server receives corrupted messages (last screenshot) when running the following code, but it works perfectly fine when using the example sketches.
This is the code
controller_reliable:
#define STATE 2 // 1 = server | 2 = client
#include "transmission.h"
#define SERVER_ADDRESS 2
RH_NRF24 driver;
RHReliableDatagram manager(driver, SERVER_ADDRESS);
#if STATE == 1
// Message to send back to the server
uint8_t data[] = " ";
#elif STATE == 2
// Message sent to the server
uint8_t data[] = "Hello";
#endif
void setup() {
Serial.begin(9600);
if (!manager.init())
Serial.println("init failed");
}
void loop() {
#if STATE == 1
manageServer(data, manager);
#elif STATE == 2
sendToServer(data, manager, SERVER_ADDRESS);
#endif
delay(500);
}
transmission.h:
#pragma once
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
void sendToServer(byte data[], RHReliableDatagram manager, int SERVER_ADDRESS);
void manageServer(byte data[], RHReliableDatagram manager);
transmission.cpp:
#include "transmission.h"
void sendToServer(byte data[], RHReliableDatagram manager, int SERVER_ADDRESS)
{
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
Serial.println("sendToServer (client) function called");
// Send data
if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
{
// Now wait for a reply from the Server
uint8_t len = sizeof(buf);
uint8_t from;
// Receve the ack from the Server, and possibly a message | THIS DOES NOT WORK
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.println("The Server sent a message:");
Serial.println((char*)buf);
}
else { /* Timeout, probably the Server was turned off ?*/ }
}
}
void manageServer(byte data[], RHReliableDatagram manager)
{
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
if (manager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
Serial.println("manageServer (server) function called");
if (manager.recvfromAck(buf, &len, &from))
{
Serial.println("The client sent a message:");
Serial.println((char*)buf);
// Send a message to the client
if(manager.sendtoWait(data, sizeof(data), from))
Serial.println("SendToWait succeded");
else
Serial.println("SendToWaitFailed");
}
}
}
The problem with this code is that the server picks up a bunch of garbage data
At first I thought this might have been because of my 2.4Ghz wifi or because I had made some mistakes with the wiring, but turning turning off the wifi gives the same result, and the example library sketches work correctly
(nrf24_reliable_datagram_client | nrf24_reliable_datagram_server).
What I think this means is that there is an issue with my code, but I really can't figure out what the problem is, I've tried reading documentation and watching videos, but I don't think I have the skill required for solving this problem.
I use two arduino clones to control the modules: one from Sunfounder and one from AZdelivery, but I don't think this might be the problem.

