Better approach to synchronize 3 arduino over HC-12 radio module

i was looking for a way to synchronize 3 arduino with HC-12 radio module so i came across this way :
i have a main arduino that collect the sensors data from the other 2 arduino

1)the main arduino first send a byte contain the address of the arduino that should send it is sensors data

2)both of the two arduino recieve that message and compare it with its address. then the one with matching address send a string containing sensors data finished with '\n'

3)the main arduino recieve data and read it until '\n' character.

4)when a '\n' char is recieved main arduino send a byte contain the address of the other arduino that should send it is sensors data

5)both of the two arduino recieve that message and compare it with its address. then the one with matching address send a string containing sensors data finished with '\n'

6)the main arduino recieve data and read it until '\n' character.

finally repeat the process during the loop().

i translate my approach to arduino program.
this is for the main arduino

#include <SoftwareSerial.h>

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
}

void loop() {
  String node1="";
  String node2="";
  byte empty,c;
  
  //emptying the hc12 buffer
  while(HC12.available()){
    HC12.read();
  }
  
  HC12.write((byte)1);//send a byte contain the address of the first arduino that we want it to return its sensors data
  do{
    if(HC12.available()){
      c=HC12.read();
      if(c!='\n'){node1+=(char)c;}
    }
  }while(c!='\n');//i think this do{...} while(...) is too mutch to explain in comment but with a little patient you can understand

  Serial.println(node1);//here we get our node1 variable containing result from the arduino with address 1
  
  c=0;
  
  HC12.write(2);//send the address of the second arduino
  do{
    if(HC12.available()){
      c=HC12.read();
      if(c!='\n'){node2+=(char)c;}
    }
  }while(c!='\n');//i think this is do{...} while(...) is too mutch to explain in comment but with a little patient you can understand
  
  Serial.println(node2);;//here we get our node1 variable containing result from the arduino with address 1
  
  //delay(2000);//i dont use delay() intentionlly but i plan to use it in the future
}

for the other arduino

#include <SoftwareSerial.h>

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
}

void loop() {
  
  byte addr;
  if(HC12.available()){
    addr = HC12.read();
    if(addr==1){
      HC12.write("temperature:20 humidity:40 co2:3 \n");//i just return a string to the main arduino for testing purpose
    }
  }
  //delay(10);
}

and for the last arduino the same, just replace if(addr==1) by if(addr==2)

so i tested the code and it gives me a good and proper result(the result i want and expected it) on the serial and i am not using delay() and synchronisation is good. so my question is how to improve my code or approach? is there a better approach?what are the changes required to make the approach respectively the code better?

I suggest two changes.
First is to add a 1 byte sequence number to each sensor message and increment by 1 for each new message. Roll over from 255 to 0 is expected and not an error. This allows you to confirm there are no duplicates nor missing data.
Second is to have a time-out waiting for each remote to send it's data. That way one dead secondary is not stopping the whole operation. You can be notified of the problem and can fix the wayward Arduino and restart it without effecting the good running Arduino.
Paul

1 Like

thank you so much, i appreciate your suggestions, i made your changes. so after those changes, can i assume my approach is good and sufficient (not necessary perfect), at least for a low number of arduino (3 to 6 maximum) ? i mean what i should take care of, what should i consider and take into account when i implement this solution as the final one? thank you in advance

Sufficient until an unforseen problem occurs!
Good luck.
Paul

1 Like

thank you so much for your precious help.now i can move forward to another task.

"i have a bad habit of not stop asking until i am sure 90% and a bad habit of double checking and that make me annoying. sorry for that"-oussema ramadan.