Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. // -----------------------------------------------------------------------------
  2. // NTC Sensor (maps to a NTCSensor)
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && NTC_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include "AnalogSensor.h"
  9. extern "C" {
  10. #include "../libs/fs_math.h"
  11. }
  12. class NTCSensor : public AnalogSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. NTCSensor() {
  18. _count = 1;
  19. _sensor_id = SENSOR_NTC_ID;
  20. }
  21. void setBeta(unsigned long beta) {
  22. if (beta > 0) _beta = beta;
  23. }
  24. void setUpstreamResistor(unsigned long resistance) {
  25. _resistance_up = resistance;
  26. if (_resistance_up > 0) _resistance_down = 0;
  27. }
  28. void setDownstreamResistor(unsigned long resistance) {
  29. _resistance_down = resistance;
  30. if (_resistance_down > 0) _resistance_up = 0;
  31. }
  32. void setR0(unsigned long resistance) {
  33. if (resistance > 0) _R0 = resistance;
  34. }
  35. void setT0(double temperature) {
  36. if (temperature > 0) _T0 = temperature;
  37. }
  38. // ---------------------------------------------------------------------
  39. // ---------------------------------------------------------------------
  40. // Sensor API
  41. // ---------------------------------------------------------------------
  42. // Descriptive name of the sensor
  43. String description() {
  44. return String("NTC @ TOUT");
  45. }
  46. // Descriptive name of the slot # index
  47. String slot(unsigned char index) {
  48. return description();
  49. }
  50. // Address of the sensor (it could be the GPIO or I2C address)
  51. String address(unsigned char index) {
  52. return String("0");
  53. }
  54. // Type for slot # index
  55. unsigned char type(unsigned char index) {
  56. if (index == 0) return MAGNITUDE_TEMPERATURE;
  57. return MAGNITUDE_NONE;
  58. }
  59. // Current value for slot # index
  60. double value(unsigned char index) {
  61. double temperature = 0;
  62. if (index == 0) {
  63. // sampled reading
  64. double read = _read();
  65. // Ru = (1023/c - 1) * Rd
  66. double resistance;
  67. double alpha = (1023.0 / read) - 1;
  68. if (_resistance_down > 0) {
  69. resistance = _resistance_down * alpha;
  70. } else if (0 == alpha) {
  71. resistance = _R0;
  72. } else {
  73. resistance = _resistance_up / alpha;
  74. }
  75. // 1/T = 1/T0 + 1/B * ln(R/R0)
  76. temperature = fs_log(resistance / _R0);
  77. temperature = (1.0 / _T0) + (temperature / _beta);
  78. temperature = 1.0 / temperature - 273.15;
  79. }
  80. return temperature;
  81. }
  82. protected:
  83. unsigned long _beta = NTC_BETA;
  84. unsigned long _resistance_up = NTC_R_UP;
  85. unsigned long _resistance_down = NTC_R_DOWN;
  86. unsigned long _R0 = NTC_R0;
  87. double _T0 = NTC_T0;
  88. };
  89. #endif // SENSOR_SUPPORT && NTC_SUPPORT