Skip to content

Commit 06dede8

Browse files
committed
0.2.0 MHZCO2
1 parent 2d4dbde commit 06dede8

File tree

6 files changed

+68
-26
lines changed

6 files changed

+68
-26
lines changed

libraries/MHZCO2/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.2.0] - 2023-12-29
10+
- Fix #9 bug in **setPPM()**
11+
- fix race condition in **receive()**
12+
- update readme.md
13+
- minor edits
14+
15+
----
16+
917
## [0.1.4] - 2023-11-14
1018
- update readme.md
1119
- add plotter example (MEGA)
1220
- update keywords.
1321
- minor edits
1422

15-
1623
## [0.1.3] - 2023-07-27
1724
- remove MTP40F as it is not compatible
1825
- update examples.

libraries/MHZCO2/MHZCO2.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MHZCO2.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.4
4+
// VERSION: 0.2.0
55
// PURPOSE: Arduino Library for MHZ series CO2 sensors
66
// DATE: 2020-05-05
77
// URL: https://github.com/RobTillaart/MHZCO2
@@ -38,7 +38,10 @@ void MHZCO2::setPPM(uint16_t PPM)
3838
{
3939
_PPM = PPM;
4040
uint8_t data[9] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
41-
data[8] = PPM >> 8;
41+
// as max value is lower than 65536 only two bytes need to be set.
42+
// data[4] = 0;
43+
// data[5[ = 0;
44+
data[6] = PPM >> 8;
4245
data[7] = PPM & 0xFF;
4346
data[8] = checksum(data);
4447
send(data, 9);
@@ -137,23 +140,32 @@ void MHZCO2::calibrateAuto(bool mode)
137140
void MHZCO2::send(uint8_t * data, uint8_t len)
138141
{
139142
_str->write(data, len);
140-
_str->flush();
143+
_str->flush(); // do not return until all data is sent.
141144
}
142145

143146

144147
int MHZCO2::receive(uint8_t * answer)
145148
{
146149
uint32_t start = millis();
147-
while (_str->available() == 0)
148-
{
149-
if (millis() - start > 1000) return MHZCO2_TIMEOUT;
150-
}
151-
for (uint8_t i = 0; i < 9; i++)
150+
151+
// need to read 9 bytes.
152+
uint8_t i = 0;
153+
while (i < 9)
152154
{
153-
answer[i] = _str->read();
155+
if (_str->available())
156+
{
157+
answer[i] = _str->read();
158+
i++;
159+
}
160+
else
161+
{
162+
// note: hardcoded timeout
163+
if (millis() - start > 1000) return MHZCO2_TIMEOUT;
164+
}
154165
}
155166
// verify checksum
156167
if (answer[8] != checksum(answer)) return MHZCO2_ERROR_CRC;
168+
157169
return MHZCO2_OK;
158170
}
159171

@@ -170,15 +182,6 @@ uint8_t MHZCO2::checksum(uint8_t *arr)
170182
}
171183

172184

173-
/* slightly faster
174-
uint8_t MHZCO2::checksum(uint8_t *arr)
175-
{
176-
uint8_t sum = 0xFF;
177-
for (uint8_t i = 1; i < 8; i++) sum -= arr[i];
178-
return sum + 1;
179-
}
180-
*/
181-
182185

183186
/////////////////////////////////////////////////////////
184187
//

libraries/MHZCO2/MHZCO2.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//
33
// FILE: MHZCO2.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.4
5+
// VERSION: 0.2.0
66
// PURPOSE: Arduino Library for MHZ series CO2 sensors
77
// DATE: 2020-05-05
88
// URL: https://github.com/RobTillaart/MHZCO2
99
//
1010

1111
#include "Arduino.h"
1212

13-
#define MHZCO2_LIB_VERSION (F("0.1.4"))
13+
#define MHZCO2_LIB_VERSION (F("0.2.0"))
1414

1515
#define MHZCO2_OK 0
1616
#define MHZCO2_TIMEOUT -10
@@ -25,11 +25,13 @@ class MHZCO2
2525
void begin(Stream * str);
2626
uint32_t uptime();
2727

28+
2829
// PPM = 2000, 5000, 10000 (other values unknown)
2930
// check datasheet
3031
void setPPM(uint16_t PPM);
3132
uint16_t getPPM();
3233

