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.

185 lines
5.1 KiB

  1. // -----------------------------------------------------------------------------
  2. // MICS-2710 (and MICS-4514) NO2 Analog Sensor
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && MICS2710_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include "BaseSensor.h"
  9. extern "C" {
  10. #include "../libs/fs_math.h"
  11. }
  12. class MICS2710Sensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. MICS2710Sensor(): BaseSensor() {
  18. _count = 2;
  19. _sensor_id = SENSOR_MICS2710_ID;
  20. }
  21. void calibrate() {
  22. setR0(_getResistance());
  23. }
  24. // ---------------------------------------------------------------------
  25. void setAnalogGPIO(unsigned char gpio) {
  26. _noxGPIO = gpio;
  27. }
  28. unsigned char getAnalogGPIO() {
  29. return _noxGPIO;
  30. }
  31. void setPreHeatGPIO(unsigned char gpio) {
  32. _preGPIO = gpio;
  33. }
  34. unsigned char getPreHeatGPIO() {
  35. return _preGPIO;
  36. }
  37. void setRL(unsigned long Rl) {
  38. if (Rl > 0) _Rl = Rl;
  39. }
  40. unsigned long getRL() {
  41. return _Rl;
  42. }
  43. void setR0(unsigned long R0) {
  44. if (R0 > 0) _R0 = R0;
  45. }
  46. unsigned long getR0() {
  47. return _R0;
  48. }
  49. // ---------------------------------------------------------------------
  50. // Sensor API
  51. // ---------------------------------------------------------------------
  52. // Initialization method, must be idempotent
  53. void begin() {
  54. // Set NOX as analog input
  55. pinMode(_noxGPIO, INPUT);
  56. // Start pre-heating
  57. pinMode(_preGPIO, OUTPUT);
  58. digitalWrite(_preGPIO, HIGH);
  59. _heating = true;
  60. _start = millis();
  61. _ready = true;
  62. }
  63. // Pre-read hook (usually to populate registers with up-to-date data)
  64. void pre() {
  65. // Check pre-heat time
  66. if (_heating && (millis() - _start > MICS2710_PREHEAT_TIME)) {
  67. digitalWrite(_preGPIO, LOW);
  68. _heating = false;
  69. }
  70. if (_ready) {
  71. _Rs = _getResistance();
  72. }
  73. }
  74. // Descriptive name of the sensor
  75. String description() {
  76. return String("MICS-2710 @ TOUT");
  77. }
  78. // Descriptive name of the slot # index
  79. String slot(unsigned char index) {
  80. return description();
  81. };
  82. // Address of the sensor (it could be the GPIO or I2C address)
  83. String address(unsigned char index) {
  84. return String("0");
  85. }
  86. // Type for slot # index
  87. unsigned char type(unsigned char index) {
  88. if (0 == index) return MAGNITUDE_RESISTANCE;
  89. if (1 == index) return MAGNITUDE_NO2;
  90. return MAGNITUDE_NONE;
  91. }
  92. // Current value for slot # index
  93. double value(unsigned char index) {
  94. if (0 == index) return _Rs;
  95. if (1 == index) return _getPPM();
  96. return 0;
  97. }
  98. private:
  99. unsigned long _getReading() {
  100. return analogRead(_noxGPIO);
  101. }
  102. double _getResistance() {
  103. // get voltage (1 == reference) from analog pin
  104. double voltage = (float) _getReading() / 1024.0;
  105. // schematic: 3v3 - Rs - P - Rl - GND
  106. // V(P) = 3v3 * Rl / (Rs + Rl)
  107. // Rs = 3v3 * Rl / V(P) - Rl = Rl * ( 3v3 / V(P) - 1)
  108. // 3V3 voltage is cancelled
  109. double resistance = (voltage > 0) ? _Rl * ( 1 / voltage - 1 ) : 0;
  110. return resistance;
  111. }
  112. double _getPPM() {
  113. // According to the datasheet (https://www.cdiweb.com/datasheets/e2v/mics-2710.pdf)
  114. // there is an almost linear relation between log(Rs/R0) and log(ppm).
  115. // Regression parameters have been calculated based on the graph
  116. // in the datasheet with these readings:
  117. //
  118. // Rs/R0 NO2(ppm)
  119. // 23 0.20
  120. // 42 0.30
  121. // 90 0.40
  122. // 120 0.50
  123. // 200 0.60
  124. // 410 0.90
  125. // 500 1.00
  126. // 1000 1.30
  127. // 10000 5.00
  128. return fs_pow(10, 0.5170 * fs_log10(_Rs / _R0) - 1.3954);
  129. }
  130. bool _heating = false;
  131. unsigned long _start = 0; // monitors the pre-heating time
  132. unsigned long _R0 = MICS2710_R0; // R0, calikbration value at 25º
  133. unsigned long _Rl = MICS2710_RL; // RL, load resistance
  134. unsigned long _Rs = 0; // cached resistance
  135. unsigned char _noxGPIO = MICS2710_PRE_PIN;
  136. unsigned char _preGPIO = MICS2710_NOX_PIN;
  137. };
  138. #endif // SENSOR_SUPPORT && MICS2710_SUPPORT