0

This is my Master's code.

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

RF24 radio(7, 8);

const byte address[6] = "00001";
char text[20] = "";
int flag;
char *p;
int i;

void print();
void read();
void rfwrite();
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_1MBPS);
  radio.setChannel(1);
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  flag = 0;
  p = text;
}

void loop() {


  if (Serial.available()) {
    read();

  }
  if (flag) {
    print();
    rfwrite();

  }


}

void print() {
  Serial.println(text);
  flag = 0;
}

void read() {
  p = &text[0];
  for (i = 0; Serial.available(); p++, i++) {
    *p = Serial.read();
  }
  text[i + 1] = '\0';
  flag = 1;
}

void rfwrite() {

  radio.startWrite(text, strlen(text)+1);


}

Slave code:

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

RF24 radio(7, 8); //ce,csn

const byte address[6] = "00001";
char text[20] = "";
int  i;

void write_to_array();
void print();
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_1MBPS);
  radio.setChannel(1);
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  while (radio.available()) {

    write_to_array();
    print();

  }


}

void write_to_array() {
  radio.read(text, strlen(text)+1);

}


void print() {
  Serial.println(text);

}

I edited the code and made it with an array like @Kwasmich said. But now some end characters are not getting printed. When I type 1234567890-= into the transmitter, only 1234567890- gets printed. When I type a new set of characters following that, the missing = gets printed. Also, this happens only on the second transmission after I upload the code to the Arduino. The first set of characters are received and printed correctly. How do I fix this? Will flushing the buffer help(I doubt this because the first transmission is received correctly)?

  • 2
    Your code char text = "" can only hold a single character, not a string. You either need a char array or a char pointer. Likewise sizeof(text) is 1 because this is always a single character. – Christian Lindig Mar 5 at 18:09
  • @ChristianLindig, but that is not the cause of the missing character – Juraj Mar 5 at 19:04
  • Your code currently sends the message only one character at a time. Not sure where the missing character comes in, but I'm pretty sure (from experience), that when you fix the single character issue, your missing characters problem goes away as well. – Gerben Mar 5 at 19:50
  • I cannot test it right now, but I guess the problem is in your slave Code, the initialization of text = "". Therefore the sizeof(text) in the initial run is 0, so something in radio.read(&text, sizeof(text)) will go wrong. Could you check with a diffenent initalization of text, e.g. "0"? – RJPlog Mar 5 at 20:41
  • @RJPlog I've tried what you suggested, initializing the text as 'a'. The output remained the same although I think sometimes I noticed that the first transmission after uploading the code to arduino resulted in successfully receiving all the characters, I can't say for sure. – Kishore Mar 11 at 6:59
0

This is not a proper solution explaining why the issue occurred in the first place but merely a workaround to anyone encountering the same problem.

There is another function in the RF24 library called startWriting(). The description in the library was not clear about the differences between this and write(). But I noticed that the last character was missing with the use of startWriting(). So I simply changed

radio.read(&text, sizeof(text));

in the master to

 radio.read(&text, sizeof(text+1));

and voila! The code now does its intended purpose!!

0

You are riding on a wave of undefined behavior. sizeof returns the size of the underlying type. In your case char. Which is 1. What you really need is the propper allocated array char text[32] = "" and the required length of the string using strlen(text) + 1. +1 to also transmit the nul terminator of the string.

The maximum string Length that can be transmitted using nRF24L01 is 32 bytes. Because of the mandatory nul termination of a string you can send at most 31 characters as a valid string.

Fun fact: The same limitation of 31 characters applies to file name langths on the classic Mac OS at least up to 7.x and also on the Amiga DOS/Workbench.

  • Please check the new code in the question. I've edited like you suggested. – Kishore Mar 12 at 14:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.