mirror of
https://github.com/sigmasternchen/MH-Z-CO2-Sensors
synced 2025-03-15 06:38:55 +00:00
Implemented isPreHeating and isReady.
This commit is contained in:
parent
99729af090
commit
83ba964a90
3 changed files with 96 additions and 11 deletions
48
MHZ.cpp
48
MHZ.cpp
|
@ -5,6 +5,16 @@
|
|||
|
||||
#include "MHZ.h"
|
||||
|
||||
const int MHZ14A = 14;
|
||||
const int MHZ19B = 19;
|
||||
|
||||
const int STATUS_NO_RESPONSE = -2;
|
||||
const int STATUS_CHECKSUM_MISMATCH = -3;
|
||||
const int STATUS_INCOMPLETE = -4;
|
||||
const int STATUS_NOT_READY = -5;
|
||||
|
||||
unsigned long lastRequest = 0;
|
||||
|
||||
MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
|
||||
: co2Serial(rxpin, txpin) {
|
||||
_rxpin = rxpin;
|
||||
|
@ -15,6 +25,9 @@ MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
|
|||
co2Serial.begin(9600);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables the debug mode (more logging).
|
||||
*/
|
||||
void MHZ::setDebug(boolean enable) {
|
||||
debug = enable;
|
||||
if (debug) {
|
||||
|
@ -24,15 +37,40 @@ void MHZ::setDebug(boolean enable) {
|
|||
}
|
||||
}
|
||||
|
||||
int retryCount = 0;
|
||||
boolean MHZ::isPreHeating() {
|
||||
if (_type == MHZ14A) {
|
||||
return millis() < (3 * 60 * 1000);
|
||||
} else if (_type == MHZ19B) {
|
||||
return millis() < (3 * 60 * 1000);
|
||||
} else {
|
||||
Serial.println("MHZ::isPreHeating() => UNKNOWN SENSOR");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
boolean MHZ::isReady() {
|
||||
if (isPreHeating()) return false;
|
||||
if (_type == MHZ14A)
|
||||
return lastRequest < millis() - 90 * 1000;
|
||||
else if (_type == MHZ19B)
|
||||
return lastRequest < millis() - 60 * 1000;
|
||||
else {
|
||||
Serial.print("MHZ::isReady() => UNKNOWN SENSOR \"");
|
||||
Serial.print(_type);
|
||||
Serial.println("\"");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int MHZ::readCO2UART() {
|
||||
if (!isReady()) return STATUS_NOT_READY;
|
||||
if (debug) Serial.println("-- read CO2 uart ---");
|
||||
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
|
||||
byte response[9]; // for answer
|
||||
|
||||
if (debug) Serial.print(" >> Sending CO2 request");
|
||||
co2Serial.write(cmd, 9); // request PPM CO2
|
||||
lastRequest = millis();
|
||||
|
||||
// clear the buffer
|
||||
memset(response, 0, 9);
|
||||
|
@ -124,9 +162,12 @@ int MHZ::readCO2UART() {
|
|||
return ppm_uart;
|
||||
}
|
||||
|
||||
uint8_t MHZ::getLastTemperature() { return temperature; }
|
||||
uint8_t MHZ::getLastTemperature() {
|
||||
if (!isReady()) return STATUS_NOT_READY;
|
||||
return temperature;
|
||||
}
|
||||
|
||||
byte MHZ::getCheckSum(byte *packet) {
|
||||
byte MHZ::getCheckSum(byte* packet) {
|
||||
if (debug) Serial.println(" getCheckSum()");
|
||||
byte i;
|
||||
unsigned char checksum = 0;
|
||||
|
@ -139,6 +180,7 @@ byte MHZ::getCheckSum(byte *packet) {
|
|||
}
|
||||
|
||||
int MHZ::readCO2PWM() {
|
||||
// if (!isReady()) return STATUS_NOT_READY; not needed?
|
||||
if (debug) Serial.print("-- reading CO2 from pwm ");
|
||||
unsigned long th, tl, ppm_pwm = 0;
|
||||
do {
|
||||
|
|
18
MHZ.h
18
MHZ.h
|
@ -11,16 +11,18 @@
|
|||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
// Define types of sensors.
|
||||
#define MHZ14A 14
|
||||
#define MHZ19B 19
|
||||
|
||||
#define STATUS_NO_RESPONSE -2
|
||||
#define STATUS_CHECKSUM_MISMATCH -3
|
||||
#define STATUS_INCOMPLETE -4
|
||||
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
// types of sensors.
|
||||
extern const int MHZ14A;
|
||||
extern const int MHZ19B;
|
||||
|
||||
// status codes
|
||||
extern const int STATUS_NO_RESPONSE;
|
||||
extern const int STATUS_CHECKSUM_MISMATCH;
|
||||
extern const int STATUS_INCOMPLETE;
|
||||
extern const int STATUS_NOT_READY;
|
||||
|
||||
class MHZ {
|
||||
public:
|
||||
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type);
|
||||
|
|
41
keywords.txt
Normal file
41
keywords.txt
Normal file
|
@ -0,0 +1,41 @@
|
|||
#######################################
|
||||
# Syntax Coloring Map For MHZ
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
MHZ KEYWORD1
|
||||
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
setDebug KEYWORD2
|
||||
isPreHeating KEYWORD2
|
||||
isReady KEYWORD2
|
||||
readCO2UART KEYWORD2
|
||||
readCO2PWM KEYWORD2
|
||||
getLastTemperature KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
co2 KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
MHZ14A LITERAL1
|
||||
MHZ19B LITERAL1
|
||||
|
||||
STATUS_NO_RESPONSE LITERAL1
|
||||
STATUS_CHECKSUM_MISMATCH LITERAL1
|
||||
STATUS_INCOMPLETE LITERAL1
|
||||
STATUS_NOT_READY LITERAL1
|
Loading…
Reference in a new issue