Go Down

Topic: NRF24l01+ payload size problem (Read 129 times) previous topic - next topic

jacobrejin

I am using the latest TMRh20 library for Nrf24l01+ modules

I am trying to send a two byte long data.
so I am using the setPayloadSize (uint8_t size) function on both the TX and RX node.

But its not working.
The code works fine with the default payload size of 32 bytes but changing it causes no reception.

below is the code

TX

Code: [Select]
#include <SPI.h>
#include "RF24.h"

RF24 radio(7,8);
uint8_t address[][6] = {"1Node","2Node"};

static char data[]="OK";

volatile bool flag=0;

void setup()
{
  Serial.begin(115200);
  Serial.println("serial monitor is working");
  delay(1);
 
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.openWritingPipe(address[0]);
  radio.setAutoAck(false);
  radio.setChannel(125);
  radio.setPayloadSize(2);
 
  pinMode(5,OUTPUT);
  attachInterrupt(digitalPinToInterrupt(2), RadioTx,LOW);
}

void loop()
{
  //unsigned long time;
  while(!flag);

  //time = micros();
  //Serial.println(time);
 
  radio.stopListening();
  radio.write( &data, sizeof(data) );
  radio.flush_tx();
  radio.write( &data, sizeof(data) );
  radio.flush_tx();
  radio.powerDown();

  //time = micros();
  //Serial.println(time);
  delay(1);
  while(1)
  {
    digitalWrite(5,HIGH);
    delay(1000);
    digitalWrite(5,LOW);
    delay(1000);
  }
}

void RadioTx()
{
  detachInterrupt(digitalPinToInterrupt(2));
  flag=1;
}


RX

Code: [Select]
#include <SPI.h>
#include "RF24.h"

RF24 radio(7,8);
uint8_t address[][6] = {"1Node","2Node"};
static char check[]="OK";

void setup()
{
  Serial.begin(115200);
  Serial.println("serial monitor is working");
  delay(1);
 
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openReadingPipe(1,address[0]);
  radio.setAutoAck(false);
  radio.setChannel(125);
  radio.setPayloadSize(2);
  radio.startListening();

  pinMode(5,OUTPUT);
}

void loop()
{
  char data[2];
  if(radio.available())
  {
    while(radio.available())
    {
      radio.read(&data,sizeof(data));
    }
    //Serial.println(data);
    //delay(1000);
    if(strcmp(data, check)==0)
    {
      Serial.println(data);
      radio.powerDown();
      while(1)
      {
        digitalWrite(5,HIGH);
        delay(1000);
        digitalWrite(5,LOW);
        delay(1000);
      }
    }
  }           
}


if i comment out the setPayloadSize (uint8_t size) function the code works properly.

I have attached the capacitors as recommended both 0.1 and 10uf also the modules are being powered from an external 3.3v supply. grounds of both power supply and arduino are connected.
1 node is run by uno 2nd node by nano

Robin2

The code works fine with the default payload size of 32 bytes but changing it causes no reception.
So don't change it.

This line creates a 3 element char array - the third element holds the terminating NULL
Code: [Select]
static char data[]="OK";

but in your receiving code you only have a 2 element array
Code: [Select]
  char data[2];


Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jacobrejin

#2
Apr 19, 2019, 06:56 pm Last Edit: Apr 19, 2019, 06:58 pm by jacobrejin
Ok I tried the simple one way transmission example and it works i guess.

this is the output of serial monitor from TX side

Data Sent Message 8  Acknowledge received
Data Sent Message 9  Acknowledge received
Data Sent Message 0  Acknowledge received
Data Sent Message 1  Acknowledge received
Data Sent Message 2  Acknowledge received
Data Sent Message 3  Acknowledge received
Data Sent Message 4  Acknowledge received


some problem with the RX side. It only prints the first few lines the the serial monitor stops printing

SimpleRx Starting
Data received
Data received
Data received
Data received
Data received


What should be the next step in debugging the problem

Robin2

What should be the next step in debugging the problem
Please post the exact programs that YOU uploaded to your Arduinos.

How often is the Data Received message appearing?  It should be about once per second.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jacobrejin

I uploaded the Simple one way transmission example from the post you mentioned

here is the code

TX
Code: [Select]


// SimpleTx - the master or the transmitter

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


#define CE_PIN   9
#define CSN_PIN 10

const byte slaveAddress[5] = {'R','x','A','A','A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "Message 0";
char txNum = '0';


unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

//====================

void loop() {
    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
        prevMillis = millis();
    }
}

//====================

void send() {

    bool rslt;
    rslt = radio.write( &dataToSend, sizeof(dataToSend) );
        // Always use sizeof() as it gives the size as the number of bytes.
        // For example if dataToSend was an int sizeof() would correctly return 2

    Serial.print("Data Sent ");
    Serial.print(dataToSend);
    if (rslt) {
        Serial.println("  Acknowledge received");
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }
}

//================

void updateMessage() {
        // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[8] = txNum;
}


RX
Code: [Select]


// SimpleRx - the slave or the receiver

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

#define CE_PIN   9
#define CSN_PIN 10

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}


As soon as i reset the arduino uno (The RX) it prints 5 lines of data received message and then stops printing.

but the transmitter keeps working fine

Robin2

As soon as i reset the arduino uno (The RX) it prints 5 lines of data received message and then stops printing.
You have not said how fast the messages get printed - that is an important piece of information.

The receiver should be printing more than the words "data received". It should also print the message that was received.

What does the Tx program do if you switch off the Rx Arduino?

Have you tried the connection test program to see if both Arduinos can communicate properly with the nRF24 they are attached to?

How are you powering the nRF24s? Try powering them with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

Try the second program in my tutorial as it requires the "Rx" to send data back to the "Tx"

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up