Browse Source

Added NTCSensor

rfm69
Xose Pérez 6 years ago
parent
commit
8351bfe5c6
7 changed files with 190 additions and 1 deletions
  1. +1
    -0
      code/espurna/config/arduino.h
  2. +3
    -0
      code/espurna/config/progmem.h
  3. +43
    -0
      code/espurna/config/sensors.h
  4. +1
    -0
      code/espurna/config/types.h
  5. +16
    -0
      code/espurna/sensor.ino
  6. +1
    -1
      code/espurna/sensors/AnalogSensor.h
  7. +125
    -0
      code/espurna/sensors/NTCSensor.h

+ 1
- 0
code/espurna/config/arduino.h View File

@ -142,6 +142,7 @@
//#define HCSR04_SUPPORT 1 //#define HCSR04_SUPPORT 1
//#define HLW8012_SUPPORT 1 //#define HLW8012_SUPPORT 1
//#define MHZ19_SUPPORT 1 //#define MHZ19_SUPPORT 1
//#define NTC_SUPPORT 1
//#define PMSX003_SUPPORT 1 //#define PMSX003_SUPPORT 1
//#define PZEM004T_SUPPORT 1 //#define PZEM004T_SUPPORT 1
//#define SHT3X_I2C_SUPPORT 1 //#define SHT3X_I2C_SUPPORT 1


+ 3
- 0
code/espurna/config/progmem.h View File

@ -175,6 +175,9 @@ PROGMEM const char espurna_sensors[] =
#if MHZ19_SUPPORT #if MHZ19_SUPPORT
"MHZ19 " "MHZ19 "
#endif #endif
#if NTC_SUPPORT
"NTC "
#endif
#if PMSX003_SUPPORT #if PMSX003_SUPPORT
"PMSX003 " "PMSX003 "
#endif #endif


+ 43
- 0
code/espurna/config/sensors.h View File

@ -430,6 +430,43 @@
#define MHZ19_TX_PIN 15 #define MHZ19_TX_PIN 15
#endif #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 // SenseAir CO2 sensor
// Enable support by passing SENSEAIR_SUPPORT=1 build flag // Enable support by passing SENSEAIR_SUPPORT=1 build flag
@ -589,6 +626,7 @@
HCSR04_SUPPORT || \ HCSR04_SUPPORT || \
HLW8012_SUPPORT || \ HLW8012_SUPPORT || \
MHZ19_SUPPORT || \ MHZ19_SUPPORT || \
NTC_SUPPORT || \
SENSEAIR_SUPPORT || \ SENSEAIR_SUPPORT || \
PMSX003_SUPPORT || \ PMSX003_SUPPORT || \
PZEM004T_SUPPORT || \ PZEM004T_SUPPORT || \
@ -719,6 +757,11 @@
#include "../sensors/MHZ19Sensor.h" #include "../sensors/MHZ19Sensor.h"
#endif #endif
#if NTC_SUPPORT
#include "../sensors/AnalogSensor.h"
#include "../sensors/NTCSensor.h"
#endif
#if SENSEAIR_SUPPORT #if SENSEAIR_SUPPORT
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#include "../sensors/SenseAirSensor.h" #include "../sensors/SenseAirSensor.h"


+ 1
- 0
code/espurna/config/types.h View File

@ -268,6 +268,7 @@
#define SENSOR_HCSR04_ID 0x23 #define SENSOR_HCSR04_ID 0x23
#define SENSOR_SENSEAIR_ID 0x24 #define SENSOR_SENSEAIR_ID 0x24
#define SENSOR_GEIGER_ID 0x25 #define SENSOR_GEIGER_ID 0x25
#define SENSOR_NTC_ID 0x26
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Magnitudes // Magnitudes


+ 16
- 0
code/espurna/sensor.ino View File

@ -324,6 +324,8 @@ void _sensorLoad() {
#if ANALOG_SUPPORT #if ANALOG_SUPPORT
{ {
AnalogSensor * sensor = new AnalogSensor(); AnalogSensor * sensor = new AnalogSensor();
sensor->setSamples(ANALOG_SAMPLES);
sensor->setDelay(ANALOG_DELAY);
_sensors.push_back(sensor); _sensors.push_back(sensor);
} }
#endif #endif
@ -500,6 +502,20 @@ void _sensorLoad() {
} }
#endif #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 #if SENSEAIR_SUPPORT
{ {
SenseAirSensor * sensor = new SenseAirSensor(); SenseAirSensor * sensor = new SenseAirSensor();


+ 1
- 1
code/espurna/sensors/AnalogSensor.h View File

@ -3,7 +3,7 @@
// Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com> // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if SENSOR_SUPPORT && ANALOG_SUPPORT
#if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT)
#pragma once #pragma once


+ 125
- 0
code/espurna/sensors/NTCSensor.h View File

@ -0,0 +1,125 @@
// -----------------------------------------------------------------------------
// NTC Sensor (maps to a NTCSensor)
// Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#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

Loading…
Cancel
Save