0.2.0 MHZCO2

This commit is contained in:
Rob Tillaart 2023-12-29 16:57:01 +01:00
parent 2d4dbde8f2
commit 06dede87d1
6 changed files with 68 additions and 26 deletions

View File

@ -6,13 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.0] - 2023-12-29
- Fix #9 bug in **setPPM()**
- fix race condition in **receive()**
- update readme.md
- minor edits
----
## [0.1.4] - 2023-11-14
- update readme.md
- add plotter example (MEGA)
- update keywords.
- minor edits
## [0.1.3] - 2023-07-27
- remove MTP40F as it is not compatible
- update examples.

View File

@ -1,7 +1,7 @@
//
// FILE: MHZCO2.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.2.0
// PURPOSE: Arduino Library for MHZ series CO2 sensors
// DATE: 2020-05-05
// URL: https://github.com/RobTillaart/MHZCO2
@ -38,7 +38,10 @@ void MHZCO2::setPPM(uint16_t PPM)
{
_PPM = PPM;
uint8_t data[9] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
data[8] = PPM >> 8;
// as max value is lower than 65536 only two bytes need to be set.
// data[4] = 0;
// data[5[ = 0;
data[6] = PPM >> 8;
data[7] = PPM & 0xFF;
data[8] = checksum(data);
send(data, 9);
@ -137,23 +140,32 @@ void MHZCO2::calibrateAuto(bool mode)
void MHZCO2::send(uint8_t * data, uint8_t len)
{
_str->write(data, len);
_str->flush();
_str->flush(); // do not return until all data is sent.
}
int MHZCO2::receive(uint8_t * answer)
{
uint32_t start = millis();
while (_str->available() == 0)
// need to read 9 bytes.
uint8_t i = 0;
while (i < 9)
{
if (millis() - start > 1000) return MHZCO2_TIMEOUT;
}
for (uint8_t i = 0; i < 9; i++)
if (_str->available())
{
answer[i] = _str->read();
i++;
}
else
{
// note: hardcoded timeout
if (millis() - start > 1000) return MHZCO2_TIMEOUT;
}
}
// verify checksum
if (answer[8] != checksum(answer)) return MHZCO2_ERROR_CRC;
return MHZCO2_OK;
}
@ -170,15 +182,6 @@ uint8_t MHZCO2::checksum(uint8_t *arr)
}
/* slightly faster
uint8_t MHZCO2::checksum(uint8_t *arr)
{
uint8_t sum = 0xFF;
for (uint8_t i = 1; i < 8; i++) sum -= arr[i];
return sum + 1;
}
*/
/////////////////////////////////////////////////////////
//

View File

@ -2,7 +2,7 @@
//
// FILE: MHZCO2.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.2.0
// PURPOSE: Arduino Library for MHZ series CO2 sensors
// DATE: 2020-05-05
// URL: https://github.com/RobTillaart/MHZCO2
@ -10,7 +10,7 @@
#include "Arduino.h"
#define MHZCO2_LIB_VERSION (F("0.1.4"))
#define MHZCO2_LIB_VERSION (F("0.2.0"))
#define MHZCO2_OK 0
#define MHZCO2_TIMEOUT -10
@ -25,11 +25,13 @@ public:
void begin(Stream * str);
uint32_t uptime();
// PPM = 2000, 5000, 10000 (other values unknown)
// check datasheet
void setPPM(uint16_t PPM);
uint16_t getPPM();
// MEASUREMENT
int measure();
uint32_t lastMeasurement();
@ -38,12 +40,14 @@ public:
int getAccuracy();
int getMinCO2();
// CALIBRATION
// USE WITH CARE => READ DATASHEET!
void calibrateZero();
void calibrateSpan(uint16_t span);
void calibrateAuto(bool mode = true);
protected:
Stream * _str;
uint32_t _startTime;
@ -61,6 +65,7 @@ protected:
};
/////////////////////////////////////////////////////////
//
// DERIVED CLASSES

View File

@ -25,19 +25,34 @@ This might change in the future as compatibles might differ on detail.
Reference: user manual MHZ129B 2019-04-25 version 1.4
#### Version 0.2.0
Version 0.2.0 fixes a bug in **setPPM()** which makes older versions obsolete.
#### Compatibles
This list is not verified although these devices should be compatible based upon datasheet.
There exists different models of 400-2000 PPM and 400-5000 PPM and 400-10000 PPM.
As far as known these have the same interface as there is very little information to be found.
| type | precision | notes |
|:-----------|:----------:|:--------|
| MHZ1311A | 50ppm + 5% | energy saving version
| MHZ19 | 50ppm + 5% |
| MHZ19B | 50ppm + 3% | test device
| MHZ19C | 50ppm + 5% |
| MHZ19C | 50ppm + 5% | (1)
| MHZ19D | 50ppm + 5% |
| MHZ19E | 50ppm + 5% |
Note (1):
There exists different models of the MHZ19C and probably others.
The range can go from 400-2000 PPM, 400-5000 PPM and 400-10000 PPM.
As far as known these have the same interface as there is very little
information to be found. See #9.
Note: The calibration of the MHZ1311A is different than MHZ19x series
If there are compatible devices missing in this list, please let me know.
@ -85,7 +100,8 @@ or a softwareSerial port.
#### Measure
- **int measure()** workhorse, send command to read the sensor.
- **int measure()** workhorse, send command to read the sensor and
waits until an answer is received. Return values see below.
- **uint32_t lastMeasurement()** timestamp in milliseconds of last measurement.
- **int getCO2()** returns CO2 PPM last measurement.
- **int getTemperature()** returns temperature last measurement.
@ -94,6 +110,16 @@ or a softwareSerial port.
The latter two might not be supported by all MH sensors.
Return values of **measure()**
| value | Name | Description |
|:-------:|:------------------:|:--------------|
| 0 | MHZCO2_OK | measurement succeeded.
| -10 | MHZCO2_TIMEOUT | to too long to receive an answer
| -11 | MHZCO2_ERROR_CRC | Checksum error, handle answer with care.
#### Calibration
**WARNING:** use with care, read the datasheet as these commands may disrupt your sensor.
@ -118,7 +144,6 @@ See - https://keelingcurve.ucsd.edu/
- improve documentation
- buy hardware MHZ19B / MHZ19C
- test with hardware
- verify checksum
- verify timeout
@ -130,6 +155,8 @@ See - https://keelingcurve.ucsd.edu/
#### Could
- investigate configurable timeout. now hardcoded 1 second.
- 2 bytes + 2 functions.
- extend unit tests
- add type info for derived classes?
- A .. E ?

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MHZCO2.git"
},
"version": "0.1.4",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=MHZCO2
version=0.1.4
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library for MHZ series CO2 sensors.