Skip to content

Commit e6cc7d0

Browse files
committed
Add support for uart communication with mh-z14
1 parent e3403c1 commit e6cc7d0

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

co2lan.ino

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#include <ESP8266WiFi.h>
22
#include <EEPROM.h>
33

4-
#define D2 4
54
#define DC_GAIN (8.5) //define the DC gain of amplifier
5+
#define D3 0
6+
#define D4 2
7+
#define D7 13
8+
#define D8 15
9+
#define D5 14
10+
#define D6 12
611

712
//These two values differ from sensor to sensor. user should derermine this value.
813
//#define ZERO_POINT_VOLTAGE (3.09 / DC_GAIN) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
@@ -30,13 +35,26 @@ public:
3035

3136
UnitData systemData;
3237

33-
bool USE_INFRA = true;
38+
bool USE_INFRA = false;
39+
bool INFRA_WITH_CORRECTION = true;
3440
unsigned long LOOP_DELAY = 30000;
3541

42+
bool USE_UART = true;
43+
44+
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
45+
char response[9];
46+
47+
#include <SoftwareSerial.h>
48+
49+
const int RX_PIN = D5;
50+
const int TX_PIN = D6;
51+
const int BAUD_RATE = 9600;
52+
53+
SoftwareSerial sensorUart(RX_PIN, TX_PIN);
54+
3655
void setup()
3756
{
3857
Serial.begin(115200);
39-
pinMode(D2, INPUT_PULLUP);
4058

4159
Serial.print("Chip id ");
4260
Serial.println(ESP.getChipId());
@@ -65,6 +83,10 @@ void setup()
6583
EEPROM.put(0, systemData);
6684
EEPROM.end();
6785
//*/
86+
87+
if (USE_UART) {
88+
sensorUart.begin(BAUD_RATE);
89+
}
6890
}
6991

7092
void loop()
@@ -78,14 +100,28 @@ void loop()
78100
float voltsRaw = (volts1 + volts2 + volts3) / 3.0;
79101
float volts = voltsRaw;
80102
Serial.print(volts);
81-
Serial.print("V (");
82-
Serial.print(volts1);
103+
Serial.print("V (C");
104+
Serial.print(volts2);
83105
Serial.print("V) ");
84106

85107
float ppm = -1;
86-
if (USE_INFRA) {
108+
if (USE_UART) {
109+
sensorUart.write(cmd, 9);
110+
sensorUart.readBytes(response, 9);
111+
112+
if (0xff == response[0] && 0x86 == response[1]) {
113+
int responseHigh = (int) response[2];
114+
int responseLow = (int) response[3];
115+
ppm = (256 * responseHigh) + responseLow;
116+
} else {
117+
ppm = 100;
118+
}
119+
} else if (USE_INFRA) {
87120
ppm = volts * 1000;
88-
ppm = ppm * (1 + ppm / 1000);
121+
122+
if (INFRA_WITH_CORRECTION) {
123+
ppm = ppm * (1 + ppm / 1250);
124+
}
89125
} else {
90126
volts = volts / DC_GAIN;
91127
Serial.print(volts, 3);
@@ -104,7 +140,6 @@ void loop()
104140
Serial.print("Time point: ");
105141
Serial.print(millis() / 60000.0);
106142
Serial.print("m ");
107-
//Serial.print(digitalRead(D2));
108143
Serial.println();
109144

110145
unsigned long now = millis();

0 commit comments

Comments
 (0)