Esp32 max485 modbus issue

Hi @emelianov

Thanks for your help so far. The loop is working fine and I am able to read the registers I wanted.
I am trying to integrate the write_reg loop with callback mqtt function in a way that when the callback contains "update", the modbus write register loop is executed. I am not able to write more than one register in the loop. Is this because the callback function is executed in the setup() loop? If i run callback in the main loop then it keeps on writing to the registers indefinitely - not what i wanted. I just want to write in registers when i send update command through mqtt.

Could you recommend any step that i can take to resolve the issue? This is the part of the main program causing issue.

    Serial.println("Modbus write register request received....");  
    if (!mb.slave()){
    mb.writeHreg(1, WR[w], &writereg[w]); 
    w++;
    if (w >= 6)
      w = 0;
    Serial.print("write register..........................:");
    Serial.println(WR[w]);   
    Serial.println(writereg[w]);
  } 
  mb.task(); 
  yield();    
  }   

You can store new values received by MQTT and write them by Modbus in background. Simplest way is permanent writing all the registers independent of they changed or not. More complex way is to create some kind of queue. MQTT will put updated values to the queue and Modbus code will pull values from the queue and sending them to slave.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.