renaldyks
renaldyks

Reputation: 188

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.

Upvotes: 0

Views: 2171

Answers (4)

RockWest
RockWest

Reputation: 1

I was struggling with this and eventually discovered the manufacturer had placed the sticker on the case backwards and I had it wired wrong. The writing on the PCB was correct.

Upvotes: 0

Exactly true, Some MD02 sensor (SHT20) wordked on 9600 and 8N2. You always check before reading the data

The code:

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

Upvotes: 0

Frees
Frees

Reputation: 61

Although the manufacturer claimed the device worked on 9600 8N1 - my device only worked on 9600 8N2 (or 8X1 + any parity check). In addition, a large timeout for waiting for a response is required (in my case, a stable response was only with a timeout of 1000 ms or more).

Upvotes: 0

AndroidE
AndroidE

Reputation: 133

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!!!

Upvotes: 1

Related Questions