I'm trying to build a mesh network using 4 nRF24L01 modules directly connected with Arduino Uno where 1 module is master node and the rest are base nodes.
The workflow is exactly as follows
I'm sending a command in the Arduino IDE serial monitor. The master node receives this command and send it to other base nodes. There's no problem receiving the command on the base node side (from master node) as well as sending the command on the master node side (to base nodes). Upon receiving the command from the master node, base nodes successfully able to read the command and send the response. No problem at all.
But when the master node receives the response status from the base node(s), there's the problem. Master node is not receiving the data immediately. I have to send the command from the Arduino serial monitor repeatedly and on the 10th time, the master node is able to read the response status from the base node(s). Exactly on the 10th times every time.
Here's the source.
master node
// global variables
int dat2;
String temp1 = "";
boolean actualDataSent = false;
// get input from arduino serial monitor
while (Serial.available() > 0) {
// my command
String temp1 = Serial.readString();
}
// write block
if (actualDataSent == false && temp1 != "") {
// send this data to the base node one by one char
for (int j = 0; j < mesh.addrListTop; j++)
{
for (int k = 0; k < temp1.length(); k++) {
char ch = (char)temp1.charAt(k);
Serial.print("NodeID: ");
Serial.print(mesh.addrList[j].nodeID);
Serial.print(" RF24Network Address: 0");
Serial.println(mesh.addrList[j].address, OCT);
RF24NetworkHeader header(mesh.addrList[j].address, OCT);
Serial.println( network.write(header, &ch, sizeof(ch)) == 1 ? F("Send OK") : F("Send Fail"));
}
actualDataSent = true;
}
temp1 = "";
}
// read block
else if (network.available() && actualDataSent == true) {
RF24NetworkHeader header;
network.peek(header);
switch (header.type) {
case 'M':
network.read(header, &dat2, sizeof(dat2));
Serial.print("Got: ");
Serial.print(dat2);
Serial.print(" from RF24Network address 0");
Serial.println(header.from_node, OCT);
actualDataSent = false;
break;
default: network.read(header, 0, 0); Serial.print("Not M case"); Serial.println(header.type); actualDataSent = false; break;
}
}
base node:
//global variables
String mypayload = "";
int res1;
// receive command from master
while (network.available()) {
RF24NetworkHeader header;
network.read(header, &payload, sizeof(payload));
mypayload.concat(payload);
}
if (mypayload != "")
{
Serial.print("Received data: ");
Serial.println(mypayload);
if ((mypayload == "100%"))
{
res1 = 1;
Serial.println("Turn ON");
if (!mesh.write(&res1, 'M', sizeof(res1))) {
if ( ! mesh.checkConnection() ) {
Serial.println("Renewing Address");
mesh.renewAddress();
} else {
Serial.println("Send fail, Test OK");
}
} else {
Serial.print("Send OK: "); Serial.println(res1);
}
}
else if (mypayload == "0%")
{
res1 = '1';
Serial.println("Turn OFF");
if (!mesh.write(&res1, 'M', sizeof(res1))) {
if ( ! mesh.checkConnection() ) {
Serial.println("Renewing Address");
mesh.renewAddress();
} else {
Serial.println("Send fail, Test OK");
}
} else {
Serial.print("Send OK: "); Serial.println(res1);
}
}
mypayload = "";
}