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.

121 lines
3.3 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 "BaseAnalogSensor.h"
  9. extern "C" {
  10. #include "../libs/fs_math.h"
  11. }
  12. class MICS5525Sensor : public BaseAnalogSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. MICS5525Sensor() {
  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. // ---------------------------------------------------------------------
  32. // Sensor API
  33. // ---------------------------------------------------------------------
  34. // Initialization method, must be idempotent
  35. void begin() {
  36. pinMode(_redGPIO, INPUT);
  37. _ready = true;
  38. }
  39. // Pre-read hook (usually to populate registers with up-to-date data)
  40. void pre() {
  41. _Rs = _getResistance();
  42. }
  43. // Descriptive name of the sensor
  44. String description() {
  45. return String("MICS-5525 @ TOUT");
  46. }
  47. // Descriptive name of the slot # index
  48. String slot(unsigned char index) {
  49. return description();
  50. };
  51. // Address of the sensor (it could be the GPIO or I2C address)
  52. String address(unsigned char index) {
  53. return String("0");
  54. }
  55. // Type for slot # index
  56. unsigned char type(unsigned char index) {
  57. if (0 == index) return MAGNITUDE_RESISTANCE;
  58. if (1 == index) return MAGNITUDE_CO;
  59. return MAGNITUDE_NONE;
  60. }
  61. // Current value for slot # index
  62. double value(unsigned char index) {
  63. if (0 == index) return _Rs;
  64. if (1 == index) return _getPPM();
  65. return 0;
  66. }
  67. private:
  68. unsigned long _getReading() {
  69. return analogRead(_redGPIO);
  70. }
  71. double _getResistance() {
  72. // get voltage (1 == reference) from analog pin
  73. double voltage = (float) _getReading() / 1024.0;
  74. // schematic: 3v3 - Rs - P - Rl - GND
  75. // V(P) = 3v3 * Rl / (Rs + Rl)
  76. // Rs = 3v3 * Rl / V(P) - Rl = Rl * ( 3v3 / V(P) - 1)
  77. // 3V3 voltage is cancelled
  78. double resistance = (voltage > 0) ? _Rl * ( 1 / voltage - 1 ) : 0;
  79. return resistance;
  80. }
  81. double _getPPM() {
  82. // According to the datasheet (https://airqualityegg.wikispaces.com/file/view/mics-5525-CO.pdf)
  83. return 764.2976 * fs_pow(2.71828, -7.6389 * ((float) _Rs / _R0));
  84. }
  85. unsigned char _redGPIO = MICS5525_RED_PIN;
  86. };
  87. #endif // SENSOR_SUPPORT && MICS5525_SUPPORT