nrl24l01每次只能发送4个字节,前面说到,第一个字节用于源节点,第二个字节用于目的节点。因此只剩下两个字节用于温度和湿度,一个字节只有八位,需要表示温湿度的正负数,因此每个字节的第一位表示正负符号,后七位表示数据,最大能表示+-127。

树莓派代码

如下:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h> using namespace std;
// RPi generic:
RF24 radio(,,BCM2835_SPI_SPEED_8MHZ); /********** 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 srchead=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"; int node = atoi(argv[]); //cout << "Listening Node is : " <<node<<" \n"; while(){
unsigned long end_time = millis();
if(radio.available()){
radio.read(&receData,sizeof(unsigned long));
//cout<<"receData is: "<<receData<<"\n";
unsigned int check = (unsigned int) receData>>;
unsigned long data = receData & 0x0000ffff;
//cout<<"check is "<<check<<"\n";
if(check==node && (receData & 0x00ff0000)==srchead){
//cout<<"Get Node oriData: "<<receData<<",data:"<<data<<",Time consume "<<(end_time-start_time)<<"ms \n";
int temperature = (data & 0x00007f00)>>;
int humidity = data & 0x0000007f;
if((0x00008000& data)==){
temperature = -temperature;
}
if((0x00000080&data)==){
humidity = -humidity;
}
cout<<temperature<<"-"<<humidity<<"\n";//温度-湿度
break;
}
} //cout<<"time out is "<<(end_time-start_time)<<"\n";
if((end_time-start_time)>=){
cout<<"Wait Data from Node "<<node<<" time out \n";
break;
} }
return ;
}

Arduino Leonardo代码

如下:

#include <SPI.h>
#include "RF24.h"
#include <SPI.h>
#include "RF24.h"
#include <printf.h>
#include <dht.h> dht DHT;
#define DHT22_PIN 7 /****************** 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){
int chk = DHT.read22(DHT22_PIN); //读取数据
if(chk==DHTLIB_OK){
int humidity = (int)(DHT.humidity+0.5);
int temperature = (int)(DHT.temperature+0.5);
Serial.print("data:temperature=");
Serial.print(temperature);
Serial.print(",humidity=");
Serial.println(humidity);
//第三个字节存放温度,第四个字节存放湿度,目前只能表示
unsigned long data = (temperature<<)+(humidity)+(srchead<<)+(deshead<<);
if(temperature>){
data = data+0x00008000;
}
if(humidity>){
data = data+0x00000080;
}
Serial.print("Sending:");
Serial.println(data);
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库 (六) 树莓派查询子节点温湿度数据的更多相关文章

  1. 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据

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

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

    考虑到项目的实际需要,树莓派作为主机,应该只在需要的时候查询特定节点发送的数据,因此接收到数据后需要根据头部判断是否是自己需要的数据,如果不是继续接收数据,超过一定时间未查询到特定节点的数据,则退出程 ...

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

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

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

    在我的项目里,树莓派主要作为中心节点,用于接收数据,Arduino作为子节点,用于发送数据,考虑到以后会有很多子节点,但又不至于使得代码过于繁琐,因此所有的传输数据添加一个头部编号用于区分不同节点. ...

  5. 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (一) 配置与测试

    引脚连接说明 与树莓派的连线 NRF24L01 => 树莓派 GND          =>   GND VCC          =>    3.3V CE           = ...

  6. STC8H开发(五): SPI驱动nRF24L01无线模块

    目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...

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

    俗话说:每个人一生下来什么都会的,都是通过自己努力和探索出来的,NRF系列芯片,刚开始都好奇心加兴趣才来捣鼓它的,刚开始做硬件和软件,没有收发数据弄得整个人头都快炸开了,所以在此和大家分享一下前辈的经 ...

  8. nRF24L01无线模块笔记

    nRF24L01模块 官网链接: https://www.nordicsemi.com/Products/nRF24-series 常见的无线收发模块, 工作在2.4GHz频段, 适合近距离遥控和数据 ...

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

    哈哈,穷吊死一个,自己做的一个超简单的板还没有电源提供,只得借助我的大开发板啦.其实这2个模块是完全可以分开的,无线嘛,你懂得!进入正题,这个实验的功能就是一个发送模块(大的那个板)连接4个按键,通过 ...

随机推荐

  1. exBSGS学习笔记

    exBSGS学习笔记 Tags:数学 题目的话就做下洛谷的模板好了 // luogu-judger-enable-o2 #include<algorithm> #include<io ...

  2. Qt5.4 All Modules

    Qt5.4 All Modules Qt Essentials Qt essentials define the foundation of Qt on all platforms. They are ...

  3. 02-分页器,自定义分页器,解耦函数分页器,分页器class

    1 .批量数据导入 主url from django.contrib import admin from django.urls import path, re_path, include urlpa ...

  4. java spring 等启动项目时的异常 或 程序异常的解决思路

    今天搭建ssm项目的时候,因为pagehelper的一个jar包没有导入idea的web项目下的lib目录中,异常报错找不到pagehelper,这个问题在出异常的时候疯狂crash,让人心情十分不舒 ...

  5. python并发编程之守护进程、互斥锁以及生产者和消费者模型

    一.守护进程 主进程创建守护进程 守护进程其实就是'子进程' 一.守护进程内无法在开启子进程,否则会报错二.进程之间代码是相互独立的,主进程代码运行完毕,守护进程也会随机结束 守护进程简单实例: fr ...

  6. NGUI可展开列表的实现

    本文来自网易云社区 作者:汪毅军 最近使用了NGUI做了下可展开列表,其主要思路如下:首先最外层使用Scroll view以达到滑动效果,然后列表使用UITable进行排列,最后通过点击Item控制I ...

  7. 服务器路由配置--Route

    第1章 命令配置 虚拟服务器 网卡配置信息 虚拟网卡名称 虚拟网卡模式 服务器01 eth1 10.0.0.10/24 nat模式 服务器02 eth2 10.0.0.11/24 nat模式 eth3 ...

  8. 2018年美国大学生数学建模竞赛(MCM/ICM) E题解题思路

    任务一就是让大家去做个基本的评价,是典型的评价类问题,所以应该按照 指标+方法的步骤去做,首先就是寻找国家脆弱性的相关概念,然后选择影响国 家脆弱性的指标,如气候变化,经济发展,政治状况等等,再就是构 ...

  9. 判断浏览器是chrome,Opera,Safari,Mac

    function(){ return { isSafari: (navigator.userAgent.indexOf('Safari')>=0 || navigator.userAgent.i ...

  10. Float浮点数的使用和条件

    在这里简单的说一下,我对浮点数的理解,可能说的比较浅,老师也没有说,只是略微的提了一下,完全是我自己个人的理解. 我觉得float浮点数的用法和int的用法有些雷同,浮点数用于计算小数点单位,我们先可 ...