RS485 Call back function help in Gammon non-blocking library

I am trying to build home automation system based on serial communication like RS485 protocol. I use Nick Gammon non blocking library to send and receive data as well feedback / call back function. I successfully achieved the communication between different module but response time slightly reduced.

I have a doubt in call back function which I am using in my so called protocol. I think non blocking library already has feedback system which I cannot understand and my self made feedback system is doing double work which slow down the response time.

Library link

Below part use in gammon blocking library ( the link) and I want to achieve the same with non-blocking library.

 // receive response  
  byte buf [10];
  byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
  digitalWrite (LED_PIN, received == 0);  // turn on LED if error    
  // only send once per successful change
  if (received)
    old_level = level;

Currently protocol which I am using like

 byte msg [] = {1, 1, 1};    // send assembled msg from local panel to relay module where Device #, Button #, Button status  

Relay module will receive the data and reply as below

  if (myChannel.update ())
    memcpy (buf, myChannel.getData (), myChannel.getLength ());  // make a copy of the data


    if (buf [0] == 1 && buf [1] == 1 && buf [2] == 1)   //buf[0] = Slave ID , buf[1] = Button ID ,  buf[2] = Slave ID
      LED_state1 = 1;

      byte msg [] = {1, 1, 1, 3};
      myChannel.sendMsg (msg, sizeof (msg));
    else if (buf [0] == 1 && buf [1] == 1 && buf [2] == 0) {
      LED_state1 = 0;
      byte msg [] = {1, 1, 0, 4};
      myChannel.sendMsg (msg, sizeof (msg));

Local panel will update the button indicator once successfully received specific reply as below

 if (myChannel.update ())

    memcpy (buf, myChannel.getData (), myChannel.getLength ());  // make a copy of the data
    if (buf [0] == 1 && buf [1] == 1 && buf [2] == 1 && buf [3] == 3) {    // 3 confirm the reply msg for ON
      LED_state1 = 1;
    if (buf [0] == 1 && buf [1] == 1 && buf [2] == 0 && buf [3] == 4) { //  confirm the reply msg for OFF
      LED_state1 = 0;

Any other suggestion are welcome.
I search another library which modified from gammon library but I am unable to understand due to week programming skill. if some one can help me to understand feedback part of the library.



You should give an overview about what your project is.

Understanding for any user will become much easier if you try to explain as much as you understand it.

You should give an overview of how much you do understand.

best regards Stefan

Its home appliances control over bus communication. X number of relays control with x number of switches. But Switch indicator led only ON when actual relay is ON as following flow process.

Capacitive touch switch press and send the command to relay module.
Relay module receive the command ,process as per instruction and reply to the switch panel.
Switch panel button indicator ON/OFF based on actual feedback from relay module.

I achieved this task but button response time is slowdown which I suspected misunderstanding of call back function in Nick Gammon non-blocking library.So I want to know that above my protocol (#1) is good approach to achieve that or there is some background call back function in library which I didn't get it.

Main, I need support if some one can explain the call back function in non-blocking library to achieve that.

MQTT is designed for transmitting small data-packets over very long distances and due to the long distances at slow speed.

But I don't have any experiences how much time it takes ususally to transmit a command with MQTT like switch ON/OFF including an acknowledge to send back.

How many seconds written as number of seconds is "slowed down" ?

Even for a professional system like KNX it takes 1 to 3 seconds to react on a simple button to switch on a light.

best regards Stefan

Its not connect with MQTT. only one master and one slave in test and later may be add esp webserver. The reason why I say slow is " when I press 2 or more button together command send to the receiver and process accordingly but indicator on reply back didn’t on"

Same think I did without library and put the call back in while loop mean next push button will not work until get feedback. This protocol work even I press more than 2 button together but this setup is blocking the whole loop if one feedback is missed.

Delay in feedback only and in few msec. I think I have to read and update the status of button after few second of press the button.

your english is very hard to understand. If you want more support from me please use google-translate. Write your posting in your native language and let do google-translate the translation.

The grammar won't be brilliant but the translated text will be much easier to understand than direct written english.

post a new version of your post #5.

You and I can freely choose to post or not. If your next posting is again such a broken hard to understand english I stop answering.

best regards Stefan

Thanks for your time.

Sorry for inconvenience, All of these explanation about the system but my question is simple and related to the library as clearly mention in first post.

Is Gammon non-blocking library has call back function and how to use that.

I hope it did not require google translate.

provide library