5

I have been trying for ages to work this problem out and can't figure it out. I have two nrf24 transceivers, one connected to the raspberry pi and the other connected to an Arduino. My code for the Arduino is written using the maniacbug rf24 library and the raspberry pi is using a modified version of that (I don't remember which one but i think it was Stanely-something-or-other). When I ran my code, Before it would give me erroneous feed back and the addresses where something like 0xffffffffffff (i dont remember the count but it was f repeating). The wiring here was done as per: rpi wiring. I then rewired as it was suggested on this page http://www.raspberrypi.org/forums/viewtopic.php?t=17061&p=350040 and it looked like

RPi GPIO9 (Pin 21) to RF Module Pin 7 ( MISO )
RPi GPIO10 (Pin 19) to RF Module Pin 6 ( MOSI )
RPi GPIO 11 (Pin 23) to RF Module Pin 5 ( SCK )
RPi GPIO8 (Pin 24) to RF Module Pin 3 ( CE )
RPi GPIO 25 (Pin 22) to RF Module Pin 4 ( CSN )
RPI 3.3V (Pin 17) to RF Module Pin 2 ( VCC/3.3V )
RPi Gnd (Pin 25) to RF Module Pin 1 (GND)

How ever now, Eveything is zeroed out instead of repeating f. and I am not getting any output (before it gave me really big numbers, the max for unsigned log i believe). I also ran the example code and same things.

This Is my radio data:

SPI device       = /dev/spidev0.0
SPI speed        = 8000000
CE GPIO          = 25
STATUS           = 0x00 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=0 TX_FULL=0
RX_ADDR_P0-1     = 0x0000000000 0x0000000000
RX_ADDR_P2-5     = 0x00 0x00 0x00 0x00
TX_ADDR          = 0x0000000000
RX_PW_P0-6       = 0x00 0x00 0x00 0x00 0x00 0x00
EN_AA            = 0x00
EN_RXADDR        = 0x00
RF_CH            = 0x00
RF_SETUP         = 0x00
CONFIG           = 0x00
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 1MBPS
Model            = nRF24L01
CRC Length       = Disabled
PA Power         = PA_MIN

this was taken from the rpi_hub example

What Am I doing wrong? should I wire it a different way?

Edit: Here is the code. Sorry it took so long, I had everything working on two arduinos so I thought the raspberry pi would be fine. turns out it isnt =/

#include "../costumLibraries/RF24/librf24-rpi/librf24/RF24.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <getopt.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
// Command Line: g++ -Wall -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -L../librf24/  -lrf24 -pthread *.cpp -o remote
using namespace std;
RF24 radio("/dev/spidev0.0",8000000 , 25);  //spi device, speed and CSN,only CSN is NEEDED in RPI
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup(void){
    //Prepare the radio module
    printf("\nPreparing interface\n");
    radio.begin();
    radio.setRetries( 15, 15);
    radio.openWritingPipe(pipes[0]);
    radio.openReadingPipe(1,pipes[1]);
    // radio.startListening();
    radio.printDetails();
    cout<<"good"<<endl;
}


int main( int argc, char ** argv){
        string choice;
        // time_t timer;
        int choiceVal;
        setup();
        while(choice != "quit"){
            cout<<">";
            cin>>choice;
            cout<<endl;
            choiceVal = atoi(choice.c_str());
            if(choice!="quit"){ 
               radio.write(&choiceVal, sizeof(int));
               printf("\n\rRecieved and sent %i",choiceVal);
               radio.startListening();
               unsigned long started_waiting_at = time(NULL);
               bool timeout = false;
               while ( ! radio.available() && ! timeout )
                 if (time(NULL) - started_waiting_at > 200 )
                   timeout = true;

               // Describe the results
               if ( timeout )
               {
                 printf("Failed, response timed out.\n\r");
               }
               bool done = false;
               while (!done){
               // Fetch the payload, and see if this was the last one.
                   done = radio.read( &choiceVal, sizeof(int) );
                   printf("recieved: %i",choiceVal);
                   delay(20);
               }
               radio.stopListening();
            }
        }
}
9
  • Your Pins look correct. I suspect the issue is in your code. It could be on the Raspberry Pi or Arduino side. I would suggest running the sample code that is provided in the examples folder of the source code - github.com/maniacbug/RF24/tree/master/examples/GettingStarted. If my memory serves me correct, the code will work on Arduino and Pi as both there C++.
    – PhillyNJ
    Aug 1, 2014 at 0:08
  • I tested on two arduinos using library examples and it worked. I tried the library example (the rpi_hub one) on the Raspberry pi and That didn't work.
    – rady
    Aug 1, 2014 at 14:37
  • Sounds like you have your culprit. You might want to post both codes for people to review.
    – PhillyNJ
    Aug 1, 2014 at 14:55
  • @PhilVallone The Examples Didn't work on the raspberry pi. The examples worked fine on the arduinos though so the Module is fine.
    – rady
    Aug 1, 2014 at 23:38
  • You need to post the code. Otherwise there is no way someone can help
    – PhillyNJ
    Aug 2, 2014 at 14:41

