From 8351bfe5c6138e9445ca93f938034943cbdddcb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 29 Jun 2018 21:35:56 +0200 Subject: [PATCH] Added NTCSensor --- code/espurna/config/arduino.h | 1 + code/espurna/config/progmem.h | 3 + code/espurna/config/sensors.h | 43 ++++++++++ code/espurna/config/types.h | 1 + code/espurna/sensor.ino | 16 ++++ code/espurna/sensors/AnalogSensor.h | 2 +- code/espurna/sensors/NTCSensor.h | 125 ++++++++++++++++++++++++++++ 7 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 code/espurna/sensors/NTCSensor.h diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index e5bf3e43..38848db5 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -142,6 +142,7 @@ //#define HCSR04_SUPPORT 1 //#define HLW8012_SUPPORT 1 //#define MHZ19_SUPPORT 1 +//#define NTC_SUPPORT 1 //#define PMSX003_SUPPORT 1 //#define PZEM004T_SUPPORT 1 //#define SHT3X_I2C_SUPPORT 1 diff --git a/code/espurna/config/progmem.h b/code/espurna/config/progmem.h index 8a23389f..d1e44c4f 100644 --- a/code/espurna/config/progmem.h +++ b/code/espurna/config/progmem.h @@ -175,6 +175,9 @@ PROGMEM const char espurna_sensors[] = #if MHZ19_SUPPORT "MHZ19 " #endif + #if NTC_SUPPORT + "NTC " + #endif #if PMSX003_SUPPORT "PMSX003 " #endif diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 2de7a48c..4b687370 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -430,6 +430,43 @@ #define MHZ19_TX_PIN 15 #endif +//------------------------------------------------------------------------------ +// NTC sensor +// Enable support by passing NTC_SUPPORT=1 build flag +//-------------------------------------------------------------------------------- + +#ifndef NTC_SUPPORT +#define NTC_SUPPORT 1 +#endif + +#ifndef NTC_SAMPLES +#define NTC_SAMPLES 10 // Number of samples +#endif + +#ifndef NTC_DELAY +#define NTC_DELAY 0 // Delay between samples in micros +#endif + +#ifndef NTC_R_UP +#define NTC_R_UP 0 // Resistor upstream, set to 0 if none +#endif + +#ifndef NTC_R_DOWN +#define NTC_R_DOWN 10000 // Resistor downstream, set to 0 if none +#endif + +#ifndef NTC_T0 +#define NTC_T0 298.15 // 25 Celsius +#endif + +#ifndef NTC_R0 +#define NTC_R0 10000 // Resistance at T0 +#endif + +#ifndef NTC_BETA +#define NTC_BETA 3977 // Beta coeficient +#endif + //------------------------------------------------------------------------------ // SenseAir CO2 sensor // Enable support by passing SENSEAIR_SUPPORT=1 build flag @@ -589,6 +626,7 @@ HCSR04_SUPPORT || \ HLW8012_SUPPORT || \ MHZ19_SUPPORT || \ + NTC_SUPPORT || \ SENSEAIR_SUPPORT || \ PMSX003_SUPPORT || \ PZEM004T_SUPPORT || \ @@ -719,6 +757,11 @@ #include "../sensors/MHZ19Sensor.h" #endif +#if NTC_SUPPORT + #include "../sensors/AnalogSensor.h" + #include "../sensors/NTCSensor.h" +#endif + #if SENSEAIR_SUPPORT #include #include "../sensors/SenseAirSensor.h" diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index b6e11d51..59666e60 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -268,6 +268,7 @@ #define SENSOR_HCSR04_ID 0x23 #define SENSOR_SENSEAIR_ID 0x24 #define SENSOR_GEIGER_ID 0x25 +#define SENSOR_NTC_ID 0x26 //-------------------------------------------------------------------------------- // Magnitudes diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index e20e89b4..06e40e7a 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -324,6 +324,8 @@ void _sensorLoad() { #if ANALOG_SUPPORT { AnalogSensor * sensor = new AnalogSensor(); + sensor->setSamples(ANALOG_SAMPLES); + sensor->setDelay(ANALOG_DELAY); _sensors.push_back(sensor); } #endif @@ -500,6 +502,20 @@ void _sensorLoad() { } #endif + #if NTC_SUPPORT + { + NTCSensor * sensor = new NTCSensor(); + sensor->setSamples(NTC_SAMPLES); + sensor->setDelay(NTC_DELAY); + sensor->setUpstreamResistor(NTC_R_UP); + sensor->setDownstreamResistor(NTC_R_DOWN); + sensor->setBeta(NTC_BETA); + sensor->setR0(NTC_R0); + sensor->setT0(NTC_T0); + _sensors.push_back(sensor); + } + #endif + #if SENSEAIR_SUPPORT { SenseAirSensor * sensor = new SenseAirSensor(); diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index ede70b46..e13d2cee 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -3,7 +3,7 @@ // Copyright (C) 2017-2018 by Xose Pérez // ----------------------------------------------------------------------------- -#if SENSOR_SUPPORT && ANALOG_SUPPORT +#if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT) #pragma once diff --git a/code/espurna/sensors/NTCSensor.h b/code/espurna/sensors/NTCSensor.h new file mode 100644 index 00000000..91426b3e --- /dev/null +++ b/code/espurna/sensors/NTCSensor.h @@ -0,0 +1,125 @@ +// ----------------------------------------------------------------------------- +// NTC Sensor (maps to a NTCSensor) +// Copyright (C) 2018 by Xose Pérez +// ----------------------------------------------------------------------------- + +#if SENSOR_SUPPORT && NTC_SUPPORT + +#pragma once + +// Set ADC to TOUT pin +#undef ADC_MODE_VALUE +#define ADC_MODE_VALUE ADC_TOUT + +#include "Arduino.h" +#include "AnalogSensor.h" + +extern "C" { + #include "../libs/fs_math.h" +} + +class NTCSensor : public AnalogSensor { + + public: + + // --------------------------------------------------------------------- + // Public + // --------------------------------------------------------------------- + + NTCSensor(): AnalogSensor() { + _count = 1; + _sensor_id = SENSOR_NTC_ID; + } + + void setBeta(unsigned long beta) { + if (beta > 0) _beta = beta; + } + + void setUpstreamResistor(unsigned long resistance) { + _resistance_up = resistance; + if (_resistance_up > 0) _resistance_down = 0; + } + + void setDownstreamResistor(unsigned long resistance) { + _resistance_down = resistance; + if (_resistance_down > 0) _resistance_up = 0; + } + + void setR0(unsigned long resistance) { + if (resistance > 0) _R0 = resistance; + } + + void setT0(double temperature) { + if (temperature > 0) _T0 = temperature; + } + + // --------------------------------------------------------------------- + + // --------------------------------------------------------------------- + // Sensor API + // --------------------------------------------------------------------- + + // Descriptive name of the sensor + String description() { + return String("NTC @ TOUT"); + } + + // 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) { + return String("0"); + } + + // Type for slot # index + unsigned char type(unsigned char index) { + if (index == 0) return MAGNITUDE_TEMPERATURE; + return MAGNITUDE_NONE; + } + + // Current value for slot # index + double value(unsigned char index) { + + double temperature = 0; + + if (index == 0) { + + // sampled reading + double read = _read(); + + // Ru = (1023/c - 1) * Rd + double resistance; + double alpha = (1023.0 / read) - 1; + if (_resistance_down > 0) { + resistance = _resistance_down * alpha; + } else if (0 == alpha) { + resistance = _R0; + } else { + resistance = _resistance_up / alpha; + } + + // 1/T = 1/T0 + 1/B * ln(R/R0) + temperature = fs_log(resistance / _R0); + temperature = (1.0 / _T0) + (temperature / _beta); + temperature = 1.0 / temperature - 273.15; + + } + + return temperature; + + } + + protected: + + unsigned long _beta = NTC_BETA; + unsigned long _resistance_up = NTC_R_UP; + unsigned long _resistance_down = NTC_R_DOWN; + unsigned long _R0 = NTC_R0; + double _T0 = NTC_T0; + +}; + +#endif // SENSOR_SUPPORT && NTC_SUPPORT