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.

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