Go Down

Topic: [Solved] NRF24L01, Receiving and displaying multiple integers correctly  (Read 82 times) previous topic - next topic

bryceikeda

I have two NRF24L01 hooked up to two Arduinos. Arduino 1 first sends a 1 to Arduino 2 and then Arduino 2 sends back 4 integers in a structure I call payload. The four numbers I send are:
x = 200
y = 300
z = 99
a = 10

When I receive the numbers and try to display the structure through the serial monitor on Arduino 1, I get the numbers:
x = 200
y = 300
z = 0
a = 0

In addition, if i try sending those four numbers using an array instead of a structure I get the numbers:
x = 200
y = 300
z = 16138
a = 16358

Below is the code I am using.

Transmitter:
Code: [Select]
#include <SPI.h>
#include "RF24.h"
int msg[1] = {1};
struct Payload
{
   int x;
   int y;
   int z;
   int a;
} payload;
/****************** User Config ***************************/

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(10,9);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24 Transmitter"));
  struct Payload payload;
  radio.begin();

  radio.setPALevel(RF24_PA_LOW);
 
  radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1,addresses[0]);
 
  // Start the radio listening for data
  radio.startListening();
}

void loop() {
 
    radio.stopListening();                                    // First, stop listening so we can talk.
   
    Serial.println(F("Now sending"));
    if (!radio.write( &msg, sizeof(unsigned long) )){
      Serial.println(F("failed"));
    }
       
    radio.startListening();                                    // Now, continue listening
   
    unsigned long started_waiting_at = micros();               // Set up a timeout period, get the current microseconds
    boolean timeout = false;                                   // Set up a variable to indicate if a response was received or not
   
    while ( ! radio.available() ){                             // While nothing is received
      if (micros() - started_waiting_at > 200000 ){            // If waited longer than 200ms, indicate timeout and exit while loop
          timeout = true;
          break;
      }     
    }
       
    if ( timeout ){                                             // Describe the results
        Serial.println(F("Failed, response timed out."));
    }else{
                             
        radio.read( &payload, sizeof(unsigned long) ); // read what is sent back
        // display what is recieved
        Serial.print(F("Sent "));
        Serial.print(msg[0]);
        Serial.print(F(", Got response: x: "));
        Serial.print(payload.x);
        Serial.print(F(" y: "));
        Serial.print(payload.y);
        Serial.print(F(" z: "));
        Serial.print(payload.z);
        Serial.print(F(" a: "));
        Serial.println(payload.a);
    }

    // Try again 1s later
    delay(1000);
}


and Receiver

Code: [Select]
#include <SPI.h>
#include "RF24.h"
struct Payload
{
   int x = 200;
   int y = 300;
   int z = 99;
   int a = 10;
} payload;
/****************** User Config ***************************/

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(10,9);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};

void setup() {
  struct Payload payload;
  Serial.begin(115200);
  Serial.println(F("RF24 Reciever"));
  Serial.println(sizeof(unsigned long));
  radio.begin();

  radio.setPALevel(RF24_PA_LOW);
 
  radio.openWritingPipe(addresses[0]);
  radio.openReadingPipe(1,addresses[1]);
 
  // Start the radio listening for data
  radio.startListening();
}

void loop() {
    unsigned int rec;
    if( radio.available()){
                                                                    // Variable for the received timestamp
      while (radio.available()) {                                   // While there is data ready
        radio.read( &rec, sizeof(unsigned long) );             // Get the payload
      }
     
      radio.stopListening();                                        // First, stop listening so we can talk   
      radio.write( &payload, sizeof(unsigned long) );              // Send the final one back.     
      radio.startListening();
      Serial.print(F("Received "));
      Serial.print(rec);  // Now, resume listening so we catch the next packets.     
      Serial.print(F(", Sent response x: "));
      Serial.print(payload.x); 
      Serial.print(F(" y: "));
      Serial.print(payload.y); 
      Serial.print(F(" z: "));
      Serial.print(payload.z); 
      Serial.print(F(" a: "));
      Serial.println(payload.a);
   }
} // Loop


Any idea what I am doing incorrectly?

Robin2

Life will be much easier if you send the complete struct - something like this

Code: [Select]
radio.write( &payload, sizeof(payload) )

and receive if with

Code: [Select]
radio.read( &payload, sizeof(payload) );

...R
Simple nRF24L01+ Tutorial
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

You are throwing away packets if there ever happen to be more than one available, why?

Code: [Select]
      while (radio.available()) {                                   // While there is data ready
        radio.read( &rec, sizeof(unsigned long) );             // Get the payload
      }
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

bryceikeda


Go Up