From 155a09dd595504ecf13cb4f1afaae782858620cb Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:08:43 -0600 Subject: [PATCH 01/12] Added T6613 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d4a319f20c..8e42373be2 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Since November 2018, Max Prokhorov (@mcspr) is also actively working as a collab * **AM2320** temperature and humidity sensor over I2C * **Dallas OneWire sensors** like the DS18B20 * **MHZ19** CO2 sensor + * **T6613** CO2 sensor * **MICS2710** CO2 & NO2 sensor * **MICS5525** CO & CO2 sensor * **SenseAir S8** CO2 sensor From a101472d43d38d6c270babef7e9358522852cd66 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:11:10 -0600 Subject: [PATCH 02/12] Added T6613 --- code/espurna/config/arduino.h | 1 + 1 file changed, 1 insertion(+) diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 85de053b43..cb60ac64ae 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -217,6 +217,7 @@ //#define SHT3X_I2C_SUPPORT 1 //#define SI7021_SUPPORT 1 //#define SONAR_SUPPORT 1 +//#define T6613_SUPPORT 1 //#define TMP3X_SUPPORT 1 //#define V9261F_SUPPORT 1 //#define VEML6075_SUPPORT 1 From af6e86adce771ff963514abe21b9c76d5a1c70a6 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:12:41 -0600 Subject: [PATCH 03/12] Added T6613 --- code/espurna/config/progmem.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/espurna/config/progmem.h b/code/espurna/config/progmem.h index 3a53b2aa60..41e9601ad9 100644 --- a/code/espurna/config/progmem.h +++ b/code/espurna/config/progmem.h @@ -264,6 +264,9 @@ PROGMEM const char espurna_sensors[] = #if SONAR_SUPPORT "SONAR " #endif + #if T6613_SUPPORT + "T6613 " + #endif #if TMP3X_SUPPORT "TMP3X " #endif From f151eab846c277604c21c6065c77c973bd831c58 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:15:16 -0600 Subject: [PATCH 04/12] Added T6613 --- code/espurna/config/sensors.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index ee18361650..3132f866a6 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -1052,6 +1052,23 @@ #define SONAR_ITERATIONS 5 // Number of iterations to ping for #endif // error correction. +//------------------------------------------------------------------------------ +// T6613 CO2 sensor +// Enable support by passing T6613_SUPPORT=1 build flag +//------------------------------------------------------------------------------ + +#ifndef T6613_SUPPORT +#define T6613_SUPPORT 0 +#endif + +#ifndef T6613_RX_PIN +#define T6613_RX_PIN 4 +#endif + +#ifndef T6613_TX_PIN +#define T6613_TX_PIN 5 +#endif + //------------------------------------------------------------------------------ // TMP3X analog temperature sensor // Enable support by passing TMP3X_SUPPORT=1 build flag @@ -1216,6 +1233,7 @@ SHT3X_I2C_SUPPORT || \ SI7021_SUPPORT || \ SONAR_SUPPORT || \ + T6613_SUPPORT || \ TMP3X_SUPPORT || \ V9261F_SUPPORT || \ VEML6075_SUPPORT || \ @@ -1403,6 +1421,10 @@ #include "../sensors/SonarSensor.h" #endif +#if T6613_SUPPORT + #include "../sensors/T6613Sensor.h" +#endif + #if TMP3X_SUPPORT #include "../sensors/TMP3XSensor.h" #endif From 398b27cedd3e84f4cb3fc4692d2a77bbead9ddd1 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:16:58 -0600 Subject: [PATCH 05/12] Added T6613 --- code/espurna/config/types.h | 1 + 1 file changed, 1 insertion(+) diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 7be27bafdf..8d4b0c694e 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -316,6 +316,7 @@ #define SENSOR_MAX6675_ID 35 #define SENSOR_LDR_ID 36 #define SENSOR_ADE7953_ID 37 +#define SENSOR_T6613_ID 38 //-------------------------------------------------------------------------------- // Magnitudes From 8bacd342aed0a970a89bbcb0f1129fed2c17f1b7 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:18:06 -0600 Subject: [PATCH 06/12] Added T6613 --- code/espurna/sensor.ino | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 42d87221b9..10da400d3f 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -1028,6 +1028,15 @@ void _sensorLoad() { _sensors.push_back(sensor); } #endif + + #if T6613_SUPPORT + { + T6613Sensor * sensor = new T6613Sensor(); + sensor->setRX(T6613_RX_PIN); + sensor->setTX(T6613_TX_PIN); + _sensors.push_back(sensor); + } + #endif #if TMP3X_SUPPORT { From a40cbe3a9a5be859820039c36f39f6245087589b Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:19:58 -0600 Subject: [PATCH 07/12] Added T6613 --- code/espurna/sensors/T6613Sensor.h | 186 +++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 code/espurna/sensors/T6613Sensor.h diff --git a/code/espurna/sensors/T6613Sensor.h b/code/espurna/sensors/T6613Sensor.h new file mode 100644 index 0000000000..30c551b2e5 --- /dev/null +++ b/code/espurna/sensors/T6613Sensor.h @@ -0,0 +1,186 @@ +// ----------------------------------------------------------------------------- +// T6613 CO2 sensor +// https://www.amphenol-sensors.com/en/telaire/co2/525-co2-sensor-modules/321-t6613 +// Uses SoftwareSerial library +// ----------------------------------------------------------------------------- + +#if SENSOR_SUPPORT && T6613_SUPPORT + +#pragma once + +#include "Arduino.h" +#include "BaseSensor.h" +#include + +#define T6613_REQUEST_LEN 5 +#define T6613_RESPONSE_LEN 5 +#define T6613_TIMEOUT 1000 +#define T6613_GETPPM 0x020203 + +class T6613Sensor : public BaseSensor { + + public: + + // --------------------------------------------------------------------- + // Public + // --------------------------------------------------------------------- + + T6613Sensor(): BaseSensor() { + _count = 1; + _sensor_id = SENSOR_T6613_ID; + } + + ~T6613Sensor() { + if (_serial) delete _serial; + } + + // --------------------------------------------------------------------- + + void setRX(unsigned char pin_rx) { + if (_pin_rx == pin_rx) return; + _pin_rx = pin_rx; + _dirty = true; + } + + void setTX(unsigned char pin_tx) { + if (_pin_tx == pin_tx) return; + _pin_tx = pin_tx; + _dirty = true; + } + + // --------------------------------------------------------------------- + + unsigned char getRX() { + return _pin_rx; + } + + unsigned char getTX() { + return _pin_tx; + } + + // --------------------------------------------------------------------- + // Sensor API + // --------------------------------------------------------------------- + + // Initialization method, must be idempotent + void begin() { + + if (!_dirty) return; + + if (_serial) delete _serial; + + _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 32); + _serial->enableIntTx(false); + _serial->begin(19200); + + _ready = true; + _dirty = false; + + } + + // Descriptive name of the sensor + String description() { + char buffer[28]; + snprintf(buffer, sizeof(buffer), "T6613 @ SwSerial(%u,%u)", _pin_rx, _pin_tx); + return String(buffer); + } + + // Descriptive name of the slot # index + String slot(unsigned char index) { + return description(); + }; + + // Address of the sensor (it could be the GPIO or I2C address) + String address(unsigned char index) { + char buffer[6]; + snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx); + return String(buffer); + } + + // Type for slot # index + unsigned char type(unsigned char index) { + if (index == 0) return MAGNITUDE_CO2; + return MAGNITUDE_NONE; + } + + void pre() { + _read(); + } + + // Current value for slot # index + double value(unsigned char index) { + if (index == 0) return _co2; + return 0; + } + + protected: + + // --------------------------------------------------------------------- + // Protected + // --------------------------------------------------------------------- + + void _write(unsigned char * command) { + _serial->write(command, T6613_REQUEST_LEN); + _serial->flush(); + } + + void _write(unsigned int command, unsigned char * response) { + + unsigned char buffer[T6613_REQUEST_LEN] = {0}; + buffer[0] = 0xFF; + buffer[1] = 0xFE; + buffer[2] = command >> 16; + buffer[3] = (command >> 8) & 0xFF; + buffer[4] = command & 0xFF; + _write(buffer); + + if (response != NULL) { + unsigned long start = millis(); + while (_serial->available() == 0) { + if (millis() - start > T6613_TIMEOUT) { + _error = SENSOR_ERROR_TIMEOUT; + return; + } + yield(); + } + _serial->readBytes(response, T6613_RESPONSE_LEN); + } + + } + + void _write(unsigned int command) { + _write(command, NULL); + } + + void _read() { + + unsigned char buffer[T6613_RESPONSE_LEN] = {0}; + _write(T6613_GETPPM, buffer); + + // Check response + if ((buffer[0] == 0xFF) + && (buffer[1] == 0xFA) + && (buffer[2] == 0x02)) { + + unsigned int value = buffer[3] * 256 + buffer[4]; + if (0 <= value && value <= 2000) { + _co2 = value; + _error = SENSOR_ERROR_OK; + } else { + _error = SENSOR_ERROR_OUT_OF_RANGE; + } + + } else { + _error = SENSOR_ERROR_CRC; + } + + } + + double _co2 = 0; + unsigned int _pin_rx; + unsigned int _pin_tx; + SoftwareSerial * _serial = NULL; + +}; + +#endif // SENSOR_SUPPORT && T6613_SUPPORT From 74d1a56b02042b02f4fc25cd1c0b4561e69c8e95 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:21:08 -0600 Subject: [PATCH 08/12] Added T6613 --- code/html/custom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/html/custom.js b/code/html/custom.js index 6dc611a63b..7003719103 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -51,7 +51,7 @@ function sensorName(id) { "HLW8012", "V9261F", "ECH1560", "Analog", "Digital", "Events", "PMSX003", "BMX280", "MHZ19", "SI7021", "SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD", - "TMP3X", "Sonar", "SenseAir", "GeigerTicks", "GeigerCPM", + "T6613", "TMP3X", "Sonar", "SenseAir", "GeigerTicks", "GeigerCPM", "NTC", "SDS011", "MICS2710", "MICS5525", "VL53L1X", "VEML6075", "EZOPH" ]; From 9f55767d59bff23ddf177d45824272035354e9e3 Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Tue, 22 Oct 2019 21:22:50 -0600 Subject: [PATCH 09/12] Added T6613 --- code/test/build/sensor.h | 1 + 1 file changed, 1 insertion(+) diff --git a/code/test/build/sensor.h b/code/test/build/sensor.h index 9ce95967da..9e6f65ecdb 100644 --- a/code/test/build/sensor.h +++ b/code/test/build/sensor.h @@ -13,6 +13,7 @@ #define MICS5525_SUPPORT 1 #define MAX6675_SUPPORT 1 #define DHT_SUPPORT 1 +#define T6613_SUPPORT 1 #define TMP3X_SUPPORT 1 #define EVENTS_SUPPORT 1 #define DIGITAL_SUPPORT 1 From c7780accbaac4e2dff747aef6a1a9c4a21603d9a Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Wed, 23 Oct 2019 17:15:45 -0600 Subject: [PATCH 10/12] Updated copyright string --- code/espurna/sensors/T6613Sensor.h | 1 + 1 file changed, 1 insertion(+) diff --git a/code/espurna/sensors/T6613Sensor.h b/code/espurna/sensors/T6613Sensor.h index 30c551b2e5..af92a05d3f 100644 --- a/code/espurna/sensors/T6613Sensor.h +++ b/code/espurna/sensors/T6613Sensor.h @@ -2,6 +2,7 @@ // T6613 CO2 sensor // https://www.amphenol-sensors.com/en/telaire/co2/525-co2-sensor-modules/321-t6613 // Uses SoftwareSerial library +// Copyright (C) 2017-2019 by Xose PĂ©rez // ----------------------------------------------------------------------------- #if SENSOR_SUPPORT && T6613_SUPPORT From 203566aef41d064c921b702e12ec98e24d179f1b Mon Sep 17 00:00:00 2001 From: James <13262021+james-coder@users.noreply.github.com> Date: Wed, 23 Oct 2019 17:21:34 -0600 Subject: [PATCH 11/12] Buffer from 6 bytes to 8 bytes Current gcc-4 does not mark this as warning, but gcc-9 notices the 3 char size requirement. Buffer should be 8 --- code/espurna/sensors/T6613Sensor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/espurna/sensors/T6613Sensor.h b/code/espurna/sensors/T6613Sensor.h index af92a05d3f..20588e78e4 100644 --- a/code/espurna/sensors/T6613Sensor.h +++ b/code/espurna/sensors/T6613Sensor.h @@ -93,7 +93,7 @@ class T6613Sensor : public BaseSensor { // Address of the sensor (it could be the GPIO or I2C address) String address(unsigned char index) { - char buffer[6]; + char buffer[8]; snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx); return String(buffer); } From e88bd8f8d77aa56a395807641b1ef1f0ff3462b5 Mon Sep 17 00:00:00 2001 From: James Alton Date: Fri, 25 Oct 2019 10:21:45 -0600 Subject: [PATCH 12/12] Use 5000ppm max --- code/espurna/sensors/T6613Sensor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/espurna/sensors/T6613Sensor.h b/code/espurna/sensors/T6613Sensor.h index 30c551b2e5..50c7e9709c 100644 --- a/code/espurna/sensors/T6613Sensor.h +++ b/code/espurna/sensors/T6613Sensor.h @@ -163,7 +163,7 @@ class T6613Sensor : public BaseSensor { && (buffer[2] == 0x02)) { unsigned int value = buffer[3] * 256 + buffer[4]; - if (0 <= value && value <= 2000) { + if (0 <= value && value <= 5000) { _co2 = value; _error = SENSOR_ERROR_OK; } else {