forked from guillier/htu21d_mh-z19_oled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMHZ19.cpp
145 lines (118 loc) · 3.91 KB
/
MHZ19.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* The MIT License (MIT)
* Copyright (c) 2015-2017 François GUILLIER <dev @ guillier . org>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "MHZ19.h"
MHZ19::MHZ19()
{
//Set initial values for private vars
}
//Begin
/*******************************************************************************************/
//Start Soft Serial communication
boolean MHZ19::begin(int rx, int tx)
{
co2Serial = new SoftwareSerial(rx, tx);
co2Serial->begin(9600);
ppmTotal = 0;
ppmCount = 0;
byte cmd[9] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86};
co2Serial->write(cmd, 9); //request ABC = OFF
}
/*******************************************************************************************/
// Return PPM of CO2
// Returns -1 if Error
// Returns -2 if Not initialised
// Returns -3 if Invalid values
// Returns -val if Unreliable reading
int MHZ19::readCO2(void)
{
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
char response[9];
co2Serial->write(cmd, 9); //request PPM CO2
co2Serial->readBytes(response, 9);
/*
Serial.print("--->");
for (int i=0; i<9; i++)
{
Serial.print(' ');
Serial.print(response[i], HEX);
}
Serial.println("");
*/
byte crc = 0;
for (byte i=1; i<8; i++) crc += response[i];
crc = 255 - crc;
crc++;
if (crc != response[8])
{
for (byte i=0; i<8; i++)
{
if (co2Serial->peek() == 0xFF)
return -1;
co2Serial->read(); // Shifting to solve misalignment
}
}
if (response[0] != 0xFF) // Wrong starting byte from co2 sensor!
return -1;
if (response[1] != 0x86) // Wrong command from co2 sensor!
return -1;
/* Serial.print("Temp: ");
Serial.println(response[4] - 40); */
int responseHigh = (int) response[2];
int responseLow = (int) response[3];
int ppm = (256 * responseHigh) + responseLow;
int uHigh = (int) response[6];
int uLow = (int) response[7];
int u = (256 * uHigh) + uLow;
Serial.print("CO2: ");
Serial.print(ppm);
Serial.print(" ");
Serial.println(int(response[5]));
if (u == 15000)
return -2;
if ((ppm < 100) || (ppm > 6000))
return -3;
if (response[5] != 64)
return -ppm; // Not reliable. For information only
return ppm;
}
/*******************************************************************************************/
// Return Average PPM of CO2
// Return -1 if No values
int MHZ19::readCO2Average(bool reset)
{
int ppm = readCO2();
if (ppm > 0)
{
ppmTotal += ppm;
ppmCount ++;
/* Serial.print(ppmTotal / ppmCount);
Serial.print(" ");
Serial.print(ppmCount); */
}
if (ppmCount == 0)
return -1;
unsigned int avg = ppmTotal / ppmCount;
if (reset)
{
ppmTotal = 0;
ppmCount = 0;
}
return avg;
}