From 8a063e7c277c7240e1123cf24643f96c837c457b Mon Sep 17 00:00:00 2001 From: Christoph Schultz Date: Thu, 14 Jan 2021 22:28:47 +0100 Subject: [PATCH] Add support for setting the range and autocalibration. Closes #1. * added possibility to set Range, to enable/disable Autocalibrate and to calibrate maually ( see Datasheet v 1.2 : https://www.winsen-sensor.com/d/files/MH-Z14A.pdf * Update MHZ.cpp * Update MHZ.cpp * Update MHZ.h added calibrate Span, for complete functionality but commented out , due to professional use (req. constant atmosphere with 2k, 5k or 10k ppm CO2.). corrected the bugs in MHZ.cpp also. * Update MHZ.cpp Added comments for Sensors with specific firmware. Co-authored-by: Christoph Schultz --- MHZ.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MHZ.h | 8 ++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/MHZ.cpp b/MHZ.cpp index 7c01714..6251b88 100644 --- a/MHZ.cpp +++ b/MHZ.cpp @@ -7,6 +7,9 @@ const int MHZ14A = 14; const int MHZ19B = 19; +const int MHZ_2K = 1; +const int MHZ_5K = 2; +const int MHZ_10K = 3; const unsigned long MHZ14A_PREHEATING_TIME = 3L * 60L * 1000L; const unsigned long MHZ19B_PREHEATING_TIME = 3L * 60L * 1000L; @@ -251,3 +254,70 @@ int MHZ::readCO2PWM() { } return ppm_pwm; } + +void MHZ::setAutoCalibrate(boolean b) //only available for MHZ-19B with firmware < 1.6 and MHZ 14a +{ + uint8_t cmd_enableAutoCal[9] = { 0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6 }; + uint8_t cmd_disableAutoCal[9] = { 0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86}; + if (b) + { + _serial->write(cmd_enableAutoCal,9); + } + else + { + _serial->write(cmd_disableAutoCal,9); + } +} + +void MHZ::setRange(int range) //only available for MHZ-19B < 1.6 and MH-Z 14a +{ + uint8_t cmd_2K[9] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x8F}; + uint8_t cmd_5K[9] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB}; + uint8_t cmd_10K[9] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x27, 0x10, 0x2F}; + + switch(range) + { + case 1: + _serial->write(cmd_2K,9); + break; + case 2: + _serial->write(cmd_5K,9); + break; + case 3: + _serial->write(cmd_10K,9); + + } +} + +void MHZ::calibrateZero() +{ + char cmd[9] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78}; + _serial->write(cmd,9); +} + +/***** calibrateSpan() function for professional use. requires a constant atmosphere with 2K, 5k or 10k ppm CO2 and calibrateZero at first. + +void MHZ::calibrateSpan(int range) +{ + char cmd_2K[9] = {0xFF, 0x01, 0x88, 0x07, 0xD0, 0x00, 0x00, 0x00, 0xA0}; + char cmd_5K[9] = {oxFF, 0x01, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0xDC}; + char cmd_10K[9]= {0xFF, 0x01, 0x88, 0x27, 0x10, 0x00, 0x00, 0x00, 0x40}; + + switch(range) + { + case 1: + _serial->write(cmd_2K,9); + break; + case 2: + _serial->write(cmd_5K,9); + break; + case 3: + _serial->write(cmd_10k,9); + } + + } + ****/ + + + + diff --git a/MHZ.h b/MHZ.h index cd2c2fb..e03c67a 100644 --- a/MHZ.h +++ b/MHZ.h @@ -16,7 +16,9 @@ // types of sensors. extern const int MHZ14A; extern const int MHZ19B; - +extern const int MHZ_2K; +extern const int MHZ_5k; +extern const int MHZ_10K; // status codes extern const int STATUS_NO_RESPONSE; extern const int STATUS_CHECKSUM_MISMATCH; @@ -35,6 +37,10 @@ class MHZ { boolean isPreHeating(); boolean isReady(); + void setAutoCalibrate(boolean b); + void calibrateZero(); + void setRange(int range); + // void calibrateSpan(int range); //only for professional use... see implementation and Dataheet. int readCO2UART(); int readCO2PWM();