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