RF24Mesh - Automated Networking for nrf24L01 & nrf52x radios v2.0.2
2024 - A user friendly mesh overlay for sensor neworks using RF24Network
Loading...
Searching...
No Matches
RF24Mesh_Example_Node2Node.ino

Example of node to node communication using RF24Mesh

#include "RF24.h"
#include "RF24Network.h"
#include "RF24Mesh.h"
#include <SPI.h>
//#include <printf.h>
/**** Configure the nrf24l01 CE and CS pins ****/
RF24 radio(7, 8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
#define nodeID 1
#define otherNodeID 2
uint32_t millisTimer = 0;
void setup() {
Serial.begin(115200);
while (!Serial) {
// some boards need this because of native USB capability
}
// Set the nodeID
mesh.setNodeID(nodeID);
// Connect to the mesh
Serial.println(F("Connecting to the mesh..."));
if (!mesh.begin()) {
if (radio.isChipConnected()) {
do {
// mesh.renewAddress() will return MESH_DEFAULT_ADDRESS on failure to connect
Serial.println(F("Could not connect to network.\nConnecting to the mesh..."));
} while (mesh.renewAddress() == MESH_DEFAULT_ADDRESS);
} else {
Serial.println(F("Radio hardware not responding."));
while (1) {
// hold in an infinite loop
}
}
}
}
void loop() {
mesh.update();
if (network.available()) {
RF24NetworkHeader header;
uint32_t mills;
network.read(header, &mills, sizeof(mills));
Serial.print(F("Rcv "));
Serial.print(mills);
Serial.print(F(" from nodeID "));
int _ID = mesh.getNodeID(header.from_node);
if (_ID > 0) {
Serial.println(_ID);
} else {
Serial.println(F("Mesh ID Lookup Failed"));
}
}
// Send to the other node every second
if (millis() - millisTimer >= 1000) {
millisTimer = millis();
// Send an 'M' type to other Node containing the current millis()
if (!mesh.write(&millisTimer, 'M', sizeof(millisTimer), otherNodeID)) {
if (!mesh.checkConnection()) {
do {
Serial.println(F("Reconnecting to mesh network..."));
} while (mesh.renewAddress() == MESH_DEFAULT_ADDRESS);
} else {
Serial.println(F("Send fail, Test OK"));
}
}
}
}
#define MESH_DEFAULT_ADDRESS