樹莓派與Arduino Leonardo使用NRF24L01無線模塊通訊之基於RF24庫 (二) 發送自定義數據

在個人項目裏,樹莓派主要做爲中心節點,用於接收數據,Arduino做爲子節點,用於發送數據,考慮到之後會有不少子節點,但又不至於使得代碼過於繁瑣,所以全部的傳輸數據添加一個頭部編號用於區分不一樣節點。html

nrf24l01支持的數據最大爲4個字節,所以使用最高位的一個字節(8位)做爲節點編號,剩餘三個字節用於傳輸數據。如下爲具體代碼:node

Arduino Leonardo程序

主要用於發送數據給樹莓派,同時接收樹莓派的響應數據。ios

#include <SPI.h>
#include "RF24.h"
#include <SPI.h>
#include "RF24.h"
#include <printf.h>
/****************** User Config ***************************/
/***      Set this radio as radio number 0 or 1         ***/
bool radioNumber = 0;

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(9,10);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving
bool role = 1;

//這個是咱們即將創建的傳輸渠道編碼
//!!要和另外一個模塊的一致
const uint64_t pipes = 0xE8E8F0F0E1LL;
 
//這個變量會保持咱們接受到的信息
//變量類型必定要和傳過來的同樣
//要傳輸的數據
unsigned long sendData = 1;
unsigned long head = 0x01000000;//高8位爲頭標誌,根據標誌不一樣區分不一樣發送源,0x00爲中心主節點
unsigned long receData;

void setup() {
  Serial.begin(57600);
  printf_begin();
  Serial.println(F("RF24/examples/GettingStarted"));
  
  radio.begin();

  radio.setPALevel(RF24_PA_MAX);
  radio.openWritingPipe(pipes);
  
}

void loop() {
  unsigned long data = sendData+head;
   Serial.print("Sending:");
   Serial.println(data);
   bool ok = radio.write(&data,sizeof(unsigned long));

   if(ok){
      radio.startListening();
      delay(925); //延時,用於響應返回時間
      if(radio.available()){
          radio.read(&receData,sizeof(unsigned long));//讀取的數據爲1時,表示正常
          Serial.print("Response:");
          Serial.println(receData);
        }
      radio.stopListening();
    }

} // Loop

 結果以下:git

 

  

樹莓派程序

樹莓派主要用於接收數據,同時發出響應。github

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#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

// 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) ***********************/

// 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
bool radioNumber = 1;

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

// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes = 0xE8E8F0F0E1LL;

unsigned long  receData;
unsigned long  respData=0x01;
unsigned long  head=0x00000000;
int main(int argc, char** argv){

  cout << "RF24/examples/GettingStarted/\n";

  // Setup and configure rf radio
  radio.begin();
  // optionally, increase the delay between retries & # of retries
  radio.setRetries(15,15);
  // Dump the configuration of the rf unit for debugging
  radio.printDetails();
  
  radio.openReadingPipe(1,pipes);
/***********************************/
  // This simple sketch opens two pipes for these two nodes to communicate
  // back and forth.

  radio.startListening();

  cout << "Listening .... \n";
  // forever loop
  while (1)
  {
  // Pong back role.  Receive each packet, dump it out, and send it back
  //

  // if there is data ready
  if ( radio.available() )
  {

    // Fetch the payload, and see if this was the last one.
    while(radio.available()){
        radio.read( &receData, sizeof(unsigned long) );
    }
    radio.stopListening();
    unsigned long data = respData+head;            
    radio.write( &data, sizeof(unsigned long) );

    // Now, resume listening so we catch the next packets.
    radio.startListening();

    // Spew it
    printf("Got payload(%d) %lu...\n",sizeof(unsigned long), receData);
                
    delay(925); //Delay after payload responded to, minimize RPi CPU time
                
    }    

  } // forever loop

  return 0;
}

 結果以下:app

上圖中數據「16777217」用八進制表示爲「0x01000001」,第一個字節的0x01表示從節點head=0x01000000發來的數據,數據爲data=0x000001。ide

相關文章
相關標籤/搜索