Skip to content

Commit

Permalink
Merge pull request #9 from ahorn42/master
Browse files Browse the repository at this point in the history
Added some fixes and improvments
  • Loading branch information
tobiasschuerg authored Jun 8, 2019
2 parents 6314890 + 253f8ae commit 7f33a73
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@

.vscode/arduino.json
.vscode/c_cpp_properties.json
.vscode/*
66 changes: 57 additions & 9 deletions MHZ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
const int MHZ14A = 14;
const int MHZ19B = 19;

const int MHZ14A_RESPONSE_TIME = 60;
const int MHZ19B_RESPONSE_TIME = 120;
const int MHZ14A_PREHEATING_TIME = 3 * 60 * 1000;
const int MHZ19B_PREHEATING_TIME = 3 * 60 * 1000;

const int MHZ14A_RESPONSE_TIME = 60 * 1000;
const int MHZ19B_RESPONSE_TIME = 120 * 1000;

const int STATUS_NO_RESPONSE = -2;
const int STATUS_CHECKSUM_MISMATCH = -3;
const int STATUS_INCOMPLETE = -4;
const int STATUS_NOT_READY = -5;
const int STATUS_PWM_NOT_CONFIGURED = -6;
const int STATUS_SERIAL_NOT_CONFIGURED = -7;

unsigned long lastRequest = 0;

bool SerialConfigured = true;
bool PwmConfigured = true;

MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type) {
SoftwareSerial * ss = new SoftwareSerial(rxpin, txpin);
_pwmpin = pwmpin;
Expand All @@ -27,10 +35,34 @@ MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type) {
_serial = ss;
}

MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t type) {
SoftwareSerial * ss = new SoftwareSerial(rxpin, txpin);
_type = type;

ss->begin(9600);
_serial = ss;

PwmConfigured = false;
}

MHZ::MHZ(uint8_t pwmpin, uint8_t type) {
_pwmpin = pwmpin;
_type = type;

SerialConfigured = false;
}

MHZ::MHZ(Stream * serial, uint8_t pwmpin, uint8_t type) {
_serial = serial;
_pwmpin = pwmpin;
_type = type;
_serial = serial;
_pwmpin = pwmpin;
_type = type;
}

MHZ::MHZ(Stream * serial, uint8_t type) {
_serial = serial;
_type = type;

PwmConfigured = false;
}

/**
Expand All @@ -47,9 +79,9 @@ void MHZ::setDebug(boolean enable) {

boolean MHZ::isPreHeating() {
if (_type == MHZ14A) {
return millis() < (3 * 60 * 1000);
return millis() < (MHZ14A_PREHEATING_TIME);
} else if (_type == MHZ19B) {
return millis() < (3 * 60 * 1000);
return millis() < (MHZ19B_PREHEATING_TIME);
} else {
Serial.println(F("MHZ::isPreHeating() => UNKNOWN SENSOR"));
return false;
Expand All @@ -71,6 +103,10 @@ boolean MHZ::isReady() {
}

int MHZ::readCO2UART() {
if (!SerialConfigured) {
if (debug) Serial.println(F("-- serial is not configured"));
return STATUS_SERIAL_NOT_CONFIGURED;
}
if (!isReady()) return STATUS_NOT_READY;
if (debug) Serial.println(F("-- read CO2 uart ---"));
byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
Expand Down Expand Up @@ -170,12 +206,20 @@ int MHZ::readCO2UART() {
return ppm_uart;
}

uint8_t MHZ::getLastTemperature() {
int MHZ::getLastTemperature() {
if (!SerialConfigured) {
if (debug) Serial.println(F("-- serial is not configured"));
return STATUS_SERIAL_NOT_CONFIGURED;
}
if (isPreHeating()) return STATUS_NOT_READY;
return temperature;
}

byte MHZ::getCheckSum(byte* packet) {
if (!SerialConfigured) {
if (debug) Serial.println(F("-- serial is not configured"));
return STATUS_SERIAL_NOT_CONFIGURED;
}
if (debug) Serial.println(F(" getCheckSum()"));
byte i;
unsigned char checksum = 0;
Expand All @@ -188,7 +232,11 @@ byte MHZ::getCheckSum(byte* packet) {
}

int MHZ::readCO2PWM() {
// if (!isReady()) return STATUS_NOT_READY; not needed?
if (!PwmConfigured) {
if (debug) Serial.println(F("-- pwm is not configured "));
return STATUS_PWM_NOT_CONFIGURED;
}
//if (!isReady()) return STATUS_NOT_READY; not needed?
if (debug) Serial.print(F("-- reading CO2 from pwm "));
unsigned long th, tl, ppm_pwm = 0;
do {
Expand Down
5 changes: 4 additions & 1 deletion MHZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ extern const int STATUS_NOT_READY;
class MHZ {
public:
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type);
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t type);
MHZ(uint8_t pwmpin, uint8_t type);
MHZ(Stream * serial, uint8_t pwmpin, uint8_t type);
MHZ(Stream * serial, uint8_t type);

void setDebug(boolean enable);

Expand All @@ -35,7 +38,7 @@ class MHZ {

int readCO2UART();
int readCO2PWM();
uint8_t getLastTemperature();
int getLastTemperature();

private:
uint8_t _pwmpin, _type, temperature;
Expand Down
18 changes: 11 additions & 7 deletions examples/MH-Z19B/MH-Z19B.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include "MHZ.h"
#include <MHZ.h>

// pin for uart reading
// pin for pwm reading
#define CO2_IN 10

// pin for pwm reading
// pin for uart reading
#define MH_Z19_RX D4 // D7
#define MH_Z19_TX D0 // D6

Expand Down Expand Up @@ -38,19 +36,25 @@ void loop() {
Serial.println(" s");

int ppm_uart = co2.readCO2UART();
Serial.print("PPMuart: ");

if (ppm_uart > 0) {
Serial.print("PPMuart: ");
Serial.print(ppm_uart);
} else {
Serial.print("n/a");
}

int ppm_pwm = co2.readCO2PWM();
Serial.print(", PPMpwm: ");
Serial.print(ppm_pwm);

int temperature = co2.getLastTemperature();
Serial.print(", Temperature: ");

if (temperature > 0) {
Serial.print(", Temperature: ");
Serial.println(temperature);
} else {
Serial.println("n/a");
}

Serial.println("\n------------------------------");
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MH-Z CO2 Sensors
version=1.1.0
version=1.2.0
author=Tobias Schürg
maintainer=Tobias Schürg
sentence=Ready to use imeplementation for CO2 sensors of the MHZ series (Intelligent Infrared CO2 Module)
Expand Down

0 comments on commit 7f33a73

Please sign in to comment.