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.

203 lines
5.8 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. _sensor_id = SENSOR_MHZ19_ID;
  28. }
  29. void setRX(unsigned char pin_rx) {
  30. if (_pin_rx == pin_rx) return;
  31. _pin_rx = pin_rx;
  32. _dirty = true;
  33. }
  34. void setTX(unsigned char pin_tx) {
  35. if (_pin_tx == pin_tx) return;
  36. _pin_tx = pin_tx;
  37. _dirty = true;
  38. }
  39. // ---------------------------------------------------------------------
  40. unsigned char getRX() {
  41. return _pin_rx;
  42. }
  43. unsigned char getTX() {
  44. return _pin_tx;
  45. }
  46. // ---------------------------------------------------------------------
  47. // Sensor API
  48. // ---------------------------------------------------------------------
  49. // Initialization method, must be idempotent
  50. void begin() {
  51. if (!_dirty) return;
  52. _dirty = false;
  53. if (_serial) delete _serial;
  54. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 256);
  55. _serial->begin(9600);
  56. calibrateAuto(false);
  57. }
  58. // Descriptive name of the sensor
  59. String description() {
  60. char buffer[28];
  61. snprintf(buffer, sizeof(buffer), "MHZ19 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  62. return String(buffer);
  63. }
  64. // Descriptive name of the slot # index
  65. String slot(unsigned char index) {
  66. return description();
  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;
  160. };