本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点的编号)。

设计思路:基于前面提到的两个节点进行双工通信,树莓派不断的向节点发送数据,为了保证数据发送可以到达,持续发送100ms。

树莓派代码

如下:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h> using namespace std; RF24 radio(,,BCM2835_SPI_SPEED_8MHZ); /********** User Config *********/
// Assign a unique identifier for this node, 0 or 1
bool radioNumber = ;
bool role = ;//send 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=;
unsigned long srchead=0x00000000;
unsigned long deshead=0x00010000;
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.openWritingPipe(pipes);
/***********************************/
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth. radio.stopListening(); cout << "Listening .... \n"; int node = atoi(argv[]);
int value = atoi(argv[]);
cout<<"Node is :"<<node<<",value is "<<value<<" .\n";
// forever loop
while ()
{
// Pong back role. Receive each packet, dump it out, and send it back
//
unsigned long data = respData +srchead+deshead;
radio.write(&data,sizeof(unsigned long));
printf("Send Data:size(%d),%lu \n",sizeof(unsigned long),data);
role = ;
//radio.startListening();
unsigned long end_time = millis();
if((end_time-start_time)>=){
break;
} } // forever loop return ;
}

输入指令:sudo ./send_once 1 14,1表示节点号,14表示输入给节点的值。结果如下:

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 = millis(); //这个是我们即将建立的传输渠道编码
//!!要和另一个模块的一致
const uint64_t pipes = 0xE8E8F0F0E1LL; //这个变量会保持我们接受到的信息
//变量类型一定要和传过来的一样
//要传输的数据
unsigned long sendData = ;
unsigned long srchead = 0x01;//高16位为头标志,前8位为源节点,后8位为目的节点。根据标志不同区分不同发送源,00为中心主节点
unsigned long deshead = 0x00;//高16位为头标志,前8位为源节点,后8位为目的节点。根据标志不同区分不同发送源,00为中心主节点
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+(srchead<<)+(deshead<<);
Serial.print("Sending:");
Serial.println(sendData);
digitalWrite(,HIGH);
bool ok = radio.write(&data,sizeof(unsigned long)); role = ;
radio.openReadingPipe(,pipes);
radio.startListening();
start_time = millis(); }
if(!role){
digitalWrite(,LOW);
if(radio.available()){
radio.read(&receData,sizeof(unsigned long)); //根据目标节点,判断是否是发给自己的,如果是,执行相关操作
unsigned long check = (receData & 0x00ff0000)>>; if(check == srchead){
//接收到来自主机的数据,执行相关操作
Serial.print("Response:");
Serial.println(receData&0x0000ffff);
Serial.println("=======================");
sendData++;
}
role = ;
radio.stopListening();
}else{
unsigned long end_time = millis();
if((end_time-start_time)>=){
role = ;
radio.stopListening();
radio.openWritingPipe(pipes);
}
}
} } // Loop

最新文章

  1. HTML5系列目录
  2. 使用mutt+msmtp在Linux命令行界面下发邮件(续)
  3. xcode中得一个坑
  4. JavaScript 学习笔记: 扩充类型的功能
  5. QT QObject::connect函数的学习
  6. 【转】无废话WCF系列教程
  7. 7款开源Java反编译工具
  8. angular1项目打包app及logo和启动图片的设置
  9. 【Git之旅】2.Git对象
  10. Django 事务操作
  11. future项目上报
  12. .net core安装及初体验
  13. vue: 代码小记
  14. mysql 命令行 备份 恢复数据
  15. Python3学习笔记20-获取对象信息
  16. Spark内部流程图
  17. Centos 7 部署Kubernetes(K8S)集群
  18. Win7如何解决telnet不是内部或外部命令的方案!
  19. 帝国cms目录结构
  20. NUC970设备驱动

热门文章

  1. ARM Linux驱动篇 学习温度传感器ds18b20的驱动编写过程
  2. 20145207 2016-2017《Java程序设计》课程总结
  3. RMAN 与control文件和spfile文件的备份
  4. 【转载】malloc内存分配与free内存释放的原理
  5. wpf 中Listbox获取选中的值
  6. spring学习笔记 星球日two - 注解方式配置bean
  7. 一键将 Python2 代码自动转化为 Python3
  8. Struts 2 访问Servlet API的方式
  9. C# string 的一点属性、方法什么的
  10. MySQL数据库查询某个库下有几张数据表