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


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

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

    树莓派代码

    如下:

    #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 = 0;//receive 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=0x01;
    unsigned long  srchead=0x00000000;
    
    int main(int argc, char** argv){
    
      cout << "RF24/examples/GettingStarted/
    ";
    
      // 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.openReadingPipe(1,pipes);
    /***********************************/
      // This simple sketch opens two pipes for these two nodes to communicate
      // back and forth.
    
      radio.startListening();
    
      cout << "Listening .... 
    ";
    
      int node = atoi(argv[1]);
    
      cout << "Listening Node is : " <<node<<" 
    ";
    
      while(1){
        unsigned long end_time = millis();
          if(radio.available()){
            radio.read(&receData,sizeof(unsigned long));
            //cout<<"receData is: "<<receData<<"
    ";
            unsigned int check = (unsigned int) receData>>24;
            unsigned long data = receData & 0x0000ffff;
            //cout<<"check is "<<check<<"
    ";
    if(check==node && (receData & 0x00ff0000)==srchead){ cout<<"Get Node Data: "<<data<<",Time consume "<<(end_time-start_time)<<"ms "; break; } } cout<<"time out is "<<(end_time-start_time)<<" "; if((end_time-start_time)>=5000){ cout<<"Wait Data from Node "<<node<<" time out "; break; } } return 0; }

    输入指令:sudo ./receive_once 1,参数1表示要查询的节点节点。得到以下结果:

    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 = 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 = 1;
    unsigned long srchead = 0x01000000;//高16位为头标志,前8位为源节点,后8位为目的节点。根据标志不同区分不同发送源,00为中心主节点
    unsigned long deshead = 0x00000000;//高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+deshead;
         Serial.print("Sending:");
         Serial.println(data);
         digitalWrite(13,HIGH);
         bool ok = radio.write(&data,sizeof(unsigned long));
    
         
           role = 0; 
           radio.startListening();
    radio.openReadingPipe(1,pipes); start_time
    = millis(); } if(!role){ digitalWrite(13,LOW); if(radio.available()){ radio.read(&receData,sizeof(unsigned long)); //根据目标节点,判断是否是发给自己的,如果是,执行相关操作 unsigned long check = receData & 0x00ff0000; if(check == srchead){ //接收到来自主机的数据,执行相关操作 Serial.print("Response:"); Serial.println(receData); } role = 1; radio.stopListening();
    radio.openWritingPipe(pipes); }
    else{ unsigned long end_time = millis(); if((end_time-start_time)>=100){ role = 1; radio.stopListening();
    radio.openWritingPipe(pipes); } } } }
    // Loop
  • 相关阅读:
    【转】简单地用nfs实现linux间文件共享
    CentOS 6.1上部署SVN时遇到的问题及解决方法
    ubuntu下部署SVN服务
    LINUX GBK>UTF8文件编码批量转换脚本[转]
    Thinkpad E420 3YC(双显卡)安装64位Ubuntu 11.10笔记
    拦截器详解
    快排的算法
    冒泡排序的算法
    struts2的输入校验
    ssh整合开发
  • 原文地址:https://www.cnblogs.com/wuchaodzxx/p/8440548.html
Copyright © 2020-2023  润新知