树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据
2018-02-11 10:07
573 查看
本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点的编号)。
设计思路:基于前面提到的两个节点进行双工通信,树莓派不断的向节点发送数据,为了保证数据发送可以到达,持续发送100ms。
树莓派代码
如下:
#include <cstdlib> #include <iostream> #include <sstream> #include <string> #include <unistd.h> #include <RF24/RF24.h> using namespace std; RF24 radio(22,0,BCM2835_SPI_SPEED_8MHZ); /********** User Config *********/ // Assign a unique identifier for this node, 0 or 1 bool radioNumber = 1; bool role = 1;//send 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=27; 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(15,15); // 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[1]); int value = atoi(argv[2]); cout<<"Node is :"<<node<<",value is "<<value<<" .\n"; // forever loop while (1) { // 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 = 0; //radio.startListening(); unsigned long end_time = millis(); if((end_time-start_time)>=1000){ break; } } // forever loop return 0; }
输入指令: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 = 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 = millis(); //这个是我们即将建立的传输渠道编码 //!!要和另一个模块的一致 const uint64_t pipes = 0xE8E8F0F0E1LL; //这个变量会保持我们接受到的信息 //变量类型一定要和传过来的一样 //要传输的数据 unsigned long sendData = 15; unsigned long srchead = 0x01;//高16位为头标志,前8位为源节点,后8位为目的节点。根据标志不同区分不同发送源,00为中心主节点 unsigned long deshead = 0x00;//高16位为头标志,前8位为源节点,后8位为目的节点。根据标志不同区分不同发送源,00为中心主节点 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+(srchead<<24)+(deshead<<16); Serial.print("Sending:"); Serial.println(sendData); digitalWrite(13,HIGH); bool ok = radio.write(&data,sizeof(unsigned long)); role = 0; radio.openReadingPipe(1,pipes); radio.startListening(); start_time = millis(); } if(!role){ digitalWrite(13,LOW); if(radio.available()){ radio.read(&receData,sizeof(unsigned long)); //根据目标节点,判断是否是发给自己的,如果是,执行相关操作 unsigned long check = (receData & 0x00ff0000)>>16; if(check == srchead){ //接收到来自主机的数据,执行相关操作 Serial.print("Response:"); Serial.println(receData&0x0000ffff); Serial.println("======================="); sendData++; } role = 1; radio.stopListening(); }else{ unsigned long end_time = millis(); if((end_time-start_time)>=100){ role = 1; radio.stopListening(); radio.openWritingPipe(pipes); } } } } // Loop
相关文章推荐
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (六) 树莓派查询子节点温湿度数据
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (二) 发送自定义数据
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (四) 树莓派单子节点查询
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (三) 全双工通信
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (一) 配置与测试
- 使用net模块实现基于TCP的数据通信
- mahout demo——本质上是基于Hadoop的分步式算法实现,比如多节点的数据合并,数据排序,网路通信的效率,节点宕机重算,数据分步式存储
- 如何使用Arduino和SIM900A GPRS / GSM模块将数据发送到Web服务器
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- wifi模块简单使用教程,如何将数据发送到电脑(esp8266 arduino库的使用)
- 使用dgram模块实现基于UDP的数据通信
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- 微信h5支付页面与web服务器之间的通信,使用jquery ajax向服务器发送数据——微信h5支付开发5
- 基于UDP协议的网络编程(使用DatagramSocket发送接收数据)
- Socket通信中服务器端发送数据使用PrintStream与OutputStream有什么区别?
- android端和pc端使用usb进行socket通信,其中android是服务器端,pc是客户端。如何实现安卓端输入的数据通过按钮发送到pc端?
- Android中基于HTTP的通信技术(2)使用Http的Post方式读取网络数据
- 基于STM32CT117E竞赛板(STM32f103RB) 使用USART串口发送数据
- Java基础知识强化之网络编程笔记17:Android网络通信之 使用Http的Post方式读取网络数据(基于HTTP通信技术)
- 使用AIDL实现IPC通信之——实现服务端主动发送数据给客户端