#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.h>;
+
+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)
{
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).
}
}
+ delay(DELAY);
+
delay(dht.getMinimumSamplingPeriod());
DHT_H = dht.getHumidity();
Serial1.println(dht.getStatusString());
}
+ delay(DELAY);
+
Gas = MQRead(GAS_PIN);
Serial.print("SENSOR:TYPE=MQ4,VALUE=");
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);
}