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 {
34  CMD_GasConcentration = 0x86,
35  CMD_CalibrateZeroPoint = 0x87,
36  CMD_CalibrateSpanPoint = 0x88,
38  CMD_SetDetectionRange = 0x99,
39 };
40 
41 struct Request {
42  uint8_t start;
45  uint8_t data[5];
47 };
48 
49 struct Response {
52  uint8_t data[6];
54 };
55 
56 enum class Error {
57  success,
61 };
62 
63 struct Measurement {
64  Error error;
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 
112  void setAutoCalibration(bool enable);
113 
120  void calibrateZero();
121 
128  void calibrateSpan(uint16_t ppm);
129 
133  void setDetectionRange(DetectionRange range);
134 
138  bool getMeasurement(MeasurementCallback callback);
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 
int16_t temperature
Definition: Uart.h:84
void setAutoCalibration(bool enable)
Enable/disable zero-point auto-calibration feature. On by default.
void setSensorNumber(uint8_t num)
Change sensor number field.
Definition: Uart.h:110
Hardware serial class.
Definition: HardwareSerial.h:106
String toString(MHZ19::Error error)
bool getMeasurement(MeasurementCallback callback)
Read measurement from device.
uint8_t sensor
Definition: Uart.h:60
The String class.
Definition: WString.h:136
Definition: common.h:22
Error
Definition: Uart.h:73
@ CMD_GasConcentration
-, B, C
Definition: Uart.h:68
Definition: Uart.h:80
DetectionRange
Device may be configured to output CO2 PPM values in various ranges.
Definition: common.h:44
void calibrateSpan(uint16_t ppm)
Calibrate span point.
uint8_t status
Definition: Uart.h:82
void sendRequest(Request &request)
Delegate< void(Measurement &m)> MeasurementCallback
Definition: Uart.h:87
uint8_t data[6]
Definition: Uart.h:69
@ CMD_SelfCalbrationOnOff
B, C.
Definition: Uart.h:71
uint8_t data[5]
Definition: Uart.h:62
Uart(HardwareSerial &serial)
Use device in UART mode.
Definition: Uart.h:99
Error error
Definition: Uart.h:81
uint8_t getSensorNumber() const
Get currently configured sensor number.
Definition: Uart.h:118
Command
Available commands.
Definition: Uart.h:50
@ CMD_CalibrateZeroPoint
-, B
Definition: Uart.h:69
@ CMD_SetDetectionRange
B.
Definition: Uart.h:72
void calibrateZero()
Calibrate zero point manually.
uint8_t command
Definition: Uart.h:68
uint8_t command
Definition: Uart.h:61
uint8_t checksum
Definition: Uart.h:63
void enable(Handler &commandHandler, HardwareSerial &serial)
uint16_t co2_ppm
Definition: Uart.h:83
void setDetectionRange(DetectionRange range)
MHZ-19B has configurable detection range.
Access MHZ19 sensor via serial port.
Definition: Uart.h:92
uint8_t checksum
Definition: Uart.h:70
uint8_t start
Definition: Uart.h:59
@ CMD_CalibrateSpanPoint
-, B
Definition: Uart.h:70
uint8_t start
Definition: Uart.h:67