Last active
March 12, 2023 08:40
-
-
Save woodif/150e8a7b1569c1d72fa05a900468eee3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dev by IOXhop.com */ | |
//#define DE_RE_PIN 4 | |
#define MODE_SEND HIGH | |
#define MODE_RECV LOW | |
float temp = 0, humi = 0; | |
void readXY_MD02() { | |
uint8_t buff[] = { | |
0x01, // Devices Address | |
0x03, // Function code | |
0x01, // Start Address HIGH | |
0xF8, // Start Address LOW | |
0x00, // Quantity HIGH | |
0x02, // Quantity LOW | |
0x44, // CRC LOW | |
0x06 // CRC HIGH | |
}; | |
// digitalWrite(DE_RE_PIN, MODE_SEND); | |
Serial2.write(buff, sizeof(buff)); | |
Serial2.flush(); // wait send completed | |
// digitalWrite(DE_RE_PIN, MODE_RECV); | |
if (Serial2.find("\x01\x04")) { | |
uint8_t n = Serial2.read(); | |
if (n != 4) { | |
Serial.println("Error data size"); | |
return; | |
} | |
// temp = ((uint16_t)(Serial2.read() << 8) | Serial2.read()) / 10.0; | |
// humi = ((uint16_t)(Serial2.read() << 8) | Serial2.read()) / 10.0; | |
temp = ((uint16_t)(Serial2.read() << 8) | Serial2.read()) / 10.0; | |
humi = ((uint16_t)(Serial2.read() << 8) | Serial2.read()) / 10.0; | |
} else { | |
Serial.println("ERROR Timeout"); | |
return; | |
} | |
} | |
void setup() { | |
// pinMode(DE_RE_PIN, OUTPUT); | |
// digitalWrite(DE_RE_PIN, MODE_RECV); | |
Serial.begin(115200); | |
Serial2.begin(4800, SERIAL_8N1, 16, 17); // Rx, Tx | |
Serial2.setTimeout(200); | |
} | |
void loop() { | |
readXY_MD02(); | |
Serial.print("Temperature: "); | |
Serial.print(temp); | |
Serial.println(" *C"); | |
Serial.print("Humidity: "); | |
Serial.print(humi); | |
Serial.println(" *C"); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment