From: Roman Bazalevsky Date: Sat, 1 Oct 2016 08:48:40 +0000 (+0300) Subject: Сенсор углекислого газа MH-Z14/19 на последовательном интерфейсе. X-Git-Url: https://git.rvb.name/weathermon.git/commitdiff_plain/41892b7f983c33c2ca0281852d0cddcc2c4df2f1 Сенсор углекислого газа MH-Z14/19 на последовательном интерфейсе. --- diff --git a/Weather_Station/Weather_Station.ino b/Weather_Station/Weather_Station.ino index b42f486..3fbdc2e 100644 --- a/Weather_Station/Weather_Station.ino +++ b/Weather_Station/Weather_Station.ino @@ -75,15 +75,43 @@ DHT dht; #define CO2_PIN A1 #define DHT_PIN 4 #define GAS_PIN A0 -#define MHZ14_PIN A2 +// #define MHZ14_PIN A2 #define START_DELAY 20000 -#define DELAY 50000 +#define DELAY 10000 byte HasBaro; #define READ_SAMPLE_INTERVAL 50 #define READ_SAMPLE_TIMES 5 +#include ; + +SoftwareSerial mySerial(8, 9); // 8 - к TX сенсора, 9 - к RX + +byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; +unsigned char response[9]; + +int MHZRead() +{ + mySerial.write(cmd, 9); + memset(response, 0, 9); + mySerial.readBytes(response, 9); + int i; + byte crc = 0; + for (i = 1; i < 8; i++) crc+=response[i]; + crc = 255 - crc; + crc++; + + if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) { + return -1; + } else { + unsigned int responseHigh = (unsigned int) response[2]; + unsigned int responseLow = (unsigned int) response[3]; + unsigned int ppm = (256*responseHigh) + responseLow; + return ppm; + } +} + float MQRead(int mq_pin) { @@ -113,6 +141,7 @@ void setup() Serial.println("START"); Serial1.begin(57600); Serial1.println("START"); + mySerial.begin(9600); // Initialize the sensor (it is important to get calibration values stored on the device). @@ -216,6 +245,8 @@ void loop() } } + delay(DELAY); + delay(dht.getMinimumSamplingPeriod()); DHT_H = dht.getHumidity(); @@ -239,6 +270,8 @@ void loop() Serial1.println(dht.getStatusString()); } + delay(DELAY); + Gas = MQRead(GAS_PIN); Serial.print("SENSOR:TYPE=MQ4,VALUE="); @@ -246,19 +279,29 @@ void loop() Serial1.print("SENSOR:TYPE=MQ4,VALUE="); Serial1.println(Gas); + delay(DELAY); + CO2_raw = MQRead(CO2_PIN); Serial.print("SENSOR:TYPE=MQ135,VALUE="); Serial.println(CO2_raw); Serial1.print("SENSOR:TYPE=MQ135,VALUE="); Serial1.println(CO2_raw); - CO2_MHZ_raw = MQRead(MHZ14_PIN); - MHZ_ppm=2000*(CO2_MHZ_raw*5.0/1023)/2.5; - Serial.print("SENSOR:TYPE=MHZ14,PPM="); - Serial.println(MHZ_ppm); - Serial1.print("SENSOR:TYPE=MHZ14,PPM="); - Serial1.println(MHZ_ppm); - - delay(DELAY); // Pause for 50 seconds. + delay(DELAY); + + MHZ_ppm=MHZRead(); + if (MHZ_ppm<0) { + Serial.print("ERROR:TYPE=MHZ14,MESSAGE=CRC Error"); + Serial.println(MHZ_ppm); + Serial1.print("ERROR:TYPE=MHZ14,MESSAGE=CRC Error"); + Serial1.println(MHZ_ppm); + } else { + Serial.print("SENSOR:TYPE=MHZ14,PPM="); + Serial.println(MHZ_ppm); + Serial1.print("SENSOR:TYPE=MHZ14,PPM="); + Serial1.println(MHZ_ppm); + } + + delay(DELAY); }