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.

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