Arduino Example Sketch
This example sketch shows how to manually configure a node via RF24Mesh, and send data to the master node. The nodes will refresh their network address as soon as a single write fails. This allows the nodes to change position in relation to each other and the master node.
#include "RF24.h"
#include "RF24Network.h"
#include <SPI.h>
RF24 radio(7, 8);
RF24Network network(radio);
#define nodeID 1
uint32_t displayTimer = 0;
struct payload_t {
unsigned long ms;
unsigned long counter;
};
void setup() {
Serial.begin(115200);
while (!Serial) {
}
mesh.setNodeID(nodeID);
radio.begin();
radio.setPALevel(RF24_PA_MIN, 0);
Serial.println(F("Connecting to the mesh..."));
if (!mesh.begin()) {
if (radio.isChipConnected()) {
do {
Serial.println(F("Could not connect to network.\nConnecting to the mesh..."));
} else {
Serial.println(F("Radio hardware not responding."));
while (1) {
}
}
}
}
void loop() {
mesh.update();
if (millis() - displayTimer >= 1000) {
displayTimer = millis();
if (!mesh.write(&displayTimer, 'M', sizeof(displayTimer))) {
if (!mesh.checkConnection()) {
Serial.println("Renewing Address");
mesh.begin();
}
} else {
Serial.println("Send fail, Test OK");
}
} else {
Serial.print("Send OK: ");
Serial.println(displayTimer);
}
}
while (network.available()) {
RF24NetworkHeader header;
payload_t payload;
network.read(header, &payload, sizeof(payload));
Serial.print("Received packet #");
Serial.print(payload.counter);
Serial.print(" at ");
Serial.println(payload.ms);
}
}
#define MESH_DEFAULT_ADDRESS