NRF24l01 not reciving data properly

I am working on a project and i want to send specific data from a mega to a nano. The mega seems to be transmitting correct data but the nano recives sometyng else. This is the data that is being sent:


and this is the data that is being recived: .

And also here is the code for the transmitter:

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

Servo steerservo;
const unsigned int MAX_MESSAGE_LENGHT = 12;
const int button = 6;
const int xcontrol = 0;
const int ycontrol = 1;


RF24 myRadio (7, 8);
byte addresses[6] = ("0");

struct package
{
  int id=1;
  float temperature = 18.3;
  char text[100] = "Message to be recived";
};


typedef struct package Package;
Package data;

void setup()
{
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);
  steerservo.attach(5);
  Serial.begin(115200);
  delay(10);
  myRadio.begin();
  myRadio.setChannel(115);
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS );
  myRadio.openWritingPipe( addresses[0]);
  delay(1000);
}

void loop(){
  Serial.print("b");
  Serial.print(digitalRead(button));
  Serial.print("\n");
  Serial.print("x");
  Serial.print(analogRead(xcontrol));
  Serial.print("\n");
  Serial.print("y");
  Serial.print(analogRead(ycontrol));
  Serial.print("\n\n");
  myRadio.write(&data, sizeof(data));

  Serial.print("\n");
  Serial.print(data.id);
  Serial.print("\n");
  Serial.println(data.temperature);
  Serial.println(data.text);
  data.id = data.id + 1;
  data.temperature = data.temperature+0.1;
  delay(10);
}

and the reciver:

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

RF24 myRadio (7, 8);
const unsigned int mml = 12;
char messagerecived = "nope";
String string = "0";
struct package{
  int id=0;
  float temperature = 0.0;
  char text[100] = "empty";
};

byte addresses[6] = ("0");


typedef struct package Package;
Package data;

void setup(){
  Serial.begin(115200);
  delay(10);

  myRadio.begin();
  myRadio.setChannel(115);
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS );
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}

void loop(){
  while (Serial.available() > 0){
  static char message[mml];
  static unsigned int mpos = 0;

  char inByte = Serial.read();
  if (inByte != "\n" && (mpos < mml - 1)){
    message[mpos] = inByte && string;
    mpos++;
    
  }
  else{
    message[mpos] = "\0";

    char messagerecived = (message);
    Serial.println(message);
  }
  }
  if ( myRadio.available()){
    while (myRadio.available()){
      myRadio.read( &data, sizeof(data) );
    }
    Serial.print("\nPackage:");
    Serial.print(data.id);
    Serial.print("\n");
    Serial.println(data.temperature);
    Serial.println(data.text);
    
  }
  if(string.startsWith("b")){
    string[0, !1];
    string.toCharArray(messagerecived, mml);
    Serial.println(messagerecived);
  }
  
}

Sorry I cannot read your screen shots.

The maximum packet size for an NRF24L01 is 32.

You should pass an address, not the content of a byte, to be misused as an address.

1 Like

I fixed the issue, thank you so much.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.