1

I want to send string from Raspberry Pi to Arduino. I can send it from Arduino to Raspberry Pi. I've 2 Nrf24l01+ modules. And followed steps at here. But i couldnt do it. So raspberry pi can transmit but can't recieve data. How can it possible? I check my cables many times. I control the code. How can i solve it? Here is codes for Raspberry Pi

#include <cstdlib>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <RF24/RF24.h>  

using namespace std;  
//  
// Hardware configuration  
// Configure the appropriate pins for your connections  

/****************** Raspberry Pi ***********************/  

// Radio CE Pin, CSN Pin, SPI Speed  
// See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.  

// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz  
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);  

// NEW: Setup for RPi B+  
//RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);  

// Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz  
//RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);  

// RPi generic:  
RF24 radio(22,0);  

/*** RPi Alternate ***/  
//Note: Specify SPI BUS 0 or 1 instead of CS pin number.  
// See http://tmrh20.github.io/RF24/RPi.html for more information on usage  

//RPi Alternate, with MRAA  
//RF24 radio(15,0);  

//RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and  set 'this->device = "/dev/spidev0.0";;' or as listed in /dev  
//RF24 radio(22,0);  


/****************** Linux (BBB,x86,etc) ***********************/  

// See http://tmrh20.github.io/RF24/pages.html for more information on usage  
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA  
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV  

// Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )  
//RF24 radio(115,0);  

//BBB Alternate, with mraa  
// CE pin = (Header P9, Pin 13) = 59 = 13 + 46  
//Note: Specify SPI BUS 0 or 1 instead of CS pin number.  
//RF24 radio(59,0);  

/********** User Config *********/  
// Assign a unique identifier for this node, 0 or 1  
// 0 Rx / 1 Tx  
bool radioNumber = 0;  
unsigned long timeoutPeriod = 3000;     // Set a user-defined timeout period. With auto-retransmit set to (15,15) retransmission will take up to 60ms and as little as 7.5ms with it set to (1,15).  

/********************************/  

// Radio pipe addresses for the 2 nodes to communicate.  
// const uint8_t pipes[][6] = {"1Node","2Node"};  
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };   // Radio pipe addresses for the 2 nodes to communicate.  
char data[32] = {"_A message from RPi w/ NRF24L+!"};            //Data buffer  

void showData(void)  
{  
      printf("Data: ");  
      for(int i=0; i<32; i++){  
         if(isprint(data[i]))  
           printf("%c", data[i]);  
         else  
           printf(".");  
      }  
      printf("\n\r");  
}  

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

  const int role_rx=0, role_tx=1;  
  int role=role_rx;  
/********* Role chooser ***********/  

  printf("\n ************ Role Setup ***********\n");  
  string input = "";  
  char myChar = {0};  

  cout << "Choose a role: Enter 0 for Rx, 1 for Tx (CTRL+C to exit) \n>";  
  getline(cin,input);  

  if(input.length() == 1) {  
    myChar = input[0];  
    if(myChar == '0'){  
        cout << "Role: Rx " << endl << endl;  
    }else{  cout << "Role: Tx " << endl << endl;  
        role = role_tx;  
    }  
  }  
  switch(role) {  
      case role_rx :  
        radioNumber=0;  
        break;  

      case role_tx :  
        radioNumber=1;  
        break;  
  }  

/***********************************/  
  // Setup and configure rf radio  
  radio.begin();  

  // optionally, increase the delay between retries & # of retries  
  radio.setRetries(15,15);  
  // Set the channel  
  radio.setChannel(1);  
  // Set the data rate  
  //radio.setDataRate(RF24_2MBPS);  
  radio.setDataRate(RF24_250KBPS);  
  //radio.setPALevel(RF24_PA_MAX);  
  radio.setPALevel(RF24_PA_MIN);  

    if ( !radioNumber )    {  
        radio.openWritingPipe(pipes[0]);  
        radio.openReadingPipe(1,pipes[1]);  
        memset(&data,'\0',sizeof(data));  
        radio.startListening();  
    } else {  
        radio.openWritingPipe(pipes[1]);  
        radio.openReadingPipe(1,pipes[0]);  
        radio.stopListening();  
    }  
    // Dump the configuration of the rf unit for debugging  
    radio.printDetails();  
    printf("Start loop:\n");  
    // forever loop  
    while (1)  
    {  
        if (radioNumber) {  
            if (radio.writeBlocking(&data,sizeof(data),timeoutPeriod)) {  
                printf(".");  
            }  
            else {  
                printf("?");  
            }  
            fflush(stdout);  
            //printf("\n");  
        }  
        else {  
        //  
        // Receive each packet, dump it  
        //  
            if(radio.available()){  
                // Read any available payloads for analysis  
                radio.read(&data,32);  
                // Dump the printable data of the payload  
                showData();  
                fflush(stdout);  
            }  
        }  
        delay(5);  
    } // forever loop  

  return 0;  
}  

