Optimized RF24Network Layer v2.0.2
2024 - Optimized RF24 Network Layer for NRF24L01 & NRF52x radios
Loading...
Searching...
No Matches
Network_Priority_RX.ino

An example of handling/prioritizing different types of data passing through the RF24Network

#include "printf.h"
#include <RF24.h>
#include <RF24Network.h>
RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board
RF24Network network(radio); // Network uses that radio
const uint16_t this_node = 00; // Address of our node in Octal format
const uint16_t other_node = 01; // Address of the other node in Octal format
uint32_t myVariable = 0;
void setup() {
Serial.begin(115200);
printf_begin(); // needed for RF24* libs' internal printf() calls
while (!Serial) {
// some boards need this because of native USB capability
}
Serial.println(F("RF24Network/examples/Network_Separation_RX/"));
if (!radio.begin()) {
Serial.println(F("Radio hardware not responding!"));
while (1) {
// hold in infinite loop
}
}
radio.setChannel(90);
network.begin(/*node address*/ this_node);
radio.printDetails();
} //setup
uint32_t sendTimer = 0;
/* **** Create a large array for data to be received ****
* MAX_PAYLOAD_SIZE is defined in RF24Network_config.h
* Payload sizes of ~1-2 KBytes or more are practical when radio conditions are good
*/
#define EXTERNAL_DATA_MAX_SIZE MAX_PAYLOAD_SIZE
uint8_t dataBuffer[EXTERNAL_DATA_MAX_SIZE];
uint32_t userDataTimer = 0;
/*
* The main loop behaviour demonstrates the different prioritization of handling data
* External data is handled immediately upon reception, with the network.update() function being
* called very regularly to handle incoming/outgoing radio traffic.
*
* The network.available() function is only called every 5 seconds, to simulate a busy microcontroller,
* so the user payloads will only print out every 5 seconds
*
* The radio has 3, 32-byte FIFO buffers operating independantly of the MCU, and RF24Network will buffer
* up to MAX_PAYLOAD_SIZE (see RF24Network_config.h) of user data.
*/
void loop() {
// Immediate handling of data with header type EXTERNAL_DATA_TYPE
if (network.update() == EXTERNAL_DATA_TYPE) {
uint16_t size = network.frag_ptr->message_size;
memcpy(&dataBuffer, network.frag_ptr->message_buffer, network.frag_ptr->message_size);
// Handle the external data however...
Serial.print(F("External Data RX, size: "));
Serial.println(network.frag_ptr->message_size);
for (uint16_t i = 0; i < network.frag_ptr->message_size; i++) {
Serial.print(dataBuffer[i]);
Serial.print(F(":"));
}
Serial.println();
}
// Use a timer to simulate a busy MCU where normal network data cannot be processed in a timely manner
if (millis() - userDataTimer > 5000) {
userDataTimer = millis();
// Handling of standard RF24Network User Data
while (network.available()) {
RF24NetworkHeader header; // Create an empty header
uint16_t dataSize = network.peek(header); // Peek to get the size of the data
uint32_t someVariable;
if (header.type == 32) { // If a certain header type is recieved
network.read(header, &someVariable, sizeof(someVariable)); // Handle the data a specific way
Serial.print(F("RX User Data:\nHeader Type "));
Serial.print(header.type);
Serial.print(F(" Value "));
Serial.println(someVariable);
} else {
// Clear the user data from the buffer if some other header type is received
network.read(header, &someVariable, 0);
}
}
}
} //loop
Definition RF24Network.h:384
#define EXTERNAL_DATA_TYPE
Definition RF24Network.h:103
Definition RF24Network.h:229
unsigned char type
Definition RF24Network.h:246