Created
December 29, 2020 16:07
-
-
Save maxpromer/c02acaf53b8f23983dbb36c1fb2b859b 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 | |
0x04, // Function code | |
0x00, // Start Address HIGH | |
0x01, // Start Address LOW | |
0x00, // Quantity HIGH | |
0x02, // Quantity LOW | |
0x20, // CRC LOW | |
0x0B // 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; | |
} else { | |
Serial.println("ERROR Timeout"); | |
return; | |
} | |
} | |
void setup() { | |
pinMode(DE_RE_PIN, OUTPUT); | |
digitalWrite(DE_RE_PIN, MODE_RECV); | |
Serial.begin(115200); | |
Serial2.begin(9600, 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
i save you have code