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.

204 lines
5.8 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 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. _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. _dirty = false;
  57. if (_serial) delete _serial;
  58. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 256);
  59. _serial->begin(9600);
  60. calibrateAuto(false);
  61. }
  62. // Descriptive name of the sensor
  63. String description() {
  64. char buffer[28];
  65. snprintf(buffer, sizeof(buffer), "MHZ19 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  66. return String(buffer);
  67. }
  68. // Type for slot # index
  69. magnitude_t type(unsigned char index) {
  70. _error = SENSOR_ERROR_OK;
  71. if (index == 0) return MAGNITUDE_CO2;
  72. _error = SENSOR_ERROR_OUT_OF_RANGE;
  73. return MAGNITUDE_NONE;
  74. }
  75. void pre() {
  76. _read();
  77. }
  78. // Current value for slot # index
  79. double value(unsigned char index) {
  80. _error = SENSOR_ERROR_OK;
  81. if (index == 0) return _co2;
  82. _error = SENSOR_ERROR_OUT_OF_RANGE;
  83. return 0;
  84. }
  85. void calibrateAuto(boolean state){
  86. _write(state ? MHZ19_AUTOCALIB_ON : MHZ19_AUTOCALIB_OFF);
  87. }
  88. void calibrateZero() {
  89. _write(MHZ19_ZEROCALIB);
  90. }
  91. void calibrateSpan(unsigned int ppm) {
  92. if( ppm < 1000 ) return;
  93. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  94. buffer[0] = 0xFF;
  95. buffer[1] = 0x01;
  96. buffer[2] = MHZ19_SPANCALIB >> 8;
  97. buffer[3] = ppm >> 8;
  98. buffer[4] = ppm & 0xFF;
  99. _write(buffer);
  100. }
  101. protected:
  102. // ---------------------------------------------------------------------
  103. // Protected
  104. // ---------------------------------------------------------------------
  105. void _write(unsigned char * command) {
  106. _serial->write(command, MHZ19_REQUEST_LEN);
  107. _serial->write(_checksum(command));
  108. _serial->flush();
  109. }
  110. void _write(unsigned int command, unsigned char * response) {
  111. unsigned char buffer[MHZ19_REQUEST_LEN] = {0};
  112. buffer[0] = 0xFF;
  113. buffer[1] = 0x01;
  114. buffer[2] = command >> 8;
  115. buffer[3] = command & 0xFF;
  116. _write(buffer);
  117. if (response != NULL) {
  118. unsigned long start = millis();
  119. while (_serial->available() == 0) {
  120. if (millis() - start > MHZ19_TIMEOUT) {
  121. _error = SENSOR_ERROR_TIMEOUT;
  122. return;
  123. }
  124. yield();
  125. }
  126. _serial->readBytes(response, MHZ19_RESPONSE_LEN);
  127. }
  128. }
  129. void _write(unsigned int command) {
  130. _write(command, NULL);
  131. }
  132. void _read() {
  133. unsigned char buffer[MHZ19_RESPONSE_LEN] = {0};
  134. _write(MHZ19_GETPPM, buffer);
  135. // Check response
  136. if ((buffer[0] == 0xFF)
  137. && (buffer[1] == 0x86)
  138. && (_checksum(buffer) == buffer[MHZ19_RESPONSE_LEN-1])) {
  139. unsigned int value = buffer[2] * 256 + buffer[3];
  140. if (0 <= value && value <= 5000) {
  141. _co2 = value;
  142. _error = SENSOR_ERROR_OK;
  143. } else {
  144. _error = SENSOR_ERROR_OUT_OF_RANGE;
  145. }
  146. }
  147. }
  148. uint8_t _checksum(uint8_t * command) {
  149. uint8_t sum = 0x00;
  150. for (unsigned char i = 1; i < MHZ19_REQUEST_LEN-1; i++) {
  151. sum += command[i];
  152. }
  153. sum = 0xFF - sum + 0x01;
  154. return sum;
  155. }
  156. double _co2 = 0;
  157. unsigned int _pin_rx;
  158. unsigned int _pin_tx;
  159. SoftwareSerial * _serial = NULL;
  160. };