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.

229 lines
6.4 KiB

7 years ago
  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-2019 by Xose Pérez <xose dot perez at gmail dot com>
  7. // -----------------------------------------------------------------------------
  8. #if SENSOR_SUPPORT && MHZ19_SUPPORT
  9. #pragma once
  10. #include <SoftwareSerial.h>
  11. #include "BaseSensor.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() {
  26. _count = 1;
  27. _sensor_id = SENSOR_MHZ19_ID;
  28. }
  29. ~MHZ19Sensor() {
  30. if (_serial) delete _serial;
  31. }
  32. // ---------------------------------------------------------------------
  33. void setRX(unsigned char pin_rx) {
  34. if (_pin_rx == pin_rx) return;
  35. _pin_rx = pin_rx;
  36. _dirty = true;
  37. }
  38. void setTX(unsigned char pin_tx) {
  39. if (_pin_tx == pin_tx) return;
  40. _pin_tx = pin_tx;
  41. _dirty = true;
  42. }
  43. // ---------------------------------------------------------------------
  44. unsigned char getRX() {
  45. return _pin_rx;
  46. }
  47. unsigned char getTX() {
  48. return _pin_tx;
  49. }
  50. // ---------------------------------------------------------------------
  51. // Sensor API
  52. // ---------------------------------------------------------------------
  53. // Initialization method, must be idempotent
  54. void begin() {
  55. if (!_dirty) return;
  56. if (_serial) delete _serial;
  57. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false);
  58. _serial->enableIntTx(false);
  59. _serial->begin(9600);
  60. calibrateAuto(_calibrateAuto);
  61. _ready = true;
  62. _dirty = false;
  63. }
  64. // Descriptive name of the sensor
  65. String description() {
  66. char buffer[28];
  67. snprintf(buffer, sizeof(buffer), "MHZ19 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  68. return String(buffer);
  69. }
  70. // Descriptive name of the slot # index
  71. String description(unsigned char index) {
  72. return description();
  73. };
  74. // Address of the sensor (it could be the GPIO or I2C address)
  75. String address(unsigned char index) {
  76. char buffer[6];
  77. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  78. return String(buffer);
  79. }
  80. // Type for slot # index
  81. unsigned char type(unsigned char index) {
  82. if (index == 0) return MAGNITUDE_CO2;
  83. return MAGNITUDE_NONE;
  84. }
  85. void pre() {
  86. _read();
  87. }
  88. // Current value for slot # index
  89. double value(unsigned char index) {
  90. if (index == 0) return _co2;
  91. return 0;
  92. }
  93. void calibrateAuto(boolean state){
  94. _write(state ? MHZ19_AUTOCALIB_ON : MHZ19_AUTOCALIB_OFF);
  95. }
  96. void calibrateZero() {
  97. _write(MHZ19_ZEROCALIB);
  98. }
  99. void calibrateSpan(unsigned int ppm) {
  100. if( ppm < 1000 ) return;
  101. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  102. buffer[0] = 0xFF;
  103. buffer[1] = 0x01;
  104. buffer[2] = MHZ19_SPANCALIB >> 8;
  105. buffer[3] = ppm >> 8;
  106. buffer[4] = ppm & 0xFF;
  107. _write(buffer);
  108. }
  109. void setCalibrateAuto(boolean value) {
  110. _calibrateAuto = value;
  111. if (_ready) {
  112. calibrateAuto(value);
  113. }
  114. }
  115. protected:
  116. // ---------------------------------------------------------------------
  117. // Protected
  118. // ---------------------------------------------------------------------
  119. void _write(unsigned char * command) {
  120. _serial->write(command, MHZ19_REQUEST_LEN);
  121. _serial->write(_checksum(command));
  122. _serial->flush();
  123. }
  124. void _write(unsigned int command, unsigned char * response) {
  125. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  126. buffer[0] = 0xFF;
  127. buffer[1] = 0x01;
  128. buffer[2] = command >> 8;
  129. buffer[3] = command & 0xFF;
  130. _write(buffer);
  131. if (response != NULL) {
  132. unsigned long start = millis();
  133. while (_serial->available() == 0) {
  134. if (millis() - start > MHZ19_TIMEOUT) {
  135. _error = SENSOR_ERROR_TIMEOUT;
  136. return;
  137. }
  138. yield();
  139. }
  140. _serial->readBytes(response, MHZ19_RESPONSE_LEN);
  141. }
  142. }
  143. void _write(unsigned int command) {
  144. _write(command, NULL);
  145. }
  146. void _read() {
  147. unsigned char buffer[MHZ19_RESPONSE_LEN] = {0};
  148. _write(MHZ19_GETPPM, buffer);
  149. // Check response
  150. if ((buffer[0] == 0xFF)
  151. && (buffer[1] == 0x86)
  152. && (_checksum(buffer) == buffer[MHZ19_RESPONSE_LEN-1])) {
  153. unsigned int value = buffer[2] * 256 + buffer[3];
  154. if (0 <= value && value <= 5000) {
  155. _co2 = value;
  156. _error = SENSOR_ERROR_OK;
  157. } else {
  158. _error = SENSOR_ERROR_OUT_OF_RANGE;
  159. }
  160. } else {
  161. _error = SENSOR_ERROR_CRC;
  162. }
  163. }
  164. uint8_t _checksum(uint8_t * command) {
  165. uint8_t sum = 0x00;
  166. for (unsigned char i = 1; i < MHZ19_REQUEST_LEN-1; i++) {
  167. sum += command[i];
  168. }
  169. sum = 0xFF - sum + 0x01;
  170. return sum;
  171. }
  172. double _co2 = 0;
  173. unsigned int _pin_rx;
  174. unsigned int _pin_tx;
  175. bool _calibrateAuto = false;
  176. SoftwareSerial * _serial = NULL;
  177. };
  178. #endif // SENSOR_SUPPORT && MHZ19_SUPPORT