-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMHZ19.h
53 lines (41 loc) · 1.22 KB
/
MHZ19.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Library to work with Winsen MH-Z19B CO2 sensor via UART
* https://www.winsen-sensor.com/sensors/co2-sensor/mh-z19b.html
*
* Written by Sergey Trofimov
* https://github.com/tms320/MHZ19
*
* GNU General Public License v3.0
*/
#pragma once
#include <Arduino.h>
#include <SoftwareSerial.h>
class MHZ19
{
public:
MHZ19();
MHZ19(HardwareSerial &hwSerial); // use for hardware UART
MHZ19(uint8_t rxPin, uint8_t txPin, bool invert = false); // use for software UART
~MHZ19();
bool init(HardwareSerial &hwSerial); // use for hardware UART
bool init(uint8_t rxPin, uint8_t txPin, bool invert = false); // use for software UART
void close();
bool isReady(); // returns 'true' when preheat timed out
bool setRange2000();
bool setRange5000();
bool setRange10000();
bool enableSelfCalibration();
bool disableSelfCalibration();
bool calibrateZeroPoint();
bool calibrateSpanPoint(int span);
int getCO2(); // negative value is seconds left to preheat; 0 - error
private:
static const int64_t PREHEAT_TIME = 180000; // ms (3 minutes according datasheet)
bool _isReady;
HardwareSerial* _hwSerial;
SoftwareSerial* _swSerial;
Stream* _uart;
byte _response[9];
byte calcCRC(byte data[9]);
bool sendCmd(byte cmd[9]);
};