How do I read from sprintf after data is sent with NRF?

Hi all,

I have been having issues getting variables to ALL come across when using two Arduino Nanos and NRF modules.

Sending them seprately was not working as by the 4th one they were not received.

Someone mentioned using sprintf a while back and I can confirm I am now getting the data all at once.

Only problem is, the data displays correctly when using Serial.print(data); but I can't seem to pull out the individual values.

I tried accessing it by assigning one variable in the array to another and printing, but this results in an incorrect result.

IE When sending 5 values I get

8000 2222 3333 4444 5555 in Serial monitor Serial.println(data) - from the Sending device

8000 2222 3333 4444 5555 in Serial monitor Serial.println(data) - on the Receiving device

So thats great! (I couldn't get more than 3 results trying it other ways)

But trying to pull out one of those numbers results in a random value instead of the value in serial monitor (Usually a 1 or a 3).

I was trying

int pleasework = sprintf(data,"%u", xPos);
Serial.println(pleasework);

Full code below, hope that all made sense,

I'm assuming it's just the way I am trying to access the received data and will be a simply, no, you need to format it like this solution.

Transmitter

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

RF24 radio(7, 8); // CE, CSN        
 
const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

// Variables

char data[100];
int line1 = 8000;
int line2 = 2222;
int line3 = 3333;
int line4 = 4444;
int line5 = 5555;


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

      
      
      
      radio.begin();                  //Starting the Wireless communication
      radio.openWritingPipe(address); //Setting the address where we will send the data
      radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
      radio.stopListening();          //This sets the module as transmitter
}

void loop()
{

  
sprintf(data,"%u %u %u %u %u",line1,line2,line3,line4,line5);

radio.write(&data, sizeof(data));                  //Sending the message to receiver

Serial.println(data);


delay(1000);



}

Receiver

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

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

char data[100];
int line1;
int line2;
int line3;
int line4;
int line5;

void setup() 
{

Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening();              //This sets the module as receiver
}

void loop()
{
  delay(20);
  if (radio.available())              //Looking for the data.

      {


        // read variables from sender
        radio.read(&data, sizeof(data));              
        

        
        // print to monitor
        Serial.println(data);
        
        int XYZ = sprintf(data, "%u",line1);
        Serial.println(XYZ);
        
        delay(1000);
 
      }

}

Thank you
D

But trying the get one of those numbers results in a '1' instead of the value.

In this code, or some code you didn't post?

If you want the values at the Receive side to be numeric, I don't understand why you're bothering to convert them to ASCII, sending them (inefficiently), and then trying to convert them back to numeric at the other end. That's a lot work for no value added. Why not just build a struct, send it in binary, and load the data into an identical struct on the Receive side?

gfvalvo:
If you want the values at the Receive side to be numeric, I don't understand why you're bothering to convert them to ASCII, sending them (inefficiently), and then trying to convert them back to numeric at the other end. That's a lot work for no value added. Why not just build a struct, send it in binary, and load the data into an identical struct on the Receive side?

Thank you. Could you point me towards an example? I’m not sure what a struct is. Still pretty newb

aarg:
In this code, or some code you didn't post?

This one :slight_smile:

DisasterofPuppets:
Thank you. Could you point me towards an example? I’m not sure what a struct is. Still pretty newb

Google to the rescue:
http://www.cplusplus.com/doc/tutorial/structures/
https://www.learncpp.com/cpp-tutorial/47-structs/

gfvalvo:
Google to the rescue:
http://www.cplusplus.com/doc/tutorial/structures/
https://www.learncpp.com/cpp-tutorial/47-structs/
C++ Structures

Thank you. Makes sense. I was trying to figure out how to do it in an array like that. Will give that a shot. Thanks again.