Search code examples
esp32modbusarduino-esp32

How to read register MD02 using ModBus ESP32?


I have an MD02 sensor (SHT20). In the storefront it says that this sensor is part of the MD02 series and not the XY-MD02.

MD02

MD02

But the store description says it can be configured according to the XY-MD02 register. After I tried, the register couldn't be used on the modbus poll. I used the register datasheet on the web http://www.sah.rs/media/sah/techdocs/xy-md02-manual.pdf

ModbusPoll

I also tried to read Modbus using HW0519 and ESP32, but the result is still the same. The register does not issue any output.

My Code:

#define RXD2 16
#define TXD2 17

byte ByteArray[250];
int ByteData[20];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);

  byte msg[] = {0x01,0x04,0x00,0x01,0x00,0x01,0x60,0x0A};

  int i;
  int len=8;

  Serial.println("Sending Data...");
  for(i=0 ; i < len ; i++){
    Serial2.write(msg[i]);
    Serial.print("[");
    Serial.print(i);
    Serial.print("]");
    Serial.print("=");
    Serial.print(String(msg[i],HEX));
  }

  len = 0;
  Serial.println();
  Serial.println();

  int a = 0;

  while(Serial2.available()){
    ByteArray[a] = Serial2.read();
    a++;
  }

  int b = 0;
  String Register;
  Serial.println("Receiving Data...");
  for(b=0;b<a;b++){
    Serial.print("[");
    Serial.print(b);
    Serial.print("]");
    Serial.print("=");

    Register = String(ByteArray[b],HEX);
    Serial.print(Register);
    Serial.print(" ");
  }

  Serial.println();
  Serial.println();
}

I've made sure the wiring diagram is correct. How to fix it? I'm very confused, because there are no relevant solutions on the internet.


Solution

  • i have same problem. But, i solved this.

    The solution

    1. Change your modbus poll with odd/even parity. I swear that the description is wrong.

    enter image description here

    That the result if changing your parity

    enter image description here

    Temp/Hum have 100 Resolution, just divide by 100.

    1. Change your code using SERIAL_8E1 or SERIAL_8O1

    The code:

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      Serial2.begin(9600, SERIAL_8E1, RXD2, TXD2);
    }
    

    Have a nice day!!!