34+
3335
// MEASUREMENT
3436
int measure();
3537
uint32_t lastMeasurement();
@@ -38,12 +40,14 @@ class MHZCO2
3840
int getAccuracy();
3941
int getMinCO2();
4042

43+
4144
// CALIBRATION
4245
// USE WITH CARE => READ DATASHEET!
4346
void calibrateZero();
4447
void calibrateSpan(uint16_t span);
4548
void calibrateAuto(bool mode = true);
4649

50+
4751
protected:
4852
Stream * _str;
4953
uint32_t _startTime;
@@ -61,6 +65,7 @@ class MHZCO2
6165
};
6266

6367

68+
6469
/////////////////////////////////////////////////////////
6570
//
6671
// DERIVED CLASSES

libraries/MHZCO2/README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,34 @@ This might change in the future as compatibles might differ on detail.
2525

2626
Reference: user manual MHZ129B 2019-04-25 version 1.4
2727

28+
#### Version 0.2.0
29+
30+
Version 0.2.0 fixes a bug in **setPPM()** which makes older versions obsolete.
31+
32+
2833
#### Compatibles
2934

3035
This list is not verified although these devices should be compatible based upon datasheet.
3136

37+
There exists different models of 400-2000 PPM and 400-5000 PPM and 400-10000 PPM.
38+
As far as known these have the same interface as there is very little information to be found.
39+
40+
3241
| type | precision | notes |
3342
|:-----------|:----------:|:--------|
3443
| MHZ1311A | 50ppm + 5% | energy saving version
3544
| MHZ19 | 50ppm + 5% |
3645
| MHZ19B | 50ppm + 3% | test device
37-
| MHZ19C | 50ppm + 5% |
46+
| MHZ19C | 50ppm + 5% | (1)
3847
| MHZ19D | 50ppm + 5% |
3948
| MHZ19E | 50ppm + 5% |
4049

50+
Note (1):
51+
There exists different models of the MHZ19C and probably others.
52+
The range can go from 400-2000 PPM, 400-5000 PPM and 400-10000 PPM.
53+
As far as known these have the same interface as there is very little
54+
information to be found. See #9.
55+
4156
Note: The calibration of the MHZ1311A is different than MHZ19x series
4257

4358
If there are compatible devices missing in this list, please let me know.
@@ -85,7 +100,8 @@ or a softwareSerial port.
85100

86101
#### Measure
87102

88-
- **int measure()** workhorse, send command to read the sensor.
103+
- **int measure()** workhorse, send command to read the sensor and
104+
waits until an answer is received. Return values see below.
89105
- **uint32_t lastMeasurement()** timestamp in milliseconds of last measurement.
90106
- **int getCO2()** returns CO2 PPM last measurement.
91107
- **int getTemperature()** returns temperature last measurement.
@@ -94,6 +110,16 @@ or a softwareSerial port.
94110

95111
The latter two might not be supported by all MH sensors.
96112

113+
114+
Return values of **measure()**
115+
116+
| value | Name | Description |
117+
|:-------:|:------------------:|:--------------|
118+
| 0 | MHZCO2_OK | measurement succeeded.
119+
| -10 | MHZCO2_TIMEOUT | to too long to receive an answer
120+
| -11 | MHZCO2_ERROR_CRC | Checksum error, handle answer with care.
121+
122+
97123
#### Calibration
98124

99125
**WARNING:** use with care, read the datasheet as these commands may disrupt your sensor.
@@ -118,7 +144,6 @@ See - https://keelingcurve.ucsd.edu/
118144
- improve documentation
119145
- buy hardware MHZ19B / MHZ19C
120146
- test with hardware
121-
- verify checksum
122147
- verify timeout
123148

124149

@@ -130,6 +155,8 @@ See - https://keelingcurve.ucsd.edu/
130155

131156
#### Could
132157

158+
- investigate configurable timeout. now hardcoded 1 second.
159+
- 2 bytes + 2 functions.
133160
- extend unit tests
134161
- add type info for derived classes?
135162
- A .. E ?

libraries/MHZCO2/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/MHZCO2.git"
1717
},
18-
"version": "0.1.4",
18+
"version": "0.2.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/MHZCO2/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MHZCO2
2-
version=0.1.4
2+
version=0.2.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino Library for MHZ series CO2 sensors.

0 commit comments

Comments
 (0)