树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库全双工通信

设计思路

Arduino Leonardo初始化为发送模式,发送完成后,立即切换为接收模式,不停的监听,收到数据后立即切换为发送模式,若超过一定时间还为接收到数据,则切换为发送模式。

树莓派初始化为接收模式,接收到数据后立即切换为发送模式,超过一定时间为接收到数据则切换为发送模式,发送数据后立即切换为接收模式。

代码实现

Arduino Leonardo代码

如下:

#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;//1表示发送模式,0表示接收模式
unsigned long start_time = micros();

//这个是我们即将建立的传输渠道编码
//!!要和另一个模块的一致
const uint64_t pipes = 0xE8E8F0F0E1LL;

//这个变量会保持我们接受到的信息
//变量类型一定要和传过来的一样
//要传输的数据
unsigned long sendData = 1;
unsigned long head = 0x01000000;//高8位为头标志,根据标志不同区分不同发送源,0x00为中心主节点
unsigned long receData;

void setup() {
  pinMode(13,OUTPUT);//指示灯
  Serial.begin(57600);
  printf_begin();
  Serial.println(F("RF24/examples/GettingStarted"));

  radio.begin();

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

}

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

       role = 0;
       radio.startListening();
       start_time = micros();

  }
  if(!role){
     digitalWrite(13,LOW);
     if(radio.available()){
         radio.read(&receData,sizeof(unsigned long));
         Serial.print("Response:");
         Serial.println(receData);
         role = 1;
         radio.stopListening();
     }else{
        unsigned long end_time = micros();
        if((end_time-start_time)>=1000){
          role = 1;
          radio.stopListening();
        }
     }
  }

} // Loop

树莓派代码

如下:

#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
// 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
bool radioNumber = 1;
bool role = 0;//receive mode
unsigned long  start_time=millis();
unsigned long count=0;
/********************************/

// 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(!role){
        if(radio.available()){
            count++;
            radio.read(&receData,sizeof(unsigned long));
            printf("Get Data:size(%d),count(%lu) %lu \n",sizeof(unsigned long),count,receData);
            role = 1;
            radio.stopListening();
        }else{
            unsigned long end_time = millis();
            if((end_time-start_time)>=1000){
                role = 1;
                radio.stopListening();
            }
        }
    }
    if(role){
        unsigned long data = respData + head;
        radio.write(&data,sizeof(unsigned long));
        printf("Send Data:size(%d),%lu \n",sizeof(unsigned long),data);
        role = 0;
        radio.startListening();
        start_time = millis();
    }

  } // forever loop

  return 0;
}

原文地址:https://www.cnblogs.com/wuchaodzxx/p/8440095.html

时间: 2024-08-25 21:56:24

树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库全双工通信的相关文章

树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (六) 树莓派查询子节点温湿度数据

nrl24l01每次只能发送4个字节,前面说到,第一个字节用于源节点,第二个字节用于目的节点.因此只剩下两个字节用于温度和湿度,一个字节只有八位,需要表示温湿度的正负数,因此每个字节的第一位表示正负符号,后七位表示数据,最大能表示+-127. 树莓派代码 如下: #include <cstdlib> #include <iostream> #include <sstream> #include <string> #include <unistd.h&g

树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库发送自定义数据

在我的项目里,树莓派主要作为中心节点,用于接收数据,Arduino作为子节点,用于发送数据,考虑到以后会有很多子节点,但又不至于使得代码过于繁琐,因此所有的传输数据添加一个头部编号用于区分不同节点. nrf24l01支持的数据最大为4个字节,因此使用最高位的一个字节(8位)作为节点编号,剩余三个字节用于传输数据.以下为具体代码: Arduino Leonardo程序 主要用于发送数据给树莓派,同时接收树莓派的响应数据. #include <SPI.h> #include "RF24.

树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (四) 树莓派单子节点查询

