nrf24l01+ receiver keeps receiving the same payload in a loop

Hi,
I have 2 wireless nodes (transceivers) which sends temperature, humidity, voltage, count and id to the receiver. Transceivers are Arduino Pro Mini + nrf24l01+ and DHT22. Receiver is Arduino Nano + nrf24l01+ and enc28j60 ethernet + ds18b20 temp sensor.
Everything works, but from time to time receiver starts to repeatably receive “the same payload”. I can reproduce the issue by powering off one of the wireless transceiver, then wait a while, power up and receiver will start the never ending loop with “receiving the same payload” even when transceiver is sending new payloads (with new values). Transceiver sends data every 8 seconds. So it looks the receiving nrf24l01+ or arduino somehow “keeps” the received payload and pretend it is being still received. I am helpless and tired of debugging and exchanging hardware, so maybe someone with fresher mind can help me finding the problem in my code. When I reset the transceiver, it is OK - it receives properly, no duplicated payloads.

Here is the output of the apache log where values from transceivers are being saved:

192.168.2.221 - - [31/Mar/2015:21:21:15 +0200] "GET /nrf.php?temp=25.10&hygro=43.30&voltage=3.0000&meranie=1&auth=secret12&mode=dht&s=1 HTTP/1.1" 200 206 "-" "-"
192.168.2.221 - - [31/Mar/2015:21:21:16 +0200] "GET /nrf.php?temp=25.10&hygro=43.30&voltage=3.0000&meranie=1&auth=secret12&mode=dht&s=1 HTTP/1.1" 200 206 "-" "-"
192.168.2.221 - - [31/Mar/2015:21:21:17 +0200] "GET /nrf.php?temp=25.10&hygro=43.30&voltage=3.0000&meranie=1&auth=secret12&mode=dht&s=1 HTTP/1.1" 200 206 "-" "-"
192.168.2.221 - - [31/Mar/2015:21:21:18 +0200] "GET /nrf.php?temp=25.10&hygro=43.30&voltage=3.0000&meranie=1&auth=secret12&mode=dht&s=1 HTTP/1.1" 200 206 "-" "-"
192.168.2.221 - - [31/Mar/2015:21:21:19 +0200] "GET /nrf.php?temp=25.10&hygro=43.30&voltage=3.0000&meranie=1&auth=secret12&mode=dht&s=1 HTTP/1.1" 200 206 "-" "-"
192.168.2.221 - - [31/Mar/2015:21:21:20 +0200] "GET /nrf.php?temp=25.10&hygro=43.30&voltage=3.0000&meranie=1&auth=secret12&mode=dht&s=1 HTTP/1.1" 200 206 "-" "-"

So you can see it sends the same data (“meranie=1” means counter which sends transceiver, should be increasing) every 1 second.

I put some debugging serial prints to see what is happening, but found nothing - it looks like a proper receiving of the paloads, but it is not true in reality - as transceiver is not sending the same value (checked as well on transceiver side) and so often.

Can anybody help by checking my code for some suspicious stuff? I use maniacbug’s rf24 library. Here is the receiver code:

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <UIPEthernet.h>
#include "printf.h"
#include <OneWire.h>
#include <DallasTemperature.h>

char hygro_buff[8];
char temp_buff[8];
char volt_buff[8];
char t0buff[8];
DeviceAddress spalnaVzduch = { 0x28, 0x7A, 0x34, 0x11, 0x05, 0x00, 0x00, 0x50 };

// DS8B20 Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// set this to the number of milliseconds delay
#define delayMillis 300000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = 0;
int startup = 0;

// struct used for transferring data wirelessly
typedef struct
{
    int A;
    float B;
    float C;
    float D;
    char E;
} data;

data payload;

bool sent = 0; // control variable for sending data via ethernet
unsigned int retries = 0; // control variable for controlling retrying of sending via ethernet
#define MAX_RETRIES 3 //max amount of attampts for ethernet sending

//
// Ethernet config
//
// ethernet mac adress - this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
IPAddress ip(192,168,2,221);
IPAddress gateway(192, 168, 2, 106);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
char pageAdd[95];
//
// End - Ethernet config
//


//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus
RF24 radio(5,6);
// Radio pipe addresses for the 2 nodes to communicate.
const PROGMEM uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup(void)
{
    Serial.begin(57600);
    printf_begin();
    // Start ethernet
    Ethernet.begin(mac, ip, gateway, gateway, subnet);
    delay(2000);

    // Setup and configure rf radio
    radio.begin();
    radio.setChannel(2);
    radio.setPALevel(RF24_PA_MAX);
    radio.setDataRate(RF24_250KBPS);
    radio.setPayloadSize(16);
    radio.openReadingPipe(1,pipes[0]);
    radio.printDetails();
    radio.startListening();

    // one wire
    sensors.begin();
    sensors.setResolution(spalnaVzduch, 12);

    startup=1; // indicates th arduino was started or reset - if 1 send first values to server immediately
}

