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.

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