3 Answers 3

1

In case you have spare boards, try a pair of same board and run library example. Same board is easy to test first.

For information, http://www.airspayce.com/mikem/arduino/RadioHead/ is new library for Arduino (not sure if also available for RPi yet).

1

For starters, you only posted half your code. Since you are trying to communicate between the 2, both codes are helpful. Nevertheless, try the below code. This code will communicate between the Arduino and Raspberry Pi. The Pi is the sender (Transmitter) and the Arduino is the receiver. You might need to adjust the import paths as your code is most likely in a different location.

This code was taken from the examples provided in the github repo and the examples directory of the source code:

Pin out:

Raspberry Pi    RF24    Name
----------------------------
GND             1       GND
3.3V            2       VCC
GPIO 25         3       CE
GPIO 8          4       CSN
GPIO 11         5       SCK
GPIO 10         6       MOSI
GPIO 09         7       MISO
                8       IRQ

Arduino     RF24    Name
------------------------
GND         1       GND
3.3V        2       VCC
9           3       CE
10          4       CSN
13          5       SCK
11          6       MOSI
12          7       MISO
                    IRQ

Arduino:

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

int LedOn = 8;
RF24 radio(9,10);//uno

const uint64_t pipes[2] = { 0xF0F0F0F0E9LL, 0xF0F0F0F0D2LL };

typedef enum { role_ping_out = 1, role_pong_back } role_e;

const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};

role_e role = role_pong_back;

void setup(void)
{

  pinMode(LedOn, OUTPUT);

  Serial.begin(57600);
  printf_begin();
  printf("\n\rRF24/examples/GettingStarted/\n\r");
  printf("ROLE: %s\n\r",role_friendly_name[role]);
  printf("*** PRESS 'T' to begin transmitting to the other node\n\r");

  radio.begin();

  radio.setRetries(15,15);
  radio.openReadingPipe(1,pipes[1]);
  radio.startListening();
  radio.printDetails();
}

void loop(void)
{

  if (role == role_ping_out)
  {   
    radio.stopListening();
    unsigned long time = millis();
    printf("Now sending %lu...",time);
    bool ok = radio.write( &time, sizeof(unsigned long) );

    if (ok)
      printf("ok...");
    else
      printf("failed.\n\r");


    radio.startListening();

    unsigned long started_waiting_at = millis();
    bool timeout = false;
    while ( ! radio.available() && ! timeout )
      if (millis() - started_waiting_at > 200 )
        timeout = true;

    if ( timeout )
    {
      printf("Failed, response timed out.\n\r");
       digitalWrite(LedOn, LOW);
    }
    else
    {

      char* mess;
      radio.read( &mess, sizeof(char*) );     
      printf("Got response %s, message is: %s\n\r",mess);      
    }

    delay(1000);
  }


  unsigned int mess;
  if ( role == role_pong_back )
  {

    if ( radio.available() )
    {
      digitalWrite(LedOn, HIGH);     
      unsigned long got_time;

      bool done = false;
      while (!done)
      {
        done = radio.read( &mess, sizeof(unsigned int) );

        printf("Got payload %lu...", mess);

        delay(30);

      }

      radio.stopListening();

      radio.write( &got_time, sizeof(unsigned long) );
      printf("Sent response.\n\r");

      radio.startListening();
    }else{
       digitalWrite(LedOn, LOW);
    }
  }

  if ( Serial.available() )
  {
    char c = toupper(Serial.read());
    if ( c == 'T' && role == role_pong_back )
    {
      printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");

      role = role_ping_out;
      radio.openWritingPipe(pipes[0]);
      radio.openReadingPipe(1,pipes[1]);

    }
    else if ( c == 'R' && role == role_ping_out )
    {
      printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");

      role = role_pong_back;
      radio.openWritingPipe(pipes[1]);
      radio.openReadingPipe(1,pipes[0]);
      radio.printDetails();
    }
  }
}
// vim:cin:ai:sts=2 sw=2 ft=cpp

