3

I can make two arduinos work together (exchange payloads) but not a rPi and an Arduino. I migrated my arduino "base" code to raspberry in order to just "copy" its behavior. The rPi seems to not proceed with the network.available() :

base.cpp

#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <time.h>

using namespace std;

//////////////////////////////////////
// Create the structures for data
//////////////////////////////////////
struct data_received {
  uint16_t node;
  uint16_t temp;
  uint16_t humi;
  uint16_t light;
  uint16_t door;
  uint16_t pir;
  uint32_t hkey;
};

struct data_to_send {
  uint32_t hkey;
}confirm;

unsigned long finish = 0;
unsigned long elapsed = 0;

//////////////////////////////////////
// Setup nRF24L01+ module & Oled
//////////////////////////////////////
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);

RF24Network network(radio);
const uint64_t this_node  = 00; // base

//////////////////////////////////////
// loop
//////////////////////////////////////

int main(int argc, char** argv)
{

  //////////////////////////////////////
  // setup
  //////////////////////////////////////

  // Initialize interfaces
  cout<<"Starting interfaces...";
  radio.begin();
  radio.setRetries(7,7);
  delay(5);
  cout<<"complete]\n";
  network.begin(90, this_node);
  radio.setDataRate(RF24_250KBPS);
  radio.printDetails();

  while(1) {

    network.update();
    data_received payload;

    cout<<"**"<<network.available()<<'\n';

    // listen for incoming clients
    while ( network.available() )
      {
        RF24NetworkHeader header;
        confirm.hkey = 0;

        // read the next available header
        network.peek(header);

        // print NRF24 header data
        cout<<"\n[header info => id: ";
        cout<<header.id<<" type: ";
        cout<<header.type<<" from_node: ";
        cout<<header.from_node<<"]";

        // read data
        network.read( header, &payload, sizeof(payload) );

        // Print data received
        cout<<"[NODE: "<<payload.node<<" ]=>  ";
        cout<<"[temp: "<<payload.temp<<"*C] ";
        cout<<"[humi: "<<((float)payload.humi)<<"%] ";
        cout<<"[light: "<<((float)payload.light)<<"%] ";
        cout<<"[door: "<<payload.door<<"] ";
        cout<<"[pir: "<<payload.pir<<"] ";
        cout<<"[hkey: "<<payload.hkey<<"] ";

        // send back data to confirm integrity
        confirm.hkey = payload.hkey;
        cout<<"...";
        RF24NetworkHeader header2(header.from_node);

        // starts counting just before sending
        elapsed = 0;
        unsigned long start = millis();

        bool sent_back = network.write( header2, &confirm, sizeof(confirm));

        if (sent_back == true){
          finish = millis();
          elapsed = finish - start;
          cout<<elapsed;
          cout<<"ms";
          cout<<" - Confirmed back \n";
        }
        else{
          cout<<"Not confirmed";
        }

      }

      int value = 0;
      float vin = 0.0;
      value = payload.light;
      vin = 3.472 * value / 1024;
      cout<<"VIn: "<<vin<<'\n';

      cout<<"Listening for nodes.."<<'\n';
      delay(1000);
  }
  return 0;
}

a node (arduino)

#include <dht11.h>
#include "RF24Network.h"
#include "RF24.h"
#include <SPI.h>
#include "printf.h"
#include <JeeLib.h>

//////////////////////////////////////
// Create the structures for data
//////////////////////////////////////
struct data_to_send {
  uint16_t node;
  uint16_t temp;
  uint16_t humi;
  uint16_t light;
  uint16_t door;
  uint16_t pir;
  uint32_t hkey;
};

struct data_received {
  uint32_t hkey = 0;
};

//////////////////////////////////////
// Setup nRF24L01+ module
//////////////////////////////////////
RF24 radio(8,7);
RF24Network rf24Net(radio);
const uint64_t this_node =  02; // node A
const uint64_t other_node = 00; // base

//////////////////////////////////////
// Setup the rest of it
//////////////////////////////////////

const int led_A_Pin = 6;
const int optoPin   = A2;

unsigned long currentTime = 0;
unsigned long startTime   = 0;
bool runOnce = true;

ISR(WDT_vect) { Sleepy::watchdogEvent(); } // Setup the watchdog

//////////////////////////////////////
// setup
//////////////////////////////////////
void setup(void) {

  // Initialize interfaces
  printf_begin();
  Serial.begin(9600);
  Serial.print(F("Starting interfaces..."));
  SPI.begin();
  radio.begin();
  Serial.print(F("complete]\n"));
  rf24Net.begin(90, this_node);
  radio.setDataRate(RF24_250KBPS);
  radio.printDetails();
  analogReference(INTERNAL);

  // initialize pins
  pinMode(led_A_Pin, OUTPUT);

}