Arduino Sketch

*/  
#include <SPI.h>  
#include "nRF24L01.h"  
#include "RF24.h"  
#include "printf.h"  
/*************  USER Configuration *****************************/  
RF24 radio(7,8);                        // Set up nRF24L01 radio on SPI bus plus pins 7 & 8  
unsigned long timeoutPeriod = 3000;     // Set a user-defined timeout period. With auto-retransmit set to (15,15) retransmission will take up to 60ms and as little as 7.5ms with it set to (1,15).  
                                        // With a timeout period of 1000, the radio will retry each payload for up to 1 second before giving up on the transmission and starting over  
/***************************************************************/  
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };   // Radio pipe addresses for the 2 nodes to communicate.  
byte data[32] = {"_This is a message via NRF24L+!"};                           //Data buffer  
volatile unsigned long counter;  
unsigned long rxTimer,startTime, stopTime, payloads = 0;  
//bool TX=1,RX=0,role=0, transferInProgress = 0;  
bool transferInProgress = 0;  
unsigned int TX=1,RX=0,role=1,lastrole=0,SKIP=2,RXPRINT=3,RXDUMP=4;  
unsigned int offset=0;  
void setup(void) {  
  Serial.begin(57600);  
  printf_begin();  
  radio.begin();                           // Setup and configure rf radio  
  radio.setChannel(1);                     // Set the channel  
  radio.setPALevel(RF24_PA_HIGH);  
  radio.setDataRate(RF24_250KBPS);  
  //radio.setDataRate(RF24_2MBPS);  
  radio.setAutoAck(1);                     // Ensure autoACK is enabled  
  radio.setRetries(2,15);                  // Optionally, increase the delay between retries. Want the number of auto-retries as high as possible (15)  
  radio.setCRCLength(RF24_CRC_16);         // Set CRC length to 16-bit to ensure quality of data  
  if (role == RX)  
  {  
    radio.openWritingPipe(pipes[0]);         // Open the default reading and writing pipe  
    radio.openReadingPipe(1,pipes[1]);  

    radio.startListening();                 // Start listening  
  }  
  else{  
    radio.openWritingPipe(pipes[1]);         // Open the default reading and writing pipe  
    radio.openReadingPipe(1,pipes[0]);  
    radio.stopListening();  
  }  
  radio.printDetails();                   // Dump the configuration of the rf unit for debugging  

  printf("\n\rRF24/examples/Transfer Rates/\n\r");  
  printf("*** PRESS 'T' to begin transmitting to the other node\n\r");  

  /* 
  randomSeed(analogRead(0));              //Seed for random number generation 
  for(int i=0; i<32; i++){ 
     data[i] = random(255);               //Load the buffer with random data 
  } 
  */  
  radio.powerUp();                        //Power up the radio  

}  
void showData(void)  
{  
      printf("Data: ");  
      for(int i=0; i<32; i++){  
         if(isprint(data[i]))  
           printf("%c", data[i]);  
         else  
           printf(".");  
      }  
      printf("\n\r");  
}  
void loop(void){  
  if(role == TX){  
    delay(500);                                              // Pause for a couple seconds between transfers  
    printf("Initiating Extended Timeout Data Transfer\n\r");  
    unsigned long cycles = 1000;                              // Change this to a higher or lower number. This is the number of payloads that will be sent.  

    unsigned long transferCMD[] = {'H','S',cycles };          // Indicate to the other radio that we are starting, and provide the number of payloads that will be sent  
    radio.writeFast(&transferCMD,12);                         // Send the transfer command  
    if(radio.txStandBy(timeoutPeriod)){                       // If transfer initiation was successful, do the following  

        startTime = millis();                                 // For calculating transfer rate  
        boolean timedOut = 0;                                 // Boolean for keeping track of failures  

        for(int i=0; i<cycles; i++){                          // Loop through a number of cycles  
          data[0] = i;                                        // Change the first byte of the payload for identification  
          if(!radio.writeBlocking(&data,32,timeoutPeriod)){   // If retries are failing and the user defined timeout is exceeded  
              timedOut = 1;                                   // Indicate failure  
              counter = cycles;                               // Set the fail count to maximum  
              break;                                          // Break out of the for loop  
          }  
        }  


        stopTime = millis();                                  // Capture the time of completion or failure  

       //This should be called to wait for completion and put the radio in standby mode after transmission, returns 0 if data still in FIFO (timed out), 1 if success  
       if(timedOut){ radio.txStandBy(); }                     //Partially blocking standby, blocks until success or max retries. FIFO flushed if auto timeout reached  
       else{ radio.txStandBy(timeoutPeriod);     }            //Standby, block until FIFO empty (sent) or user specified timeout reached. FIFO flushed if user timeout reached.  

   }else{                                        
       Serial.println("Communication not established");       //If unsuccessful initiating transfer, exit and retry later  
       counter = cycles+1;  
   }  
   if (counter <= cycles )  
   {  
     float rate = cycles * 32 / (stopTime - startTime);         //Display results:  

     Serial.print("Transfer complete at "); Serial.print(rate); printf(" KB/s \n\r");  
     Serial.print(counter);  
     Serial.print(" of ");  
     Serial.print(cycles); Serial.println(" Packets Failed to Send");  
     //if (counter == 0)  
     showData();  
   }  
   counter = 0;  
   Serial.print("------------------------------------------------------------\r\n\r\n");  
}  

if(role == RX){  

  if(!transferInProgress){                       // If a bulk data transfer has not been started  
     if(radio.available()){  
        //Serial.print("Rx\r\n");  
        //Serial.print(".");  
        radio.read(&data,32);                    //Read any available payloads for analysis  
        if(data[0] == 'H' && data[4] == 'S'){    // If a bulk data transfer command has been received  
          payloads = data[8];                    // Read the first two bytes of the unsigned long. Need to read the 3rd and 4th if sending more than 65535 payloads  
          payloads |= data[9] << 8;              // This is the number of payloads that will be sent  
          counter = 0;                           // Reset the payload counter to 0  
          transferInProgress = 1;                // Indicate it has started  
          startTime = rxTimer = millis();        // Capture the start time to measure transfer rate and calculate timeouts  
        }  
     }  
  }else{  
     if(radio.available()){                     // If in bulk transfer mode, and a payload is available  
       //Serial.print("Rx\r\n");  
       radio.read(&data,32);                    // Read the payload  
       rxTimer = millis();                      // Reset the timeout timer  
       counter++;                               // Keep a count of received payloads  
     }else  
     if(millis() - rxTimer > timeoutPeriod){    // If no data available, check the timeout period  
       Serial.println("Transfer Failed");       // If per-payload timeout exceeeded, end the transfer  
       transferInProgress = 0;  
       //Serial.print("!\r\n");  
     }else  
     if(counter >= payloads){                   // If the specified number of payloads is reached, transfer is completed  
      //Serial.print("!\r\n");  
      startTime = millis() - startTime;         // Calculate the total time spent during transfer  
      float numBytes = counter*32;              // Calculate the number of bytes transferred  
      Serial.print("Rate: ");                   // Print the transfer rate and number of payloads  
      Serial.print(numBytes/startTime);  
      Serial.println(" KB/s");  
      printf("Payload Count: %d (%c)\n\r", counter, (char* )data[1]);  
      showData();  
      transferInProgress = 0;                   // End the transfer as complete  
    }  
  }  
}  
if(role == RXDUMP){  
     if(radio.available()){  
        radio.read(&data,32);                    //Read any available payloads for analysis  
        showData();  
     }  
}  
if(role == RXPRINT){  
  if(!transferInProgress){                       // If a bulk data transfer has not been started  
     if(radio.available()){  
        //Serial.print("Rx\r\n");  
        Serial.print(".");  
        radio.read(&data,32);                    //Read any available payloads for analysis  
        if(data[0] == 'H' && data[4] == 'S'){    // If a bulk data transfer command has been received  
          payloads = data[8];                    // Read the first two bytes of the unsigned long. Need to read the 3rd and 4th if sending more than 65535 payloads  
          payloads |= data[9] << 8;              // This is the number of payloads that will be sent  
          counter = 0;                           // Reset the payload counter to 0  
          transferInProgress = 1;                // Indicate it has started  
          startTime = rxTimer = millis();        // Capture the start time to measure transfer rate and calculate timeouts  
        }  
     }  
  }else{  
     if(radio.available()){                     // If in bulk transfer mode, and a payload is available  
       Serial.print(".");  
       radio.read(&data,32);                    // Read the payload  
       rxTimer = millis();                      // Reset the timeout timer  
       counter++;                               // Keep a count of received payloads  
     }else  
     if(millis() - rxTimer > timeoutPeriod){    // If no data available, check the timeout period  
       Serial.println("Transfer Failed");       // If per-payload timeout exceeeded, end the transfer  
       transferInProgress = 0;  
       Serial.print("!\r\n");  
     }else  
     if(counter >= payloads){                   // If the specified number of payloads is reached, transfer is completed  
      Serial.print("!\r\n");  
      startTime = millis() - startTime;         // Calculate the total time spent during transfer  
      float numBytes = counter*32;              // Calculate the number of bytes transferred  
      Serial.print("Rate: ");                   // Print the transfer rate and number of payloads  
      Serial.print(numBytes/startTime);  
      Serial.println(" KB/s");  
      printf("Payload Count: %d (%c)\n\r", counter, (char* )data[1]);  
      showData();  
      transferInProgress = 0;                   // End the transfer as complete  
      delay(500);  
    }  
  }  
}  
if(role == SKIP){  
}  

  //  
  // Change roles  
  //  
  if ( Serial.available() )  
  {  
    char c = toupper(Serial.read());  
    printf("*** SERIAL : %c %d\n\r", c, c);  
    if ( c == 'C' && role == SKIP)  
    {  
      role = lastrole;  
      printf("*** CONTINUING PREVIOUS ROLE\n\r");  
    }  
    else if ( c == 'D' && (role != RXDUMP ))  
    {  
      radio.openWritingPipe(pipes[0]);  
      radio.openReadingPipe(1,pipes[1]);  
      radio.startListening();  
      printf("*** CHANGING TO RECEIVE DUMP ROLE -- PRESS 'T' TO SWITCH BACK\n\r");  
      role = RXDUMP;                // Become the primary receiver (pong back)  
    }  
    else if ( c == 'T' && (role != TX ))  
    {  
      printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");  
      radio.openWritingPipe(pipes[1]);  
      radio.openReadingPipe(1,pipes[0]);  
      radio.stopListening();  
      role = TX;                  // Become the primary transmitter (ping out)  
    }  
    else if ( c == 'P' && (role != RXPRINT ) )  
    {  
      radio.openWritingPipe(pipes[0]);  
      radio.openReadingPipe(1,pipes[1]);  
      radio.startListening();  
      printf("*** CHANGING TO RECEIVE PRINT ROLE -- PRESS 'T' TO SWITCH BACK\n\r");  
      role = RXPRINT;                // Become the primary receiver (pong back)  
    }  
    else if ( c == 'R' && (role == RX) )  
    {  
      radio.openWritingPipe(pipes[0]);  
      radio.openReadingPipe(1,pipes[1]);  
      radio.startListening();  
      printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");  
      role = RX;                // Become the primary receiver (pong back)  
    }  
    else if ( c == 'S' && role != SKIP)  
    {  
      lastrole = role;  
      role = SKIP;  
      showData();  
      printf("*** WAITING -- PRESS 'C' TO CONTINUE\n\r");  
    }  
    else if ( c == 'M' ){  
      offset = 1;  
      while ( c != 13 )  
      {  

        if ( Serial.available() )  
        {  
          c = Serial.read();  
          if ( c != 13 ){  
          offset++;  
          if (offset>=32)  
            offset = 1;  
          data[offset] = c;  
          printf("*** SERIAL : %c data[%d]=(%c)\n\r", c, offset, data[offset]);  
          }  
        }  
      }  
    }  
  }  
} 

When i run code on my computer via ssh, the output is..:

enter image description here

I thought it can be about my compiling, because of this i compiled the same code many times. But i'm still thinking this..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.