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.

189 lines
5.2 KiB

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