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.

144 lines
3.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // MICS-5525 (and MICS-4514) CO Analog Sensor
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && MICS5525_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 MICS5525Sensor : public BaseSensor {
  16. public:
  17. // ---------------------------------------------------------------------
  18. // Public
  19. // ---------------------------------------------------------------------
  20. MICS5525Sensor(): BaseSensor() {
  21. _count = 2;
  22. _sensor_id = SENSOR_MICS5525_ID;
  23. }
  24. void calibrate() {
  25. setR0(_getResistance());
  26. }
  27. // ---------------------------------------------------------------------
  28. void setAnalogGPIO(unsigned char gpio) {
  29. _redGPIO = gpio;
  30. }
  31. unsigned char getAnalogGPIO() {
  32. return _redGPIO;
  33. }
  34. void setRL(unsigned long Rl) {
  35. if (Rl > 0) _Rl = Rl;
  36. }
  37. unsigned long getRL() {
  38. return _Rl;
  39. }
  40. void setR0(unsigned long R0) {
  41. if (R0 > 0) _R0 = R0;
  42. }
  43. unsigned long getR0() {
  44. return _R0;
  45. }
  46. // ---------------------------------------------------------------------
  47. // Sensor API
  48. // ---------------------------------------------------------------------
  49. // Initialization method, must be idempotent
  50. void begin() {
  51. pinMode(_redGPIO, INPUT);
  52. _ready = true;
  53. }
  54. // Pre-read hook (usually to populate registers with up-to-date data)
  55. void pre() {
  56. _Rs = _getResistance();
  57. }
  58. // Descriptive name of the sensor
  59. String description() {
  60. return String("MICS-5525 @ TOUT");
  61. }
  62. // Descriptive name of the slot # index
  63. String slot(unsigned char index) {
  64. return description();
  65. };
  66. // Address of the sensor (it could be the GPIO or I2C address)
  67. String address(unsigned char index) {
  68. return String("0");
  69. }
  70. // Type for slot # index
  71. unsigned char type(unsigned char index) {
  72. if (0 == index) return MAGNITUDE_RESISTANCE;
  73. if (1 == index) return MAGNITUDE_CO;
  74. return MAGNITUDE_NONE;
  75. }
  76. // Current value for slot # index
  77. double value(unsigned char index) {
  78. if (0 == index) return _Rs;
  79. if (1 == index) return _getPPM();
  80. return 0;
  81. }
  82. private:
  83. unsigned long _getReading() {
  84. return analogRead(_redGPIO);
  85. }
  86. double _getResistance() {
  87. // get voltage (1 == reference) from analog pin
  88. double voltage = (float) _getReading() / 1024.0;
  89. // schematic: 3v3 - Rs - P - Rl - GND
  90. // V(P) = 3v3 * Rl / (Rs + Rl)
  91. // Rs = 3v3 * Rl / V(P) - Rl = Rl * ( 3v3 / V(P) - 1)
  92. // 3V3 voltage is cancelled
  93. double resistance = (voltage > 0) ? _Rl * ( 1 / voltage - 1 ) : 0;
  94. return resistance;
  95. }
  96. double _getPPM() {
  97. // According to the datasheet (https://airqualityegg.wikispaces.com/file/view/mics-5525-CO.pdf)
  98. return 764.2976 * fs_pow(2.71828, -7.6389 * ((float) _Rs / _R0));
  99. }
  100. unsigned long _R0 = MICS5525_R0; // R0, calibration value at 25º on air
  101. unsigned long _Rl = MICS5525_RL; // RL, load resistance
  102. unsigned long _Rs = 0; // cached resistance
  103. unsigned char _redGPIO = MICS5525_RED_PIN;
  104. };
  105. #endif // SENSOR_SUPPORT && MICS5525_SUPPORT