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.

162 lines
4.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // MHZ19 CO2 sensor
  3. // Based on: https://github.com/nara256/mhz19_uart
  4. // http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf
  5. // -----------------------------------------------------------------------------
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <SoftwareSerial.h>
  10. #define MHZ19_REQUEST_LEN 8
  11. #define MHZ19_RESPONSE_LEN 9
  12. #define MHZ19_TIMEOUT 1000
  13. #define MHZ19_GETPPM 0x8600
  14. #define MHZ19_ZEROCALIB 0x8700
  15. #define MHZ19_SPANCALIB 0x8800
  16. #define MHZ19_AUTOCALIB_ON 0x79A0
  17. #define MHZ19_AUTOCALIB_OFF 0x7900
  18. class MHZ19Sensor : public BaseSensor {
  19. public:
  20. MHZ19Sensor(int pin_rx = MHZ19_RX_PIN, int pin_tx = MHZ19_TX_PIN): BaseSensor() {
  21. // Cache
  22. _pin_rx = pin_rx;
  23. _pin_tx = pin_tx;
  24. _count = 1;
  25. // Init
  26. _serial = new SoftwareSerial(pin_rx, pin_tx, false, 256);
  27. _serial->begin(9600);
  28. calibrateAuto(false);
  29. }
  30. // Descriptive name of the sensor
  31. String name() {
  32. char buffer[28];
  33. snprintf(buffer, sizeof(buffer), "MHZ19 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  34. return String(buffer);
  35. }
  36. // Descriptive name of the slot # index
  37. String slot(unsigned char index) {
  38. return name();
  39. }
  40. // Type for slot # index
  41. magnitude_t type(unsigned char index) {
  42. _error = SENSOR_ERROR_OK;
  43. if (index == 0) return MAGNITUDE_CO2;
  44. _error = SENSOR_ERROR_OUT_OF_RANGE;
  45. return MAGNITUDE_NONE;
  46. }
  47. void pre() {
  48. _read();
  49. }
  50. // Current value for slot # index
  51. double value(unsigned char index) {
  52. _error = SENSOR_ERROR_OK;
  53. if (index == 0) return _co2;
  54. _error = SENSOR_ERROR_OUT_OF_RANGE;
  55. return 0;
  56. }
  57. void calibrateAuto(boolean state){
  58. _write(state ? MHZ19_AUTOCALIB_ON : MHZ19_AUTOCALIB_OFF);
  59. }
  60. void calibrateZero() {
  61. _write(MHZ19_ZEROCALIB);
  62. }
  63. void calibrateSpan(unsigned int ppm) {
  64. if( ppm < 1000 ) return;
  65. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  66. buffer[0] = 0xFF;
  67. buffer[1] = 0x01;
  68. buffer[2] = MHZ19_SPANCALIB >> 8;
  69. buffer[3] = ppm >> 8;
  70. buffer[4] = ppm & 0xFF;
  71. _write(buffer);
  72. }
  73. protected:
  74. void _write(unsigned char * command) {
  75. _serial->write(command, MHZ19_REQUEST_LEN);
  76. _serial->write(_checksum(command));
  77. _serial->flush();
  78. }
  79. void _write(unsigned int command, unsigned char * response) {
  80. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  81. buffer[0] = 0xFF;
  82. buffer[1] = 0x01;
  83. buffer[2] = command >> 8;
  84. buffer[3] = command & 0xFF;
  85. _write(buffer);
  86. if (response != NULL) {
  87. unsigned long start = millis();
  88. while (_serial->available() == 0) {
  89. if (millis() - start > MHZ19_TIMEOUT) {
  90. _error = SENSOR_ERROR_TIMEOUT;
  91. return;
  92. }
  93. yield();
  94. }
  95. _serial->readBytes(response, MHZ19_RESPONSE_LEN);
  96. }
  97. }
  98. void _write(unsigned int command) {
  99. _write(command, NULL);
  100. }
  101. void _read() {
  102. unsigned char buffer[MHZ19_RESPONSE_LEN] = {0};
  103. _write(MHZ19_GETPPM, buffer);
  104. // Check response
  105. if ((buffer[0] == 0xFF)
  106. && (buffer[1] == 0x86)
  107. && (_checksum(buffer) == buffer[MHZ19_RESPONSE_LEN-1])) {
  108. unsigned int value = buffer[2] * 256 + buffer[3];
  109. if (0 <= value && value <= 5000) {
  110. _co2 = value;
  111. _error = SENSOR_ERROR_OK;
  112. } else {
  113. _error = SENSOR_ERROR_OUT_OF_RANGE;
  114. }
  115. }
  116. }
  117. uint8_t _checksum(uint8_t * command) {
  118. uint8_t sum = 0x00;
  119. for (unsigned char i = 1; i < MHZ19_REQUEST_LEN-1; i++) {
  120. sum += command[i];
  121. }
  122. sum = 0xFF - sum + 0x01;
  123. return sum;
  124. }
  125. double _co2 = 0;
  126. unsigned int _pin_rx;
  127. unsigned int _pin_tx;
  128. SoftwareSerial * _serial;
  129. };