diff --git a/lib/SENSAIRS8/SENSAIRS8.cpp b/lib/SENSAIRS8/SENSAIRS8.cpp new file mode 100644 index 0000000..68e96bf --- /dev/null +++ b/lib/SENSAIRS8/SENSAIRS8.cpp @@ -0,0 +1,67 @@ +/* + .cpp - SENSAIRS8 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 +#include "SENSAIRS8.h" + +// Constructor - Inititalize Hardware UART +SENSAIRS8::SENSAIRS8(void) { + Serial.begin(9600); + Serial.setTimeout(READ_TIMEOUT); +} + +void SENSAIRS8::getSensorData(lora_data &loradata) { + uint8_t _cmd[7] = {0xFE, 0x44, 0x00, 0x08, 0x02, 0x9F, 0x25}; + while (Serial.available() > 0) Serial.read(); + Serial.write(_cmd, 7); + + delay(1000); + uint8_t readBytes = read(); + + if (readBytes > 0) { + loradata.ppm = (buffer[3]*256) + buffer[4]; + } +} + +// Read a Sensor Response +uint8_t SENSAIRS8::read() { + // Number of returned Bytes + uint8_t ret = 0; + // Clear Internal Buffer + zeroBuffer(); + + // Read Available Bytes + if (Serial.available() > 0) { + ret = Serial.readBytes(buffer, SER_BUF_LEN); + } + return ret; +} + +// Fill the Internal Buffer with Zeroes +void SENSAIRS8::zeroBuffer() { + for (int i=0; i < SER_BUF_LEN; i++) + buffer[i] = 0x00; +} \ No newline at end of file diff --git a/lib/SENSAIRS8/SENSAIRS8.h b/lib/SENSAIRS8/SENSAIRS8.h new file mode 100644 index 0000000..c0b1230 --- /dev/null +++ b/lib/SENSAIRS8/SENSAIRS8.h @@ -0,0 +1,52 @@ +/* + SENSAIRS8.h - SENSAIRS8 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 SENSAIRS8_H +#define SENSAIRS8_H + +// Data Structure for the LoRa Packet +struct lora_data { + uint8_t bat; + int16_t ppm; +} __attribute__ ((packed)); + +#define READ_TIMEOUT 500 // Timeout for Serial Communication +#define SER_BUF_LEN 7 // Length of the Internal Serial Message Buffer + +class SENSAIRS8 { + private: + uint8_t buffer[SER_BUF_LEN]; + + uint8_t read(); + void zeroBuffer(void); + uint16_t getPPM(void); + + public: + SENSAIRS8(void); + void getSensorData(lora_data &loradata); +}; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1f01c89..a7baec9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,9 @@ void blink(uint8_t num) { #elif defined HAS_SG112A #include SG112A sensor; +#elif defined HAS_SENSAIRS8 + #include + SENSAIRS8 sensor; #elif defined HAS_BME280 #include BME280 sensor; @@ -88,7 +91,7 @@ const lmic_pinmap lmic_pins = { }; // List of unused Pins - will be disabled for Power Saving -#if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C +#if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C || defined HAS_SENSAIRS8 const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0}; #else const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};