modbus to ESP32/ Modbus library in arduino

Hi All,
I am trying to take the modbus/RS485 output in the ESP32 with the help of ESP32 - modbus converter.
but i'm getting random readings again and again.

Can anyone tell me how get the exact same reading from modbus to ESP32 ?

Also I need support on using the modbus library in Arduino, where I can specify the address & set parity for the modbus RS485 device. The modbus vendor has recommended a baud rate of 19200, which i am able to change.

Request urgent help

Thanks

Post links to the used hardware! Post a wiring diagram! Post your code (don’t forget the code tags, that’s the </> button in the editor)! Post a link to the manual of the counterpart device!

“Serial2.begin(19200, SERIAL_8N1, RX_PIN, TX_PIN);”

Please tell me how to define the Serial2.begin in program

ESP 32
MAX485 Module TTL to RS485 MAX485CSA Converter Board for Arduino
4-20mA to Modbus

#include "ModbusMaster.h"
#define MAX485_RE_NEG  4
#define Slave_ID       1
#define RX_PIN      16
#define TX_PIN      17
ModbusMaster modbus;
void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, HIGH);
}
void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, LOW);
}
void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  digitalWrite(MAX485_RE_NEG, LOW);
  Serial.begin(19200, SERIAL_8N1);
  Serial2.begin(19200, SERIAL_8N1, RX_PIN, TX_PIN);
  modbus.begin(Slave_ID, Serial2);
  modbus.preTransmission(preTransmission);
  modbus.postTransmission(postTransmission);
}
long lastMillis = 0;
void loop() 
{
  long currentMillis = millis();
  if (currentMillis - lastMillis > 1000) 
  {
    uint8_t result = modbus.readInputRegisters(0x01, 2);
    if (getResultMsg(&amp;modbus, result)) 
    {
      Serial.println();
      double res_dbl = modbus.getResponseBuffer(0) / 10;
      String res = "Temperature: " + String(res_dbl) + " C\r\n";
      res_dbl = modbus.getResponseBuffer(1) / 10;
      res += "Humidity: " + String(res_dbl) + " %";
      Serial.println(res);
    }
    lastMillis = currentMillis;
  }
}
bool getResultMsg(ModbusMaster *node, uint8_t result) 
{
  String tmpstr2 = "\r\n";

  switch (result) 
  {
  case node->ku8MBSuccess:
    return true;
    break;
  case node->ku8MBIllegalFunction:
    tmpstr2 += "Illegal Function";
    break;
  case node->ku8MBIllegalDataAddress:
    tmpstr2 += "Illegal Data Address";
    break;
  case node->ku8MBIllegalDataValue:
    tmpstr2 += "Illegal Data Value";
    break;
  case node->ku8MBSlaveDeviceFailure:
    tmpstr2 += "Slave Device Failure";
    break;
  case node->ku8MBInvalidSlaveID:
    tmpstr2 += "Invalid Slave ID";
    break;
  case node->ku8MBInvalidFunction:
    tmpstr2 += "Invalid Function";
    break;
  case node->ku8MBResponseTimedOut:
    tmpstr2 += "Response Timed Out";
    break;
  case node->ku8MBInvalidCRC:
    tmpstr2 += "Invalid CRC";
    break;
  default:
    tmpstr2 += "Unknown error: " + String(result);
    break;
  }
  Serial.println(tmpstr2);
  return false;
}

The direct connection of DE and RE doesn’t do anything without connecting that to a MCU pin. This is for selecting the sending or receiving hardware, you cannot have both active at the same time (with your hardware).

The RS-485 hardware you’ve chosen is a 5V device. You must not connect it directly to your ESP32, you might damage it (or already did damage it).