Arduino下nrf24l01实现多对一通讯

在应用nrf24l01的过程中,发现关于多对一的代码较少,也尝试过自己通过改变收发地址来实现多对一,但工作效率低数据刷新较慢,无法实现一些连续信号的实时采集,如:加速度信号。而在arduino平台下有现成的RF24network库,可以实现多对一功能的实现。

参考资料

RF24network库基本资料

Arduino 平台下添加库

工具>管理库,搜索rf24,将作者为TMRh20的相关库均下载,尤其是RF24和RF24Network
在这里插入图片描述
在这里插入图片描述

程序

下面放上简单的三发一收代码
接收端

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

RF24 radio(7, 6);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network
const uint16_t this_node = 00;   // Address of our node in Octal format ( 04,031, etc)
const uint16_t node01 = 01;      // Address of the other node in Octal format
const uint16_t node02 = 02;
const uint16_t node03 = 03;


void setup() {
  Serial.begin(115200);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node); //(channel, node address)
  radio.setDataRate(RF24_2MBPS);
}
//short data;
void loop() {
  network.update();
  //===== Receiving =====//
  while ( network.available() ) {     // Is there any incoming data?
    RF24NetworkHeader header;
    //char text[32] = "";
    unsigned long incomingData;
    //network.read(header, &text, sizeof(text)); // Read the incoming data
    network.read(header, &incomingData, sizeof(incomingData)); // Read the incoming data
    //memcpy(data, text, sizeof(data));
//    long randomNumber="";
//    network.read(header, &randomNumber, sizeof(randomNumber));
    if (header.from_node == 01) {    // If data comes from Node 02
      Serial.print(incomingData); 
      Serial.print(","); 
    }
    if (header.from_node == 02) {    // If data comes from Node 012
     Serial.print(incomingData); 
      Serial.print(",");
    }
    if (header.from_node == 03) {    // If data comes from Node 02
      Serial.println(incomingData); 
      //Serial.print(","); 
    }
  }
}

发送端01

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7, 6);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network

const uint16_t this_node = 01;   // Address of our node in Octal format ( 04,031, etc)
const uint16_t master00 = 00;    // Address of the other node in Octal format

const unsigned long interval = 10;  //ms  // How often to send data to the other unit

unsigned long last_sent;            // When did we last send?
void setup() {
  Serial.begin(115200);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node);  //(channel, node address)
  radio.setDataRate(RF24_2MBPS);
}
//const char text[] = "hello";
unsigned long randomNumber;
void loop() {
  network.update();
  //===== Sending =====//
  unsigned long now = millis();
  if (now - last_sent >= interval) {   // If it's time to send a data, send it!
    last_sent = now;
    Serial.print("Sending...");
    RF24NetworkHeader header(master00);   // (Address where the data is going)
    randomNumber = random(100,150); 
    bool ok = network.write(header, &randomNumber, sizeof(randomNumber)); 
   // bool ok = network.write(header, &text, sizeof(text)); // Send the data
    if (ok)
//      Serial.println("ok.")
      Serial.println(randomNumber);
    else
      Serial.println("failed.");
  }
}

发送端02

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7, 6);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network

const uint16_t this_node = 02;   // Address of our node in Octal format ( 04,031, etc)
const uint16_t master00 = 00;    // Address of the other node in Octal format

const unsigned long interval = 10;  //ms  // How often to send data to the other unit

unsigned long last_sent;            // When did we last send?
void setup() {
  Serial.begin(115200);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node);  //(channel, node address)
  radio.setDataRate(RF24_2MBPS);
}
//const char text[] = "abcd";
unsigned long randomNumber;
void loop() {
  network.update();
  //===== Sending =====//
  unsigned long now = millis();
  if (now - last_sent >= interval) {   // If it's time to send a data, send it!
    last_sent = now;
    Serial.print("Sending...");
    RF24NetworkHeader header(master00);   // (Address where the data is going)
    randomNumber = random(0,50); 
    bool ok = network.write(header, &randomNumber, sizeof(randomNumber)); 
   // bool ok = network.write(header, &text, sizeof(text)); // Send the data
    if (ok)
//      Serial.println("ok.");
      Serial.println(randomNumber);
    else
      Serial.println("failed.");
  }
}

发送端03

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7, 6);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network

const uint16_t this_node = 03;   // Address of our node in Octal format ( 04,031, etc)
const uint16_t master00 = 00;    // Address of the other node in Octal format

const unsigned long interval = 10;  //ms  // How often to send data to the other unit

unsigned long last_sent;            // When did we last send?
void setup() {
  Serial.begin(115200);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node);  //(channel, node address)
  radio.setDataRate(RF24_2MBPS);
}
//const char text[] = "abcd";
unsigned long randomNumber;
void loop() {
  network.update();
  //===== Sending =====//
  unsigned long now = millis();
  if (now - last_sent >= interval) {   // If it's time to send a data, send it!
    last_sent = now;
    Serial.print("Sending...");
    RF24NetworkHeader header(master00);   // (Address where the data is going)
    randomNumber = random(200,300); 
    bool ok = network.write(header, &randomNumber, sizeof(randomNumber)); 
   // bool ok = network.write(header, &text, sizeof(text)); // Send the data
    if (ok)
//      Serial.println("ok.");
      Serial.println(randomNumber);
    else
      Serial.println("failed.");
  }
}

最后可以实现三个子节点01,02,03分别发送不同范围的随机数到主节点00的功能。

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值