mirror of
https://github.com/sigmasternchen/MH-Z-CO2-Sensors
synced 2025-03-15 06:38:55 +00:00
Added isPreheating and isReady to MHZ.h and reformatted.
This commit is contained in:
parent
7158f63459
commit
99729af090
4 changed files with 34 additions and 43 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
.vscode/arduino.json
|
||||
.vscode/c_cpp_properties.json
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
|
||||
#include <SoftwareSerial.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include "MHZ.h"
|
||||
|
||||
#define CO2_IN D2
|
||||
|
@ -9,7 +9,6 @@
|
|||
#define MH_Z19_RX D7
|
||||
#define MH_Z19_TX D6
|
||||
|
||||
|
||||
MHZ co2(MH_Z19_RX, MH_Z19_TX, CO2_IN, MHZ19B);
|
||||
|
||||
void setup() {
|
||||
|
@ -18,10 +17,9 @@ void setup() {
|
|||
delay(100);
|
||||
Serial.println("MHZ 19B");
|
||||
|
||||
co2.setDebug(false);
|
||||
co2.setDebug(true);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// Serial.print("\n----- Time from start: ");
|
||||
// Serial.print(millis() / 1000);
|
||||
|
|
37
MHZ.cpp
37
MHZ.cpp
|
@ -5,10 +5,8 @@
|
|||
|
||||
#include "MHZ.h"
|
||||
|
||||
|
||||
MHZ:: MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
|
||||
: co2Serial(rxpin, txpin)
|
||||
{
|
||||
MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
|
||||
: co2Serial(rxpin, txpin) {
|
||||
_rxpin = rxpin;
|
||||
_txpin = txpin;
|
||||
_pwmpin = pwmpin;
|
||||
|
@ -17,7 +15,6 @@ MHZ:: MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
|
|||
co2Serial.begin(9600);
|
||||
}
|
||||
|
||||
|
||||
void MHZ::setDebug(boolean enable) {
|
||||
debug = enable;
|
||||
if (debug) {
|
||||
|
@ -27,16 +24,15 @@ void MHZ::setDebug(boolean enable) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
int retryCount = 0;
|
||||
|
||||
int MHZ::readCO2UART() {
|
||||
if (debug) Serial.println("-- read CO2 uart ---");
|
||||
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
|
||||
byte response[9]; // for answer
|
||||
byte response[9]; // for answer
|
||||
|
||||
if (debug) Serial.print(" >> Sending CO2 request");
|
||||
co2Serial.write(cmd, 9); //request PPM CO2
|
||||
co2Serial.write(cmd, 9); // request PPM CO2
|
||||
|
||||
// clear the buffer
|
||||
memset(response, 0, 9);
|
||||
|
@ -44,20 +40,20 @@ int MHZ::readCO2UART() {
|
|||
int waited = 0;
|
||||
while (co2Serial.available() == 0) {
|
||||
if (debug) Serial.print(".");
|
||||
delay(100); // wait a short moment to avoid false reading
|
||||
if(waited++ > 10) {
|
||||
delay(100); // wait a short moment to avoid false reading
|
||||
if (waited++ > 10) {
|
||||
if (debug) Serial.println("No response after 10 seconds");
|
||||
co2Serial.flush();
|
||||
return STATUS_NO_RESPONSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (debug) Serial.println();
|
||||
|
||||
// The serial stream can get out of sync. The response starts with 0xff, try to resync.
|
||||
// The serial stream can get out of sync. The response starts with 0xff, try
|
||||
// to resync.
|
||||
// TODO: I think this might be wrong any only happens during initialization?
|
||||
boolean skip = false;
|
||||
while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF)
|
||||
{
|
||||
while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF) {
|
||||
if (!skip) {
|
||||
Serial.print("MHZ: - skipping unexpected readings:");
|
||||
skip = true;
|
||||
|
@ -68,10 +64,9 @@ int MHZ::readCO2UART() {
|
|||
}
|
||||
if (skip) Serial.println();
|
||||
|
||||
|
||||
if (co2Serial.available() > 0) {
|
||||
int count = co2Serial.readBytes(response, 9);
|
||||
if (count <9) {
|
||||
if (count < 9) {
|
||||
co2Serial.flush();
|
||||
return STATUS_INCOMPLETE;
|
||||
}
|
||||
|
@ -105,7 +100,7 @@ int MHZ::readCO2UART() {
|
|||
|
||||
int ppm_uart = 256 * (int)response[2] + response[3];
|
||||
|
||||
temperature = response[4] - 44; // - 40;
|
||||
temperature = response[4] - 44; // - 40;
|
||||
|
||||
byte status = response[5];
|
||||
if (debug) {
|
||||
|
@ -116,7 +111,7 @@ int MHZ::readCO2UART() {
|
|||
}
|
||||
|
||||
// Is always 0 for version 14a and 19b
|
||||
// Version a?: status != 0x40
|
||||
// Version 19a?: status != 0x40
|
||||
if (debug || status != 0) {
|
||||
Serial.print(" ! Status maybe not OK ! ");
|
||||
Serial.println(status, HEX);
|
||||
|
@ -125,13 +120,11 @@ int MHZ::readCO2UART() {
|
|||
Serial.println(status, HEX);
|
||||
}
|
||||
|
||||
// co2Serial.flush();
|
||||
co2Serial.flush();
|
||||
return ppm_uart;
|
||||
}
|
||||
|
||||
uint8_t MHZ::getLastTemperature() {
|
||||
return temperature;
|
||||
}
|
||||
uint8_t MHZ::getLastTemperature() { return temperature; }
|
||||
|
||||
byte MHZ::getCheckSum(byte *packet) {
|
||||
if (debug) Serial.println(" getCheckSum()");
|
||||
|
|
31
MHZ.h
31
MHZ.h
|
@ -11,38 +11,35 @@
|
|||
#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>
|
||||
|
||||
|
||||
class MHZ {
|
||||
public:
|
||||
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type);
|
||||
public:
|
||||
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type);
|
||||
|
||||
void setDebug(boolean enable);
|
||||
void setDebug(boolean enable);
|
||||
|
||||
int readCO2UART();
|
||||
int readCO2PWM();
|
||||
uint8_t getLastTemperature();
|
||||
boolean isPreHeating();
|
||||
boolean isReady();
|
||||
|
||||
private:
|
||||
uint8_t _rxpin, _txpin, _pwmpin, _type, temperature;
|
||||
boolean debug = false;
|
||||
int readCO2UART();
|
||||
int readCO2PWM();
|
||||
uint8_t getLastTemperature();
|
||||
|
||||
SoftwareSerial co2Serial;
|
||||
byte getCheckSum(byte *packet);
|
||||
private:
|
||||
uint8_t _rxpin, _txpin, _pwmpin, _type, temperature;
|
||||
boolean debug = false;
|
||||
|
||||
SoftwareSerial co2Serial;
|
||||
byte getCheckSum(byte *packet);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in a new issue