void loop(void)
{
    // if there is data ready
    if ( radio.available() )
    {
        bool done = false;
        while (!done)
        {
            // Fetch the payload, and see if this was the last one.
            done = radio.read( &payload, sizeof(payload) );
            Serial.print(F("Radio read: "));
            Serial.println(done);
            dtostrf(payload.B,1,2,temp_buff);
            dtostrf(payload.C,1,2,hygro_buff);
            dtostrf(payload.D,1,4,volt_buff);
            sprintf(pageAdd,"/nrf.php?temp=%s&hygro=%s&voltage=%s&meranie=%d&auth=secret12&mode=dht&s=%c",temp_buff,hygro_buff,volt_buff,payload.A,payload.E);
            // Delay just a little bit to let the other unit
            // make the transition to receiver
            delay(20);

            //
            // Ethernet send part
            //
            sent = 0;
            retries = 0;
            do
            {
                Serial.println(F("Sending data..."));
                sent = sendData(pageAdd);
                Serial.print(F("Sent via ethernet: "));
                Serial.println(sent);
                if ( retries++ > MAX_RETRIES )
                    sent=1;
            }
            while ( sent == 0 );
            //
            // END - thernet send part
            //

        }
    }


    //
    // Onewire part
    //
    thisMillis = millis();
    if(thisMillis - lastMillis > delayMillis || startup == 1)
    {
        lastMillis = thisMillis;
        // call sensors.requestTemperatures() to issue a global temperature
        // request to all devices on the bus
        Serial.print(F("Requesting temps"));
        sensors.requestTemperatures(); // Send the command to get temperatures
        Serial.println(F(" ...Done"));
        // float ti string
        dtostrf(sensors.getTempC(spalnaVzduch),1,2,t0buff);
        sprintf(pageAdd,"/nrf.php?onewire=%s&auth=secret12&mode=ow",t0buff);
        //
        // Ethernet send part
        //
        sent = 0;
        retries = 0;
        do
        {
            Serial.println(F("Sending data..."));
            sent = sendData(pageAdd);
            Serial.print(F("Sent via ethernet: "));
            Serial.println(sent);
            if ( retries++ > MAX_RETRIES )
                sent = 1;
        }
        while ( sent == 0 );
        //
        // END - thernet send part
        //
    }
    // set the startup variable to 0 - means we already performed the initial temp reading and saving upon startup
    startup = 0;
}

bool sendData(char *page)
{
    char outBuf[90];
    bool cconn=false;
    unsigned int connectLoop=0;
    do   //try connecting until connected or timeout
    {
        connectLoop++;
        Serial.println("Attempt to connect to server");
        cconn = client.connect(IPAddress(192,168,2,106),80);
        if ( connectLoop > 10000 ) return 0; //timeout if too many not succsssfull attempts, return 0
    }
    while ( cconn == false );

    Serial.print("client connected: ");
    Serial.println(cconn);
    if ( cconn = 1 )
    {
        sprintf(outBuf,"GET %s HTTP/1.1",page);
        client.println(outBuf);
        client.println(F("Host: 192.168.2.106"));
        client.println(F("Connection: close\r\n"));
        connectLoop = 0;
        while (!client.available())
        {
            Serial.println("client not available");
            connectLoop++;
            if ( connectLoop > 10000 ) return 0; //timeout if client was not available for long time, return 0
        }
        connectLoop = 0;
        while (client.read() > 0)
        {
            Serial.println("Reading data");
            connectLoop++;
            if ( connectLoop > 10000 ) return 0; //timeout if reading data took too long, return 0
        }
        Serial.print("Client stopped: ");
        client.stop();
        return 1;
    }
}

Here is also the serial output of the "repeating payload" - there is always Radio read: 1 and "1" is returned by radio.read() - which is strange:

Radio read: 1
Sending data...
Attempt to connect to server
client connected: 1
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Client stopped: Sent via ethernet: 1
Radio read: 1
Sending data...
Attempt to connect to server
client connected: 1
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
client not available
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Reading data
Client stopped: Sent via ethernet: 1

I modified my code, added debug before checking radio available:

void loop(void)
{
    // if there is data ready
    Serial.println(F("Checking radio available"));
    if ( radio.available() )

and removed the debug from sendData function to prevent flooding serial output. It looks my code stucks inside

while (!done) { .. }

Checking radio available
Checking radio available
Checking radio available
Checking radio available
Checking radio available
Checking radio available
Radio is available
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1
Radio read: 1
Sending data...
Sent via ethernet: 1

Strange. Serial output tells "Radio read: 1" and "1" comes from boolean done which is checked in the while cycle. I do not understand why the loop will not finish. I am not advanced programmer (as you already see from my post :D ), but when radio.read() will return true if read was OK, then this true value is stored in boolean done variable and the while cycle should end, should not it?

OMG, I think I found the problem :fearful:

It looks I have to use integer variable done instead of boolean. When I changed it as following:

 int done = 0;
        while (done == 0)
        {
            // Fetch the payload, and see if this was the last one.
            done = radio.read( &payload, sizeof(payload) );

I could not reproduce the problem anymore.

I do not understand it, because radio.read(), according to he class reference, returns bool (takne from the class referece):

bool RF24::read (void * buf, uint8_t len )

Returns: True if the payload was delivered successfully false if not