#include "RF24.h"
#include "RF24Network.h"
#include <SPI.h>
RF24 radio(7, 8);
RF24Network network(radio);
#define nodeID 3
#define otherNodeID 2
uint32_t millisTimer = 0;
uint32_t stringTimer = 0;
char dataStr[] = { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" };
char tmpStr[sizeof(dataStr) + 1];
uint8_t strCtr = 1;
uint32_t delayTime = 120;
void setup() {
Serial.begin(115200);
while (!Serial) {
}
mesh.setNodeID(nodeID);
Serial.println(F("Connecting to the mesh..."));
if (!mesh.begin()) {
if (radio.isChipConnected()) {
Serial.println(F("Connecting to the mesh..."));
}
} else {
Serial.println(F("Radio hardware not responding."));
while (1) {
}
}
}
}
unsigned int sizeCtr = 2;
uint32_t errorCount = 0;
uint32_t duplicates = 0;
uint32_t totalData = 0;
void loop() {
mesh.update();
while (network.available()) {
RF24NetworkHeader hdr;
size_t dataSize = network.peek(hdr);
totalData += dataSize;
if (hdr.type == 'S') {
if (dataSize != sizeCtr) {
if (dataSize == sizeCtr + 1) {
duplicates++;
}
sizeCtr = dataSize + 1;
errorCount++;
} else {
sizeCtr++;
if (sizeCtr > sizeof(dataStr)) {
sizeCtr = 2;
}
}
network.read(hdr, &tmpStr, dataSize);
} else if (hdr.type == 'M') {
uint32_t mills;
network.read(hdr, &mills, sizeof(mills));
Serial.print(F("Received "));
Serial.print(mills);
int _ID = 0;
_ID = mesh.getNodeID(hdr.from_node);
if (_ID > 0) {
if (_ID == nodeID) {
Serial.println(F(" from master."));
} else {
Serial.print(F(" from node(ID) "));
Serial.print(_ID);
Serial.println('.');
}
} else {
Serial.println(F("Mesh ID Lookup Failed"));
}
Serial.print(F("Total Data Received: "));
Serial.print(totalData);
Serial.println(" bytes");
Serial.print(F("Detected Errors in data received (Including Duplicates): "));
Serial.println(errorCount);
Serial.print(F("Duplicates: "));
Serial.println(duplicates);
Serial.println(F("-------------------------------------"));
}
}
if (millis() - millisTimer >= 1000) {
millisTimer = millis();
if (!mesh.write(&millisTimer, 'M', sizeof(millisTimer), otherNodeID)) {
Serial.println(F("Send fail"));
if (!mesh.checkConnection()) {
do {
Serial.println(F("Reconnecting to mesh network..."));
} else {
Serial.println(F("Send fail, Test OK"));
}
} else {
Serial.print(F("Send OK: "));
Serial.println(millisTimer);
}
}
if (millis() - stringTimer >= delayTime) {
stringTimer = millis();
memcpy(tmpStr, dataStr, strCtr);
tmpStr[strCtr] = '\0';
if (mesh.write(tmpStr, 'S', strCtr + 1, otherNodeID)) {
strCtr++;
delayTime = 333;
if (strCtr == sizeof(dataStr)) {
strCtr = 1;
}
}
}
}
#define MESH_DEFAULT_ADDRESS