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.

166 lines
4.6 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 "BaseAnalogSensor.h"
  9. extern "C" {
  10. #include "../libs/fs_math.h"
  11. }
  12. class MICS2710Sensor : public BaseAnalogSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. MICS2710Sensor() {
  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. // ---------------------------------------------------------------------
  38. // Sensor API
  39. // ---------------------------------------------------------------------
  40. // Initialization method, must be idempotent
  41. void begin() {
  42. // Set NOX as analog input
  43. pinMode(_noxGPIO, INPUT);
  44. // Start pre-heating
  45. pinMode(_preGPIO, OUTPUT);
  46. digitalWrite(_preGPIO, HIGH);
  47. _heating = true;
  48. _start = millis();
  49. _ready = true;
  50. }
  51. // Pre-read hook (usually to populate registers with up-to-date data)
  52. void pre() {
  53. // Check pre-heat time
  54. if (_heating && (millis() - _start > MICS2710_PREHEAT_TIME)) {
  55. digitalWrite(_preGPIO, LOW);
  56. _heating = false;
  57. }
  58. if (_ready) {
  59. _Rs = _getResistance();
  60. }
  61. }
  62. // Descriptive name of the sensor
  63. String description() {
  64. return String("MICS-2710 @ TOUT");
  65. }
  66. // Descriptive name of the slot # index
  67. String slot(unsigned char index) {
  68. return description();
  69. };
  70. // Address of the sensor (it could be the GPIO or I2C address)
  71. String address(unsigned char index) {
  72. return String("0");
  73. }
  74. // Type for slot # index
  75. unsigned char type(unsigned char index) {
  76. if (0 == index) return MAGNITUDE_RESISTANCE;
  77. if (1 == index) return MAGNITUDE_NO2;
  78. return MAGNITUDE_NONE;
  79. }
  80. // Current value for slot # index
  81. double value(unsigned char index) {
  82. if (0 == index) return _Rs;
  83. if (1 == index) return _getPPM();
  84. return 0;
  85. }
  86. private:
  87. unsigned long _getReading() {
  88. return analogRead(_noxGPIO);
  89. }
  90. double _getResistance() {
  91. // get voltage (1 == reference) from analog pin
  92. double voltage = (float) _getReading() / 1024.0;
  93. // schematic: 3v3 - Rs - P - Rl - GND
  94. // V(P) = 3v3 * Rl / (Rs + Rl)
  95. // Rs = 3v3 * Rl / V(P) - Rl = Rl * ( 3v3 / V(P) - 1)
  96. // 3V3 voltage is cancelled
  97. double resistance = (voltage > 0) ? _Rl * ( 1 / voltage - 1 ) : 0;
  98. return resistance;
  99. }
  100. double _getPPM() {
  101. // According to the datasheet (https://www.cdiweb.com/datasheets/e2v/mics-2710.pdf)
  102. // there is an almost linear relation between log(Rs/R0) and log(ppm).
  103. // Regression parameters have been calculated based on the graph
  104. // in the datasheet with these readings:
  105. //
  106. // Rs/R0 NO2(ppm)
  107. // 23 0.20
  108. // 42 0.30
  109. // 90 0.40
  110. // 120 0.50
  111. // 200 0.60
  112. // 410 0.90
  113. // 500 1.00
  114. // 1000 1.30
  115. // 10000 5.00
  116. return fs_pow(10, 0.5170 * fs_log10(_Rs / _R0) - 1.3954);
  117. }
  118. bool _heating = false;
  119. unsigned long _start = 0; // monitors the pre-heating time
  120. unsigned char _noxGPIO = MICS2710_PRE_PIN;
  121. unsigned char _preGPIO = MICS2710_NOX_PIN;
  122. };
  123. #endif // SENSOR_SUPPORT && MICS2710_SUPPORT