Noob Question - Sending Data With NRF24L01

Since this is my first time posting, this message is so that I can put a link in my question.

Hello all, I've just started working with Arduino, having almost zero prior electronics or coding experience. My background is mostly a course I took coding in C++.

The project I'm currently working on is to have one Arduino read a potentiometer and send the data via an NRF24L01 module (SparkFun Transceiver Breakout - nRF24L01+ - WRL-00691 - SparkFun Electronics) to another Arduino with the same module. Right now, I have them both interfaced with USB, and I'm trying to get serial printouts of the data being sent.

I read this page: Arduino Playground - Nrf24L01 and started by modifying the examples that came with the mirf library. The sending Arduino is running the following:

#include <Spi.h>
#include <mirf.h>
#include <nRF24L01.h>

void setup(){
  Serial.begin(9600); 
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.config();
  Mirf.payload = sizeof(int);
}

void loop(){
  //byte data[Mirf.payload];
  Mirf.setTADDR((byte *)"serv1");
  
  Mirf.send((byte *)"hello");
  
  while(Mirf.isSending()){
  }
  Serial.println("Finished sending");
  delay(10);
}

and the receiving Arduino is running:

#include <Spi.h>
#include <mirf.h>
#include <nRF24L01.h>

void setup(){
  Serial.begin(9600); 
  Mirf.init(); 
  Mirf.setRADDR((byte *)"serv1");
  Mirf.config();
  Mirf.payload = sizeof(int);
}

void loop(){
  byte data[Mirf.payload];

  if(Mirf.dataReady()){
    
    do{
      Serial.println("Got packet");
     
      Mirf.getData(data);
      
      Serial.println(data[0]);
      
    }while(!Mirf.rxFifoEmpty());
  }
}

Now as long as I'm sending a string, such as "hello", I can print out the received array one letter at a time (or more, if I use a for loop). However, if I send a number, the printed result doesn't seem to correspond at all. That is, if the sending syntax is:

Mirf.send((byte *)1024);

What am I doing wrong/missing? Do I need to format the sent data as an array, which is why sending a string works? Please help me out!

Thanks.

Is it possible you're sending an ascii number, and the result on the receiving side is the decimal equivalent of the number you sent? Ascii chart located http://www.asciitable.com/

I thought about that, but no, the numbers seem completely unrelated, and most of them are out of the range of the ascii chart.

Hi

I think the code you've got there will try and send sizeof(int) bytes located at memory address 1024. You need to create a variable with your number in first:

int temp = 1024;
Mirf.send((byte *) &temp);

Mirf.send((byte *)1024);

What is the data at address 1024?

Hello again, thanks for the responses. 1024 is just an arbitrary number that I tried sending.

I tried aaronds suggestions of creating a variable to hold the number (something I had done before), but on the receiving end it prints a blank line. If I cast the received data as an integer, the output still seems arbitrary.

Serial.println((int)data[0]);

When using 1024, the output for data[0] is 0, the output for data[1] is 4, data[2] is 194 and data[3] is 0 again. I thought the array would only be that long, but data[4] is 0 and data[5] is 15. I haven't felt like checking beyond that.

Try something like this on the server:

int data;

Mirf.getData((byte *) &data);
Serial.println(data);

I think it actually working it's just not being printed out right.

Holy Crap. I don't fully understand it, but it works! Thanks so much!

Hello again, thanks for the responses. 1024 is just an arbitrary number that I tried sending

No, it isn't arbitrary, it is the address of some data that the routine expects - you even cast it to be a byte pointer.