树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据
本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点的编号)。
设计思路:基于前面提到的两个节点进行双工通信,树莓派不断的向节点发送数据,为了保证数据发送可以到达,持续发送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
树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据的更多相关文章
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (六) 树莓派查询子节点温湿度数据
nrl24l01每次只能发送4个字节,前面说到,第一个字节用于源节点,第二个字节用于目的节点.因此只剩下两个字节用于温度和湿度,一个字节只有八位,需要表示温湿度的正负数,因此每个字节的第一位表示正负符 ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (四) 树莓派单子节点查询
考虑到项目的实际需要,树莓派作为主机,应该只在需要的时候查询特定节点发送的数据,因此接收到数据后需要根据头部判断是否是自己需要的数据,如果不是继续接收数据,超过一定时间未查询到特定节点的数据,则退出程 ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (三) 全双工通信
设计思路 Arduino Leonardo初始化为发送模式,发送完成后,立即切换为接收模式,不停的监听,收到数据后立即切换为发送模式,若超过一定时间还为接收到数据,则切换为发送模式. 树莓派初始化为接 ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (二) 发送自定义数据
在我的项目里,树莓派主要作为中心节点,用于接收数据,Arduino作为子节点,用于发送数据,考虑到以后会有很多子节点,但又不至于使得代码过于繁琐,因此所有的传输数据添加一个头部编号用于区分不同节点. ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (一) 配置与测试
引脚连接说明 与树莓派的连线 NRF24L01 => 树莓派 GND => GND VCC => 3.3V CE = ...
- STC8H开发(五): SPI驱动nRF24L01无线模块
目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...
- nRF2401A/nRF24L01/nRF24L01+无线模块最常见问题汇集(转)
俗话说:每个人一生下来什么都会的,都是通过自己努力和探索出来的,NRF系列芯片,刚开始都好奇心加兴趣才来捣鼓它的,刚开始做硬件和软件,没有收发数据弄得整个人头都快炸开了,所以在此和大家分享一下前辈的经 ...
- nRF24L01无线模块笔记
nRF24L01模块 官网链接: https://www.nordicsemi.com/Products/nRF24-series 常见的无线收发模块, 工作在2.4GHz频段, 适合近距离遥控和数据 ...
- [51单片机] nRF24L01 无线模块 串口法命令 通过无线控制另一个的灯
>_<!概述: 这是在上一个的基础上通过按键发送4种不同命令来控制接收端的LED灯亮的改进版(上一个:http://www.cnblogs.com/zjutlitao/p/3840013. ...
随机推荐
- Python数值运算与赋值的快捷方式
一种比较常见的操作是对一个变量进行一项数学运算并将运算得出的结果返回给这个变量,因此对于这类运算通常有如下的快捷表达方式: a = 2a = a * 3 同样也可写作: a = 2a *= 3 要注意 ...
- Windows10放开Administrator权限
手机上大家都喜欢使ROOT权限,root是超线用户的意思,但是Win10最高权限是Administrator管理员权限,但是系统默认是没有开启这个权限的需要系统安装好以后再次去开启. 方法/步骤 在桌 ...
- C++ STL 学习笔记__(5)list
10.2.6List容器 List简介 ² list是一个双向链表容器,可高效地进行插入删除元素. ² list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.It++(ok) i ...
- 安装 vagrant homestead步骤
vagrant box add laravel/homestead /Users/user/Downloads/virtualbox.box #virtualbox.box存放的位置 cd ~ g ...
- Windows:使用Dos命令管理服务(Services)
Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...
- Docker系列之CentOS7安装Docker(一)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 一.瞎扯淡(只讲有用的) 感兴趣的同学可以上网搜索一下docker具体的介绍.我这边主要介绍偏实战的内容,不喜勿喷,有问题也请指出 ...
- Python 从零搭建 Conf_Web 配置管理平台
作者:Eagle 某船舶行业科技公司,运维工程师,51Reboot学员.通过在51Reboot学习,由运维工程师转至运维开发工程师.完成公司自动化平台的构建,对运维开发有了自己的理解,空闲时间写了这么 ...
- Flutter - 创建横跨所有页面的侧滑菜单
前一篇博客讲到了如何创建侧滑菜单,但是再实际使用过程中,会发现,这个策划菜单只能在首页侧滑出来. 当导航到其他页面后,侧滑就不管用了.这也有点不符合良好的用户体验设计.Google Play就是很好的 ...
- UWP 五星评价(不跳转到龟速商店)
之前写过一篇文章 UWP 五星好评 代码如下 var pfn = Package.Current.Id.FamilyName; await Launcher.LaunchUriAsync(new ...
- [翻译] Python 3.5中async/await的工作机制
Python 3.5中async/await的工作机制 多处翻译出于自己理解,如有疑惑请参考原文 原文链接 身为Python核心开发组的成员,我对于这门语言的各种细节充满好奇.尽管我很清楚自己不可能对 ...