Uart.h
Go to the documentation of this file.
1 /****
2  * Uart.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-MHZ19 Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <HardwareSerial.h>
23 #include <SimpleTimer.h>
24 #include "common.h"
25 
26 namespace MHZ19
27 {
33 enum Command {
39 };
40 
41 struct Request {
42  uint8_t start;
43  uint8_t sensor;
44  uint8_t command;
45  uint8_t data[5];
46  uint8_t checksum;
47 };
48 
49 struct Response {
50  uint8_t start;
51  uint8_t command;
52  uint8_t data[6];
53  uint8_t checksum;
54 };
55 
56 enum class Error {
57  success,
60  timeout,
61 };
62 
63 struct Measurement {
65  uint8_t status;
66  uint16_t co2_ppm;
67  int16_t temperature;
68 };
69 
71 
75 class Uart
76 {
77 public:
82  Uart(HardwareSerial& serial) : serial(serial)
83  {
84  }
85 
93  void setSensorNumber(uint8_t num)
94  {
95  sensorNumber = num;
96  }
97 
101  uint8_t getSensorNumber() const
102  {
103  return sensorNumber;
104  }
105 
113 
121 
128  void calibrateSpan(uint16_t ppm);
129 
134 
139 
140 protected:
141  void sendRequest(Request& request);
142 
143 private:
144  void onData();
145  void processResponse(Response& response);
146  void returnMeasurement(Measurement& m);
147 
148  HardwareSerial& serial;
149  uint8_t sensorNumber{0x01};
150  SimpleTimer timer;
151  MeasurementCallback callback;
152 };
153 
154 } // namespace MHZ19
155 
String toString(MHZ19::Error error)
Hardware serial class.
Definition: HardwareSerial.h:107
Access MHZ19 sensor via serial port.
Definition: Uart.h:76
void calibrateSpan(uint16_t ppm)
Calibrate span point.
void setDetectionRange(DetectionRange range)
MHZ-19B has configurable detection range.
void setAutoCalibration(bool enable)
Enable/disable zero-point auto-calibration feature. On by default.
void calibrateZero()
Calibrate zero point manually.
Uart(HardwareSerial &serial)
Use device in UART mode.
Definition: Uart.h:82
uint8_t getSensorNumber() const
Get currently configured sensor number.
Definition: Uart.h:101
void setSensorNumber(uint8_t num)
Change sensor number field.
Definition: Uart.h:93
bool getMeasurement(MeasurementCallback callback)
Read measurement from device.
void sendRequest(Request &request)
The String class.
Definition: WString.h:137
void enable(Handler &commandHandler, HardwareSerial &serial)
Definition: common.h:23
Command
Available commands.
Definition: Uart.h:33
@ CMD_SetDetectionRange
B.
Definition: Uart.h:38
@ CMD_CalibrateSpanPoint
-, B
Definition: Uart.h:36
@ CMD_CalibrateZeroPoint
-, B
Definition: Uart.h:35
@ CMD_GasConcentration
-, B, C
Definition: Uart.h:34
@ CMD_SelfCalbrationOnOff
B, C.
Definition: Uart.h:37
Error
Definition: Uart.h:56
DetectionRange
Device may be configured to output CO2 PPM values in various ranges.
Definition: common.h:27
Definition: Uart.h:63
uint8_t status
Definition: Uart.h:65
uint16_t co2_ppm
Definition: Uart.h:66
Error error
Definition: Uart.h:64
int16_t temperature
Definition: Uart.h:67
Definition: Uart.h:41
uint8_t data[5]
Definition: Uart.h:45
uint8_t start
Definition: Uart.h:42
uint8_t sensor
Definition: Uart.h:43
uint8_t command
Definition: Uart.h:44
uint8_t checksum
Definition: Uart.h:46
Definition: Uart.h:49
uint8_t data[6]
Definition: Uart.h:52
uint8_t command
Definition: Uart.h:51
uint8_t start
Definition: Uart.h:50
uint8_t checksum
Definition: Uart.h:53