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.

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