Created
November 8, 2025 03:22
-
-
Save maxpromer/b64c5766411530f85aa5585f6ea201fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| float temp = 0, humi = 0; | |
| void readXY_MD02() { | |
| uint8_t buff[] = { | |
| 0x01, // Devices Address | |
| 0x04, // Function code | |
| 0x00, // Start Address HIGH | |
| 0x01, // Start Address LOW | |
| 0x00, // Quantity HIGH | |
| 0x02, // Quantity LOW | |
| 0x20, // CRC LOW | |
| 0x0B // CRC HIGH | |
| }; | |
| Serial2.write(buff, sizeof(buff)); | |
| Serial2.flush(); // wait MODE_SEND completed | |
| if (Serial2.find("\x01\x04")) { | |
| delay(50); | |
| 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; | |
| } else { | |
| Serial.println("ERROR Timeout"); | |
| return; | |
| } | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| Serial2.begin(9600, SERIAL_8N1, 27, 26); // 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