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.

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