//////////////////////////////////////
// loop
//////////////////////////////////////
uint32_t txTimer = 0;

void loop(void)
{
    digitalWrite(led_A_Pin, HIGH);
    delay(200);
    digitalWrite(led_A_Pin, LOW);
    delay(200);
    digitalWrite(led_A_Pin, HIGH);

    currentTime = millis();

    if (currentTime - startTime <= 1000) {
      Serial.println(currentTime - startTime);
    }
    else {
       digitalWrite(led_A_Pin, LOW);
    }

  rf24Net.update();

  if (millis() - txTimer > 1000) {
    data_to_send payload;
    payload.node  = 02;
    payload.temp  = 11;
    payload.humi  = 11;

    payload.light = analogRead(A2);
    payload.door  = 0;
    payload.pir   = 0;
    payload.hkey  = random(10000000, 99999999);


    // starts counting
    unsigned long elapsed = 0;
    unsigned long start = millis();
    Serial.println(payload.hkey);

    digitalWrite(led_A_Pin, HIGH);
    delay(100);
    digitalWrite(led_A_Pin, LOW);
    delay(100);
    digitalWrite(led_A_Pin, HIGH);
    delay(100);
    digitalWrite(led_A_Pin, LOW);
    delay(100);

    // sends packet
    RF24NetworkHeader header(other_node);

    Serial.println("Sending data to base every 30 sec.");
    txTimer = millis();
    bool ok = rf24Net.write( header, &payload, sizeof(payload) );
    if (ok) {
      unsigned long finish = millis();
      elapsed = finish - start;
      Serial.println("ok.");
      Serial.print(elapsed);
      Serial.print("ms\n");
      rf24Net.update();
    }
    else {
      Serial.println("failed.");
    }
  }

  while ( rf24Net.available() )
  {
    Serial.println("Received packet");
    RF24NetworkHeader header2;
    data_received confirm;
    rf24Net.read(header2, &confirm, sizeof(confirm));
    Serial.println(confirm.hkey);
    Serial.println("------------------------------");

    Serial.println(F("Going to sleep..."));
    delay(100);
    radio.powerDown();
    Sleepy::loseSomeTime(2000);
    radio.powerUp();
    Serial.println(F("Woke up!"));
  }
}

This is the library I am using: https://github.com/TMRh20/RF24Network

The RPi<=>NRF24 Schematic:

enter image description here

I am executing the program and it fails to receive. Can you please help? Is it a bug or am I doing something wrong ?

NRF Configurations

UPDATE: pingpair_dyn

1
  • Did you ever find a way to receive nRF24 payloads using the Pi? I'm having the same issue. Arduino<-->Arduino works, Pi can send to Arduino, but Pi can't receive from Arduino.
    – Paul Grime
    Commented May 12, 2016 at 15:06

1 Answer 1

1

I had the same problem a few months ago and while I never found a reliable solution I can offer this workaround:

  • get a Arduino Pro Mini 3.3V (the cheap ones start at EUR 2)
  • program it to handle the communication and take I/O via the serial port (I got it to output JSON)
  • because it's 3.3V you can connect it directly to the Raspberry serial port.

If you want to continue to with the Pi you need to match the configuration you are using on the Arduino, e.g. (this was working for me, YMMV):

radio.begin();
radio.setChannel(0x4c);
radio.setRetries(0,15);                 // Smallest time between retries, max no. of retries
radio.setAutoAck(1);                    // Ensure autoACK is enabled
radio.enableDynamicPayloads(); // feature 0x4
radio.enableAckPayload(); // feature 0x2
radio.enableDynamicAck(); // feature 0x1

//radio.setPayloadSize(1);                // Here we are sending 1-byte payloads to test the call-response speed
radio.setCRCLength(RF24_CRC_16);
radio.openWritingPipe(pipes[1]);        // Both radios listen on the same pipes by default, and switch when writing
radio.openReadingPipe(1,pipes[0]);
radio.startListening();                 // Start listening

You can print the config on the Arduino to see what "features" you are using.

1
  • This is how I overcame it. Exactly to be honest. But I don't know whether to accept it as a solution, because it is an "alternative". This way it seems that works for me too fine, but I wish to know how I can fix the initial problem. Thank you.
    – Radolino
    Commented Aug 21, 2015 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.