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.

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