2

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 = "";
  }
3
  • What output are you getting on the terminal (on the base node) for tries 1 .. 9 ? Commented Dec 6, 2016 at 10:37
  • @KennetRunner - on base node's terminal the output is always same i.e. send is OK. Apparently it seems that there's no problem at the base node. The problem is only at the master node, I suppose. But the question is why the desired status would come on the 10th time?
    – somnathbm
    Commented Dec 6, 2016 at 13:01
  • You wrote res1 = 1 and res1 = '1'. What is it? And int or a char. What libraries are you using? Post your complete sketch.
    – user31481
    Commented Nov 15, 2017 at 21:27

1 Answer 1

1

The nRF24L01 "ACK_PAYLOAD" feature allows for two-way data transfer in a single radio transaction. This is more complicated to set up but plenty of tutorials can be found online, on arduino stackexchange and of course another very helpful source is the nRF24L01 data sheet. This should solve your problem by ensuring the master/slave radio messages are sent in the same, rather than independent, radio transactions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.