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.

182 lines
5.3 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. // ---------------------------------------------------------------------
  23. // Public
  24. // ---------------------------------------------------------------------
  25. MHZ19Sensor(): BaseSensor() {
  26. _count = 1;
  27. }
  28. void setGPIO(unsigned char pin_rx, unsigned char pin_tx) {
  29. _pin_rx = pin_rx;
  30. _pin_tx = pin_tx;
  31. }
  32. // ---------------------------------------------------------------------
  33. // Sensor API
  34. // ---------------------------------------------------------------------
  35. // Initialization method, must be idempotent
  36. void begin() {
  37. if (_serial) delete _serial;
  38. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 256);
  39. _serial->begin(9600);
  40. calibrateAuto(false);
  41. }
  42. // Descriptive name of the sensor
  43. String name() {
  44. char buffer[28];
  45. snprintf(buffer, sizeof(buffer), "MHZ19 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  46. return String(buffer);
  47. }
  48. // Descriptive name of the slot # index
  49. String slot(unsigned char index) {
  50. return name();
  51. }
  52. // Type for slot # index
  53. magnitude_t type(unsigned char index) {
  54. _error = SENSOR_ERROR_OK;
  55. if (index == 0) return MAGNITUDE_CO2;
  56. _error = SENSOR_ERROR_OUT_OF_RANGE;
  57. return MAGNITUDE_NONE;
  58. }
  59. void pre() {
  60. _read();
  61. }
  62. // Current value for slot # index
  63. double value(unsigned char index) {
  64. _error = SENSOR_ERROR_OK;
  65. if (index == 0) return _co2;
  66. _error = SENSOR_ERROR_OUT_OF_RANGE;
  67. return 0;
  68. }
  69. void calibrateAuto(boolean state){
  70. _write(state ? MHZ19_AUTOCALIB_ON : MHZ19_AUTOCALIB_OFF);
  71. }
  72. void calibrateZero() {
  73. _write(MHZ19_ZEROCALIB);
  74. }
  75. void calibrateSpan(unsigned int ppm) {
  76. if( ppm < 1000 ) return;
  77. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  78. buffer[0] = 0xFF;
  79. buffer[1] = 0x01;
  80. buffer[2] = MHZ19_SPANCALIB >> 8;
  81. buffer[3] = ppm >> 8;
  82. buffer[4] = ppm & 0xFF;
  83. _write(buffer);
  84. }
  85. protected:
  86. // ---------------------------------------------------------------------
  87. // Protected
  88. // ---------------------------------------------------------------------
  89. void _write(unsigned char * command) {
  90. _serial->write(command, MHZ19_REQUEST_LEN);
  91. _serial->write(_checksum(command));
  92. _serial->flush();
  93. }
  94. void _write(unsigned int command, unsigned char * response) {
  95. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  96. buffer[0] = 0xFF;
  97. buffer[1] = 0x01;
  98. buffer[2] = command >> 8;
  99. buffer[3] = command & 0xFF;
  100. _write(buffer);
  101. if (response != NULL) {
  102. unsigned long start = millis();
  103. while (_serial->available() == 0) {
  104. if (millis() - start > MHZ19_TIMEOUT) {
  105. _error = SENSOR_ERROR_TIMEOUT;
  106. return;
  107. }
  108. yield();
  109. }
  110. _serial->readBytes(response, MHZ19_RESPONSE_LEN);
  111. }
  112. }
  113. void _write(unsigned int command) {
  114. _write(command, NULL);
  115. }
  116. void _read() {
  117. unsigned char buffer[MHZ19_RESPONSE_LEN] = {0};
  118. _write(MHZ19_GETPPM, buffer);
  119. // Check response
  120. if ((buffer[0] == 0xFF)
  121. && (buffer[1] == 0x86)
  122. && (_checksum(buffer) == buffer[MHZ19_RESPONSE_LEN-1])) {
  123. unsigned int value = buffer[2] * 256 + buffer[3];
  124. if (0 <= value && value <= 5000) {
  125. _co2 = value;
  126. _error = SENSOR_ERROR_OK;
  127. } else {
  128. _error = SENSOR_ERROR_OUT_OF_RANGE;
  129. }
  130. }
  131. }
  132. uint8_t _checksum(uint8_t * command) {
  133. uint8_t sum = 0x00;
  134. for (unsigned char i = 1; i < MHZ19_REQUEST_LEN-1; i++) {
  135. sum += command[i];
  136. }
  137. sum = 0xFF - sum + 0x01;
  138. return sum;
  139. }
  140. double _co2 = 0;
  141. unsigned int _pin_rx;
  142. unsigned int _pin_tx;
  143. SoftwareSerial * _serial;
  144. };