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.

219 lines
6.2 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-2018 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 "BaseSensor.h"
  12. #include <SoftwareSerial.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(): BaseSensor() {
  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. _dirty = false;
  58. if (_serial) delete _serial;
  59. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 32);
  60. _serial->enableIntTx(false);
  61. _serial->begin(9600);
  62. calibrateAuto(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 slot(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. protected:
  110. // ---------------------------------------------------------------------
  111. // Protected
  112. // ---------------------------------------------------------------------
  113. void _write(unsigned char * command) {
  114. _serial->write(command, MHZ19_REQUEST_LEN);
  115. _serial->write(_checksum(command));
  116. _serial->flush();
  117. }
  118. void _write(unsigned int command, unsigned char * response) {
  119. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  120. buffer[0] = 0xFF;
  121. buffer[1] = 0x01;
  122. buffer[2] = command >> 8;
  123. buffer[3] = command & 0xFF;
  124. _write(buffer);
  125. if (response != NULL) {
  126. unsigned long start = millis();
  127. while (_serial->available() == 0) {
  128. if (millis() - start > MHZ19_TIMEOUT) {
  129. _error = SENSOR_ERROR_TIMEOUT;
  130. return;
  131. }
  132. yield();
  133. }
  134. _serial->readBytes(response, MHZ19_RESPONSE_LEN);
  135. }
  136. }
  137. void _write(unsigned int command) {
  138. _write(command, NULL);
  139. }
  140. void _read() {
  141. unsigned char buffer[MHZ19_RESPONSE_LEN] = {0};
  142. _write(MHZ19_GETPPM, buffer);
  143. // Check response
  144. if ((buffer[0] == 0xFF)
  145. && (buffer[1] == 0x86)
  146. && (_checksum(buffer) == buffer[MHZ19_RESPONSE_LEN-1])) {
  147. unsigned int value = buffer[2] * 256 + buffer[3];
  148. if (0 <= value && value <= 5000) {
  149. _co2 = value;
  150. _error = SENSOR_ERROR_OK;
  151. } else {
  152. _error = SENSOR_ERROR_OUT_OF_RANGE;
  153. }
  154. } else {
  155. _error = SENSOR_ERROR_CRC;
  156. }
  157. }
  158. uint8_t _checksum(uint8_t * command) {
  159. uint8_t sum = 0x00;
  160. for (unsigned char i = 1; i < MHZ19_REQUEST_LEN-1; i++) {
  161. sum += command[i];
  162. }
  163. sum = 0xFF - sum + 0x01;
  164. return sum;
  165. }
  166. double _co2 = 0;
  167. unsigned int _pin_rx;
  168. unsigned int _pin_tx;
  169. SoftwareSerial * _serial = NULL;
  170. };
  171. #endif // SENSOR_SUPPORT && MHZ19_SUPPORT