Compare commits
2 commits
493a466a90
...
12d2ffdbfb
Author | SHA1 | Date | |
---|---|---|---|
12d2ffdbfb | |||
8578801edf |
6 changed files with 242 additions and 11 deletions
103
lib/MHZ19C/MHZ19C.cpp
Normal file
103
lib/MHZ19C/MHZ19C.cpp
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
/*
|
||||||
|
MHZ19C.cpp - MHZ19C Sensor Library
|
||||||
|
Copyright (c) 2020-2021, Stefan Brand
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "MHZ19C.h"
|
||||||
|
|
||||||
|
// Constructor - Inititalize Hardware UART
|
||||||
|
MHZ19C::MHZ19C(void) {
|
||||||
|
Serial.begin(9600);
|
||||||
|
Serial.setTimeout(MHZ19C_READ_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MHZ19C::getSensorData(lora_data &loradata) {
|
||||||
|
write(MHZ19C_CMD_GET_PPM, 0x00);
|
||||||
|
delay(50);
|
||||||
|
uint8_t readBytes = read();
|
||||||
|
|
||||||
|
loradata.ppm = 0;
|
||||||
|
if (readBytes > 0) {
|
||||||
|
switch(buffer[1]) {
|
||||||
|
case 0x86:
|
||||||
|
loradata.ppm = (buffer[2]*256) + buffer[3];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn Self Calibration Routine On or Off
|
||||||
|
void MHZ19C::setSelfCalibration(bool state) {
|
||||||
|
if (state) {
|
||||||
|
write(0x79, 0xA0);
|
||||||
|
} else {
|
||||||
|
write(0x79, 0x00);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write a Command to the Sensor
|
||||||
|
void MHZ19C::write(uint8_t cmd, uint8_t arg) {
|
||||||
|
uint8_t _cmd[9] = {0xFF, 0x01, cmd, arg, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||||
|
uint8_t crc = crc8(_cmd);
|
||||||
|
_cmd[8] = crc;
|
||||||
|
while (Serial.available() > 0) Serial.read();
|
||||||
|
Serial.write(_cmd, 9);
|
||||||
|
Serial.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a Sensor Response
|
||||||
|
uint8_t MHZ19C::read() {
|
||||||
|
uint8_t ret = 0;
|
||||||
|
zeroBuffer();
|
||||||
|
|
||||||
|
// Read Available Bytes
|
||||||
|
if (Serial.available() > 0) {
|
||||||
|
ret = Serial.readBytes(buffer, MHZ19C_SER_BUF_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check Sync Bit and CRC
|
||||||
|
if (buffer[0] != 0xFF || buffer [8] != crc8(buffer))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Return Read Bytes
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the Internal Buffer with Zeroes
|
||||||
|
void MHZ19C::zeroBuffer() {
|
||||||
|
for (int i=0; i < MHZ19C_SER_BUF_LEN; i++)
|
||||||
|
buffer[i] = 0x00;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate 8Bit CRC of Messages and Commands
|
||||||
|
uint8_t MHZ19C::crc8(uint8_t *paket){
|
||||||
|
uint8_t i, checksum = 0x00;
|
||||||
|
for( i = 1; i < 8; i++)
|
||||||
|
checksum += paket[i];
|
||||||
|
|
||||||
|
checksum = 0xff - checksum;
|
||||||
|
checksum += 1;
|
||||||
|
return checksum;
|
||||||
|
}
|
58
lib/MHZ19C/MHZ19C.h
Normal file
58
lib/MHZ19C/MHZ19C.h
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
MHZ19C.h - MHZ19C Sensor Library
|
||||||
|
Copyright (c) 2020-2021, Stefan Brand
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MHZ19C_H
|
||||||
|
#define MHZ19C_H
|
||||||
|
|
||||||
|
// Data Structure for the LoRa Packet
|
||||||
|
struct lora_data {
|
||||||
|
uint8_t bat;
|
||||||
|
int16_t ppm;
|
||||||
|
} __attribute__ ((packed));
|
||||||
|
|
||||||
|
#define MHZ19C_READ_TIMEOUT 500 // Timeout for Serial Communication
|
||||||
|
#define MHZ19C_SER_BUF_LEN 9 // Length of the Internal Serial Message Buffer
|
||||||
|
|
||||||
|
#define MHZ19C_CMD_SET_AUTOCAL 0x79 // Turn Self Calibration on/off
|
||||||
|
#define MHZ19C_CMD_GET_PPM 0x86 // Get Current PPM Reading
|
||||||
|
|
||||||
|
class MHZ19C {
|
||||||
|
private:
|
||||||
|
uint8_t buffer[MHZ19C_SER_BUF_LEN];
|
||||||
|
|
||||||
|
void write(byte cmd, byte arg);
|
||||||
|
uint8_t read();
|
||||||
|
void zeroBuffer(void);
|
||||||
|
uint8_t crc8(uint8_t *paket);
|
||||||
|
uint16_t getPPM(void);
|
||||||
|
|
||||||
|
public:
|
||||||
|
MHZ19C(void);
|
||||||
|
void getSensorData(lora_data &loradata);
|
||||||
|
void setSelfCalibration(bool state);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,6 +1,33 @@
|
||||||
|
/*
|
||||||
|
SG112A.cpp - SG112A Sensor Library
|
||||||
|
Copyright (c) 2020-2021, Stefan Brand
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "SG112A.h"
|
#include "SG112A.h"
|
||||||
|
|
||||||
|
// Constructor - Inititalize Hardware UART
|
||||||
SG112A::SG112A(void) {
|
SG112A::SG112A(void) {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.setTimeout(READ_TIMEOUT);
|
Serial.setTimeout(READ_TIMEOUT);
|
||||||
|
@ -20,6 +47,7 @@ void SG112A::getSensorData(lora_data &loradata) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write a Command to the Sensor
|
||||||
void SG112A::write(byte cmd) {
|
void SG112A::write(byte cmd) {
|
||||||
uint8_t _cmd[6] = {0xAA, 0x55, cmd, 0x00, 0x00, 0x00};
|
uint8_t _cmd[6] = {0xAA, 0x55, cmd, 0x00, 0x00, 0x00};
|
||||||
uint16_t crc = crc16(_cmd, 4);
|
uint16_t crc = crc16(_cmd, 4);
|
||||||
|
@ -30,26 +58,37 @@ void SG112A::write(byte cmd) {
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read a Sensor Response
|
||||||
uint8_t SG112A::read() {
|
uint8_t SG112A::read() {
|
||||||
|
// Number of returned Bytes
|
||||||
uint8_t ret = 0;
|
uint8_t ret = 0;
|
||||||
|
// Clear Internal Buffer
|
||||||
zeroBuffer();
|
zeroBuffer();
|
||||||
|
|
||||||
|
// Read Available Bytes
|
||||||
if (Serial.available() > 0) {
|
if (Serial.available() > 0) {
|
||||||
ret = Serial.readBytes(buffer, SER_BUF_LEN);
|
ret = Serial.readBytes(buffer, SER_BUF_LEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer[0] != 0xBB && buffer[1] != 0x66)
|
// Check Sync Bytes
|
||||||
ret = 0;
|
if (buffer[0] != 0xBB || buffer[1] != 0x66)
|
||||||
|
return 0;
|
||||||
// TODO: Do CRC Check Here
|
|
||||||
|
// Check CRC of the Returned Messages
|
||||||
|
uint16_t crc = crc16(buffer, ret-2);
|
||||||
|
if (buffer[ret-1] != (uint8_t)(crc >> 8) || buffer[ret-2] != (uint8_t)(crc & 0xFF))
|
||||||
|
return 0;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fill the Internal Buffer with Zeroes
|
||||||
void SG112A::zeroBuffer() {
|
void SG112A::zeroBuffer() {
|
||||||
for (int i=0; i < SER_BUF_LEN; i++)
|
for (int i=0; i < SER_BUF_LEN; i++)
|
||||||
buffer[i] = 0x00;
|
buffer[i] = 0x00;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate 16Bit CRC of Messages and Commands
|
||||||
uint16_t SG112A::crc16(uint8_t *cmd, int len){
|
uint16_t SG112A::crc16(uint8_t *cmd, int len){
|
||||||
uint16_t ret = 0xffff;
|
uint16_t ret = 0xffff;
|
||||||
uint16_t polynomial = 0xa001;
|
uint16_t polynomial = 0xa001;
|
||||||
|
@ -71,4 +110,4 @@ uint16_t SG112A::crc16(uint8_t *cmd, int len){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
|
@ -1,17 +1,44 @@
|
||||||
|
/*
|
||||||
|
SG112A.h - SG112A Sensor Library
|
||||||
|
Copyright (c) 2020-2021, Stefan Brand
|
||||||
|
All rights reserved.
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef SG112A_H
|
#ifndef SG112A_H
|
||||||
#define SG112A_H
|
#define SG112A_H
|
||||||
|
|
||||||
|
// Data Structure for the LoRa Packet
|
||||||
struct lora_data {
|
struct lora_data {
|
||||||
uint8_t bat;
|
uint8_t bat;
|
||||||
int16_t ppm;
|
int16_t ppm;
|
||||||
} __attribute__ ((packed));
|
} __attribute__ ((packed));
|
||||||
|
|
||||||
#define READ_TIMEOUT 500
|
#define READ_TIMEOUT 500 // Timeout for Serial Communication
|
||||||
#define SER_BUF_LEN 16
|
#define SER_BUF_LEN 16 // Length of the Internal Serial Message Buffer
|
||||||
|
|
||||||
#define CMD_GET_VER 0x10
|
#define CMD_GET_VER 0x10 // Get Sensor Version
|
||||||
#define CMD_GET_SER 0x12
|
#define CMD_GET_SER 0x12 // Get Sensor Serial
|
||||||
#define CMD_GET_PPM 0x14
|
#define CMD_GET_PPM 0x14 // Get Current PPM Reading
|
||||||
|
|
||||||
class SG112A {
|
class SG112A {
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#define HAS_BME280
|
#define HAS_BME280
|
||||||
// #define HAS_SHT21
|
// #define HAS_SHT21
|
||||||
// #define HAS_SG112A
|
// #define HAS_SG112A
|
||||||
|
// #define HAS_MHZ19C
|
||||||
// #define HAS_NO_SENSOR
|
// #define HAS_NO_SENSOR
|
||||||
|
|
||||||
// How many minutes to sleep between Measuring/Sending
|
// How many minutes to sleep between Measuring/Sending
|
||||||
|
|
|
@ -35,6 +35,9 @@ void blink(uint8_t num) {
|
||||||
struct lora_data {
|
struct lora_data {
|
||||||
uint8_t bat;
|
uint8_t bat;
|
||||||
} __attribute ((packed));
|
} __attribute ((packed));
|
||||||
|
#elif defined HAS_MHZ19C
|
||||||
|
#include <MHZ19C.h>
|
||||||
|
MHZ19C sensor;
|
||||||
#elif defined HAS_SG112A
|
#elif defined HAS_SG112A
|
||||||
#include <SG112A.h>
|
#include <SG112A.h>
|
||||||
SG112A sensor;
|
SG112A sensor;
|
||||||
|
@ -71,7 +74,7 @@ const lmic_pinmap lmic_pins = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// List of unused Pins - will be disabled for Power Saving
|
// List of unused Pins - will be disabled for Power Saving
|
||||||
#if defined DEBUG || defined HAS_SG112A
|
#if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C
|
||||||
const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};
|
const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};
|
||||||
#else
|
#else
|
||||||
const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};
|
const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};
|
||||||
|
|
Loading…
Reference in a new issue