考虑到项目的实际需要,树莓派作为主机,应该只在需要的时候查询特定节点发送的数据,因此接收到数据后需要根据头部判断是否是自己需要的数据,如果不是继续接收数据,超过一定时间未查询到特定节点的数据,则退出程序,避免无限等待. 本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点的编号). 树莓派代码 如下: #include <cstdlib> #inc

nRF2401A/nRF24L01/nRF24L01+无线模块最常见问题汇集(转)

俗话说:每个人一生下来什么都会的,都是通过自己努力和探索出来的,NRF系列芯片,刚开始都好奇心加兴趣才来捣鼓它的,刚开始做硬件和软件,没有收发数据弄得整个人头都快炸开了,所以在此和大家分享一下前辈的经验,希望大家会少走弯路. Q:最近在选型的时候发现基本常见的无线模块都是3V供电,而我使用的单片机是5V的,它们之间要怎么连接呢?直接连接会不会损坏模块? 回答: 如果你使用的51单片机的P0口是高阻引脚,可以直接将无线模块的IO脚连接到P0端口,并在对应的连线与3V电源间放置1K~10K左右的偏置

[51单片机] nRF24L01 无线模块 测试 按键-灯-远程控制

哈哈,穷吊死一个,自己做的一个超简单的板还没有电源提供,只得借助我的大开发板啦.其实这2个模块是完全可以分开的,无线嘛,你懂得!进入正题,这个实验的功能就是一个发送模块(大的那个板)连接4个按键,通过按动这4个不同的按键来发送4种不同的命令,来控制接收端(小的板)点亮4个不同的灯. >_<!首先是发送模块: 1 void main() 2 { 3 uchar Tx_Buf1[]={1}; //发送的信息1 4 uchar Rx_Buf[32]; //接收到的数据暂存器,最多32字节数据 5 i

CB1 驱动NRF24L01 无线模块 通信成功

系统:lubuntu 官方  (喜欢cb官方原版)硬件: cb   NRF24L01*2   51开发板一块   杜邦线若干 CB1 驱动NRF24L01 无线模块与STC51单片机通信成功!!! 1.废话不多说,上图上代码: 2.自制51单片机开发板:(24L01在右下角,有内置接口)   3.来张清晰的图,上面手抖了,      LCD1602显示温度,并实时通过无线发给cubieboard 1   4. 下图,第一位是正负温度指示,0x00代表正 ,0xff代表零下  . 后两位分别代表温

[51单片机] nRF24L01 无线模块 串口法命令 通过无线控制另一个的灯

>_<!概述: 这是在上一个的基础上通过按键发送4种不同命令来控制接收端的LED灯亮的改进版(上一个:http://www.cnblogs.com/zjutlitao/p/3840013.html),这里俺把按键发命令给去掉,然后加入一个串口通信的功能,PC通过串口给发送端发送命令,然后发送端通过无线将命令发给接收端来实现控制,这里接收端和上一个例程中的一样,只是在发送端的代码里去除了按键控制,变成了串口控制. >_<!发送端电路: >_<!接收电路图: >_&l

[51单片机] SPI nRF24L01无线 [可以放在2个单片机里实现通信]

main.c 1 #include<reg51.h> 2 #include"2401.h" 3 4 #define uint unsigned int 5 #define uchar unsigned char 6 7 sbit KEY8=P3^7; //发送按键 8 sbit beep=P2^3;//喇叭 9 sbit LED6=P1^6; ////接收到数据后的功能实现灯 10 11 void delay_ms(uint z) //延时函数 12 { 13 uint x

树莓派与Arduino Leonardo R3使用NRF24L01无线模块通信

连接说明 与树莓派的连线 NRF24L01 => 树莓派 GND          =>   GND VCC          =>    3.3V CE           =>   GPIO25 即22管脚 CSN         =>   CE0(GPIO8) 即 24管脚 SCK         =>   SCLK(GPIO11)即23管脚 MOSI       =>   MOSI(GPIO10)即19管脚 MISO       =>   MISO(