Arduino Example Sketch
- Note
- This sketch only functions on -Arduino Due-
This example sketch shows how to manually configure a node via RF24Mesh as a master node, which will receive all data from sensor nodes.
The nodes can change physical or logical position in the network, and reconnect through different routing nodes as required. The master node manages the address assignments for the individual nodes in a manner similar to DHCP.
#include "RF24Network.h"
#include "RF24.h"
#include <SPI.h>
RF24 radio(7, 8);
RF24Network network(radio);
uint32_t displayTimer = 0;
void setup() {
Serial.begin(115200);
while (!Serial) {
}
mesh.setNodeID(0);
Serial.println(mesh.getNodeID());
radio.begin();
radio.setPALevel(RF24_PA_MIN, 0);
if (!mesh.begin()) {
Serial.println(F("Radio hardware not responding."));
while (1) {
}
}
}
void loop() {
mesh.update();
mesh.DHCP();
if (network.available()) {
RF24NetworkHeader header;
network.peek(header);
uint32_t dat = 0;
switch (header.type) {
case 'M':
network.read(header, &dat, sizeof(dat));
Serial.println(dat);
break;
default:
network.read(header, 0, 0);
Serial.println(header.type);
break;
}
}
if (millis() - displayTimer > 5000) {
displayTimer = millis();
Serial.println(" ");
Serial.println(F("********Assigned Addresses********"));
for (int i = 0; i < mesh.addrListTop; i++) {
Serial.print("NodeID: ");
Serial.print(mesh.addrList[i].nodeID);
Serial.print(" RF24Network Address: 0");
Serial.println(mesh.addrList[i].address, OCT);
}
Serial.println(F("**********************************"));
}
}