设计思路

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 = ; /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(,);
/**********************************************************/ byte addresses[][] = {"1Node","2Node"}; // Used to control whether this node is sending or receiving
bool role = ;//1表示发送模式,0表示接收模式
unsigned long start_time = micros(); //这个是我们即将建立的传输渠道编码
//!!要和另一个模块的一致
const uint64_t pipes = 0xE8E8F0F0E1LL; //这个变量会保持我们接受到的信息
//变量类型一定要和传过来的一样
//要传输的数据
unsigned long sendData = ;
unsigned long head = 0x01000000;//高8位为头标志,根据标志不同区分不同发送源,0x00为中心主节点
unsigned long receData; void setup() {
pinMode(,OUTPUT);//指示灯
Serial.begin();
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(,HIGH);
bool ok = radio.write(&data,sizeof(unsigned long)); role = ;
radio.openReadingPipe(1,pipes);
radio.startListening();
start_time = micros(); }
if(!role){
digitalWrite(,LOW);
if(radio.available()){
radio.read(&receData,sizeof(unsigned long));
Serial.print("Response:");
Serial.println(receData);
role = ;
radio.stopListening();
radio.openWritingPipe(pipes);
}else{
unsigned long end_time = micros();
if((end_time-start_time)>=){
role = ;
radio.stopListening();
radio.openWritingPipe(pipes);
}
}
} } // 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(,); /*** 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 = ;
bool role = ;//receive mode
unsigned long start_time=millis();
unsigned long count=;
/********************************/ // 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(,);
// Dump the configuration of the rf unit for debugging
radio.printDetails(); radio.openReadingPipe(,pipes);
/***********************************/
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth. radio.startListening(); cout << "Listening .... \n";
// forever loop
while ()
{
// 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 = ;
radio.stopListening();
radio.openWritingPipe(pipes);
}else{
unsigned long end_time = millis();
if((end_time-start_time)>=){
role = ;
radio.stopListening();
radio.openWritingPipe(pipes);
}
}
}
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 = ;
radio.startListening();
radio.openReadingPipe(1,pipes);
start_time = millis();
} } // forever loop return ;
}

最新文章

  1. 浅谈学习掌握linux系统的优势
  2. Wordpress制作sidebar.php
  3. scp命令获取远程文件
  4. Each child in an array or iterator should have a unique &quot;key&quot; prop. Check the render method of `CreditCategoryModal`
  5. 20160331javaweb之JSP include 指令&amp;&amp;九大隐式对象
  6. (转)Mac OS X中配置Apache
  7. asp.net缓存(三)
  8. 体验SubSonic
  9. tsung: an open-source multi-protocol distributed load testing tool
  10. 理解 ES6 语法中 yield 关键字的返回值
  11. 【Netty】EventLoop和线程模型
  12. Nginx的try_files指令和命名location使用实例
  13. PS图片去色
  14. cmake让add_subdirectory()的所有target生成到同一目录
  15. js中json的添加和指定位置的删除
  16. Javascript高级编程学习笔记(33)—— 客户端检测(2)怪癖检测
  17. VBA汇总同目录下的所有工作簿数据到另一个工作簿,并进行统计
  18. 手把手教你搭建WEB服务器和FTP服务器
  19. 【vim】删除指定标记前的内容 dt[标记]
  20. Jquery实现form表单提交后局部刷新页面的多种方法

热门文章

  1. python学习之路(1)
  2. 20155327 实验三 敏捷开发与XP实践
  3. jQuery学习-事件绑定
  4. 【TJOI2017】DNA
  5. GNU构建系统和Autotool
  6. 【Python Learning第一篇】Linux命令学习及Vim命令的使用
  7. ASP.NET MVC - PageData的应用
  8. elk6.3 centos集群搭建 head插件安装
  9. 解决网速慢时maven仓库访问慢
  10. Received non-all-whitespace CHARACTERS or CDATA event in nextTag(). ,无法整齐打印验证错误。 解析XML文档出现的问题