-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmh_z19.cpp
64 lines (58 loc) · 1.66 KB
/
mh_z19.cpp
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
54
55
56
57
58
59
60
61
62
63
64
#include <SoftwareSerial.h>
#include <Arduino.h>
#include "mh_z19.h"
#include "global.h"
//#define DEBUG
SoftwareSerial mhSerial(A3, A2); // A0 - к TX сенсора, A1 - к RX
void mh_setup() {
mhSerial.begin(9600);
byte icmd[9] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB};
mhSerial.write(icmd, 9);
memset(buf, 0, 9);
mhSerial.readBytes(buf, 9);
#ifdef DEBUG
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=buf[i];
crc = 255 - crc;
crc++;
if ( !(buf[0] == 0xFF && buf[1] == 0x99 && buf[8] == crc) ) {
Serial.println("MH: setRange CRC error: " + String(crc) + " / "+ String(buf[0]) + "," + String(buf[1]) + ",...," +String(buf[8]));
} else
{
Serial.println("MH: setRange OK");
}
#endif
}
unsigned int mh_getPPM()
{
byte get_cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
mhSerial.listen();
mhSerial.write(get_cmd, 9);
memset(buf, 0, 9);
mhSerial.readBytes(buf, 9);
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=buf[i];
crc = 255 - crc;
crc++;
if ( !(buf[0] == 0xFF && buf[1] == 0x86 && buf[8] == crc) ) {
#ifdef DEBUG
Serial.println("MH: CRC error: " + String(crc) + " / "+ String(buf[0]) + "," + String(buf[1]) + ",...," +String(buf[8]));
#endif
return 1;
} else {
unsigned int bufHigh = (unsigned int) buf[2];
unsigned int bufLow = (unsigned int) buf[3];
unsigned int ppm = (256*bufHigh) + bufLow;
#ifdef DEBUG
Serial.print(F("MH: "));Serial.println(ppm);
#endif
return ppm;
}
}
void mh_calibrate() {
byte cal_cmd[9] = {0xFF,0x01,0x87,0x00,0x00,0x00,0x00,0x00,0x78};
mhSerial.write(cal_cmd, 9);
mhSerial.listen();
}