How to set modbusrtu for multiple slaves?

Hello
So far, I have established communication with only one sensor by using esp8266 and the following module and rs485 connection. The rs485 to ttl conversion module and my code is as follows.

https://www.walmart.com/ip/Stable-UART-serial-port-to-RS485-converter-function-module-RS485-to-TTL-module/432105513

#include <SoftwareSerial.h>

const byte ph[] = {0x02, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x38}; // Soil PH Sensor
byte ph_values[11];

#define RE 5
#define DE 4
#define PUB_DELAY 50

void setup() {
    mod.begin(9600, SWSERIAL_8N1, 14, 12, false, 256);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  // put your setup code here, to run once:

}

void loop() {

   while (true){
    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(ph, sizeof(ph)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(ph_values, 7);
     // Serial.print("Multi PH Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(ph_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (ph_values[0] == 0x02 && ph_values[1] == 0x03 && ph_values[2] == 0x02){
      float ph_reg3and4 = ((ph_values[3] << 8) + ph_values[4]);
      ph_reg3and4 = ph_reg3and4/100;
      pubPH = ph_reg3and4;
      if (ph_reg3and4 > -1 && ph_reg3and4 < 15){
        Serial.print("PH : "+(String)ph_reg3and4+"");
        Serial.println();
       delay(100);
       int ph;
        ph = (int) (ph_reg3and4 * 10L);
        if (phcomp != ph){
        phcomp = ph;
        Serial.println("Published");
        }
        break;
      }
    }else{
      Serial.print("PH cant measure");
    break;
    }
  }

I want to communicate with several slaves through esp8266. I know I have to use the ModbusRTU library. But I don't know how to write the code. Thank you for your guidance.

The library has examples that shows how to use it.

The posted code does not compile because it isn't complete (p.e. many variable declarations are missing).

Very bad design to have a while(true) loop over the complete loop() routine.

Sorry. This was a part of my code. The original code is long, so I shortened it to make the serial part clear. This is also the serial code that can be compiled. I would like to add the modbusrtu section to it so that the ph registry address can be sent to several different sensors

#include <SoftwareSerial.h>

const byte ph[] = {0x02, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x38}; // Soil PH Sensor
byte ph_values[11];

#define RE 5
#define DE 4
#define PUB_DELAY 50
SoftwareSerial mod;

float phcomp ,phoscomp , potcomp , eccomp , moistcomp , tempcomp , nicomp , orpcomp , ph2comp , temp2comp , rescomp;
float pubPH , pubPHOS , pubPOT , pubNI , pubMOIST , pubTEMP , pubEC ,pubRES;

void setup() {
    mod.begin(9600, SWSERIAL_8N1, 14, 12, false, 256);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  // put your setup code here, to run once:

}

void loop() {

    digitalWrite(DE, HIGH);
    digitalWrite(RE, HIGH);
    delay(100);
    if (mod.write(ph, sizeof(ph)) == 8)
    {
      mod.flush();
      digitalWrite(DE, LOW);
      digitalWrite(RE, LOW);
      mod.readBytes(ph_values, 7);
     // Serial.print("Multi PH Values : ");
      for (byte i = 0; i < 7; i++)
      {
      // Serial.print(ph_values[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
    if (ph_values[0] == 0x02 && ph_values[1] == 0x03 && ph_values[2] == 0x02){
      float ph_reg3and4 = ((ph_values[3] << 8) + ph_values[4]);
      ph_reg3and4 = ph_reg3and4/100;
      pubPH = ph_reg3and4;
      if (ph_reg3and4 > -1 && ph_reg3and4 < 15){
        Serial.print("PH : "+(String)ph_reg3and4+"");
        Serial.println();
       delay(100);
       int ph;
        ph = (int) (ph_reg3and4 * 10L);
        if (phcomp != ph){
        phcomp = ph;
        Serial.println("Published");
        }
      }
    }
}

And which part of the ModbusRtu.h examples didn't you understand?

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