Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telaire T6613 Support #1956

Merged
merged 13 commits into from
Nov 6, 2019
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
james-coder marked this conversation as resolved.
Show resolved Hide resolved
* **MICS2710** CO2 & NO2 sensor
* **MICS5525** CO & CO2 sensor
* **SenseAir S8** CO2 sensor
Expand Down
1 change: 1 addition & 0 deletions code/espurna/config/arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/config/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ PROGMEM const char espurna_sensors[] =
#if SONAR_SUPPORT
"SONAR "
#endif
#if T6613_SUPPORT
"T6613 "
#endif
#if TMP3X_SUPPORT
"TMP3X "
#endif
Expand Down
22 changes: 22 additions & 0 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1216,6 +1233,7 @@
SHT3X_I2C_SUPPORT || \
SI7021_SUPPORT || \
SONAR_SUPPORT || \
T6613_SUPPORT || \
TMP3X_SUPPORT || \
V9261F_SUPPORT || \
VEML6075_SUPPORT || \
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
187 changes: 187 additions & 0 deletions code/espurna/sensors/T6613Sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// -----------------------------------------------------------------------------
// 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 <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
james-coder marked this conversation as resolved.
Show resolved Hide resolved

#if SENSOR_SUPPORT && T6613_SUPPORT

#pragma once

#include "Arduino.h"
james-coder marked this conversation as resolved.
Show resolved Hide resolved
#include "BaseSensor.h"
#include <SoftwareSerial.h>

#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);
james-coder marked this conversation as resolved.
Show resolved Hide resolved

_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[8];
snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
james-coder marked this conversation as resolved.
Show resolved Hide resolved
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 <= 5000) {
_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
2 changes: 1 addition & 1 deletion code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
];
Expand Down
1 change: 1 addition & 0 deletions code/test/build/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down