Raspberry Pi:

#include <cstdlib>
#include <iostream>
#include "../RF24.h"

RF24 radio("/dev/spidev0.0",8000000 , 25);  //spi device, speed and CSN,only CSN is NEEDED in RPI

const int role_pin = 7;
const uint64_t pipes[2] = { 0xF0F0F0F0E9LL, 0xF0F0F0F0D2LL };

typedef enum { role_ping_out = 1, role_pong_back } role_e;

const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};

role_e role;

void setup(void)
{
    printf("\n\rRF24/examples/pingpair/\n\r");
    printf("ROLE: %s\n\r",role_friendly_name[role]);

    radio.begin();

    radio.setRetries(15,15);

    radio.setChannel(0x4c);
    radio.setPALevel(RF24_PA_MAX);

    if ( role == role_ping_out )
    {
        radio.openWritingPipe(pipes[0]);
        radio.openReadingPipe(1,pipes[1]);
    }
    else
    {
        radio.openWritingPipe(pipes[1]);
        radio.openReadingPipe(1,pipes[0]);
    }

    radio.startListening();
    radio.printDetails();
}

void loop(void)
{
    if (role == role_ping_out)
    {
        radio.stopListening();

        unsigned int  mess = 41;

        unsigned long time = __millis();
        printf("Now sending: %lu ---  ...",mess);
        bool ok = radio.write( &mess, sizeof(unsigned int));

        if (ok)
            printf(" ok...");
        else
            printf("failed.\n\r");

        radio.startListening();

        unsigned long started_waiting_at = __millis();
        bool timeout = false;
        while ( ! radio.available() && ! timeout ) {
            __msleep(30); //add a small delay to let radio.available to check payload
            if (__millis() - started_waiting_at > 200 )
                timeout = true;
        }

        if ( timeout )
        {
            printf("Failed, response timed out.\n\r");
        }
        else
        {
            unsigned long got_time;
            radio.read( &got_time, sizeof(unsigned long) );
            printf("Got response %lu, round-trip delay: %lu\n\r",got_time,__millis()-got_time);
        }

        sleep(1);
    }

    if ( role == role_pong_back )
    {
        if ( radio.available() )
        {
            unsigned long got_time;
            bool done = false;
            while (!done)
            {

                done = radio.read( &got_time, sizeof(unsigned long) );

                printf("Got payload %lu...",got_time);

                delay(20);
            }

            radio.stopListening();

            printf("Sent response.\n\r");
            radio.write( &got_time, sizeof(unsigned long) );

            radio.startListening();
        }
    }
}

int main(int argc, char** argv)
{
    setup();
    while(1)
        loop();

    return 0;
}


// vim:cin:ai:sts=2 sw=2 ft=cpp
13
  • I didn't post the Arduino code because I know for a fact that works. I tested it across two arduinos.
    – rady
    Aug 10, 2014 at 16:42
  • It's important because I believe the pipes have to be the same.
    – PhillyNJ
    Aug 10, 2014 at 17:53
  • The pipes are the same. I use the same library for the arduino and the Pi. I basically copied and pasted from my arduino code and just changed the necessary parts.
    – rady
    Aug 11, 2014 at 13:58
  • Did you try the code I posted ?
    – PhillyNJ
    Aug 11, 2014 at 14:28
  • 1
    As a note, the code I posted works. If you connected the pins as indicated and run the code on both devices, they will communicate. This will help you determine if your radios are still good (I have had one go bad before). If it does not work, then its your radio(s).
    – PhillyNJ
    Aug 11, 2014 at 14:59
1

stumbled across this question while looking for something else, had been working with the nrf24l01 modules and pi's and with the code from https://github.com/TMRh20 worked perfectly for me. Arduino <-> Pi and Pi <-> Pi although i was using it as a tcp/ip link.

Your Answer

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

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