xsns_15_mhz19.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. xsns_15_mhz19.ino - MH-Z19(B) CO2 sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_MHZ19
  16. /*********************************************************************************************\
  17. * MH-Z19 - CO2 sensor
  18. *
  19. * Adapted from EspEasy plugin P049 by Dmitry (rel22 ___ inbox.ru)
  20. *
  21. * Hardware Serial will be selected if GPIO1 = [MHZ Rx] and GPIO3 = [MHZ Tx]
  22. **********************************************************************************************
  23. * Filter usage
  24. *
  25. * Select filter usage on low stability readings
  26. \*********************************************************************************************/
  27. #define XSNS_15 15
  28. enum MhzFilterOptions {MHZ19_FILTER_OFF, MHZ19_FILTER_OFF_ALLSAMPLES, MHZ19_FILTER_FAST, MHZ19_FILTER_MEDIUM, MHZ19_FILTER_SLOW};
  29. #define MHZ19_FILTER_OPTION MHZ19_FILTER_FAST
  30. /*********************************************************************************************\
  31. * Source: http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf
  32. *
  33. * Automatic Baseline Correction (ABC logic function)
  34. *
  35. * ABC logic function refers to that sensor itself do zero point judgment and automatic calibration procedure
  36. * intelligently after a continuous operation period. The automatic calibration cycle is every 24 hours after powered on.
  37. *
  38. * The zero point of automatic calibration is 400ppm.
  39. *
  40. * This function is usually suitable for indoor air quality monitor such as offices, schools and homes,
  41. * not suitable for greenhouse, farm and refrigeratory where this function should be off.
  42. *
  43. * Please do zero calibration timely, such as manual or commend calibration.
  44. \*********************************************************************************************/
  45. #define MHZ19_ABC_ENABLE 1 // Automatic Baseline Correction (0 = off, 1 = on (default))
  46. /*********************************************************************************************/
  47. #include <TasmotaSerial.h>
  48. #ifndef CO2_LOW
  49. #define CO2_LOW 800 // Below this CO2 value show green light
  50. #endif
  51. #ifndef CO2_HIGH
  52. #define CO2_HIGH 1200 // Above this CO2 value show red light
  53. #endif
  54. #define MHZ19_READ_TIMEOUT 400 // Must be way less than 1000 but enough to read 9 bytes at 9600 bps
  55. #define MHZ19_RETRY_COUNT 8
  56. TasmotaSerial *MhzSerial;
  57. const char kMhzTypes[] PROGMEM = "MHZ19|MHZ19B";
  58. enum MhzCommands { MHZ_CMND_READPPM, MHZ_CMND_ABCENABLE, MHZ_CMND_ABCDISABLE, MHZ_CMND_ZEROPOINT, MHZ_CMND_RESET, MHZ_CMND_RANGE_1000, MHZ_CMND_RANGE_2000, MHZ_CMND_RANGE_3000, MHZ_CMND_RANGE_5000 };
  59. const uint8_t kMhzCommands[][4] PROGMEM = {
  60. // 2 3 6 7
  61. {0x86,0x00,0x00,0x00}, // mhz_cmnd_read_ppm
  62. {0x79,0xA0,0x00,0x00}, // mhz_cmnd_abc_enable
  63. {0x79,0x00,0x00,0x00}, // mhz_cmnd_abc_disable
  64. {0x87,0x00,0x00,0x00}, // mhz_cmnd_zeropoint
  65. {0x8D,0x00,0x00,0x00}, // mhz_cmnd_reset
  66. {0x99,0x00,0x03,0xE8}, // mhz_cmnd_set_range_1000
  67. {0x99,0x00,0x07,0xD0}, // mhz_cmnd_set_range_2000
  68. {0x99,0x00,0x0B,0xB8}, // mhz_cmnd_set_range_3000
  69. {0x99,0x00,0x13,0x88}}; // mhz_cmnd_set_range_5000
  70. uint8_t mhz_type = 1;
  71. uint16_t mhz_last_ppm = 0;
  72. uint8_t mhz_filter = MHZ19_FILTER_OPTION;
  73. bool mhz_abc_enable = MHZ19_ABC_ENABLE;
  74. bool mhz_abc_must_apply = false;
  75. char mhz_types[7];
  76. float mhz_temperature = 0;
  77. uint8_t mhz_retry = MHZ19_RETRY_COUNT;
  78. uint8_t mhz_received = 0;
  79. uint8_t mhz_state = 0;
  80. /*********************************************************************************************/
  81. byte MhzCalculateChecksum(byte *array)
  82. {
  83. byte checksum = 0;
  84. for (byte i = 1; i < 8; i++) {
  85. checksum += array[i];
  86. }
  87. checksum = 255 - checksum;
  88. return (checksum +1);
  89. }
  90. size_t MhzSendCmd(byte command_id)
  91. {
  92. uint8_t mhz_send[9] = { 0 };
  93. mhz_send[0] = 0xFF; // Start byte, fixed
  94. mhz_send[1] = 0x01; // Sensor number, 0x01 by default
  95. memcpy_P(&mhz_send[2], kMhzCommands[command_id], sizeof(uint16_t));
  96. /*
  97. mhz_send[4] = 0x00;
  98. mhz_send[5] = 0x00;
  99. */
  100. memcpy_P(&mhz_send[6], kMhzCommands[command_id] + sizeof(uint16_t), sizeof(uint16_t));
  101. mhz_send[8] = MhzCalculateChecksum(mhz_send);
  102. snprintf_P(log_data, sizeof(log_data), PSTR("Final MhzCommand: %x %x %x %x %x %x %x %x %x"),mhz_send[0],mhz_send[1],mhz_send[2],mhz_send[3],mhz_send[4],mhz_send[5],mhz_send[6],mhz_send[7],mhz_send[8]);
  103. AddLog(LOG_LEVEL_DEBUG);
  104. return MhzSerial->write(mhz_send, sizeof(mhz_send));
  105. }
  106. /*********************************************************************************************/
  107. bool MhzCheckAndApplyFilter(uint16_t ppm, uint8_t s)
  108. {
  109. if (1 == s) {
  110. return false; // S==1 => "A" version sensor bootup, do not use values.
  111. }
  112. if (mhz_last_ppm < 400 || mhz_last_ppm > 5000) {
  113. // Prevent unrealistic values during start-up with filtering enabled.
  114. // Just assume the entered value is correct.
  115. mhz_last_ppm = ppm;
  116. return true;
  117. }
  118. int32_t difference = ppm - mhz_last_ppm;
  119. if (s > 0 && s < 64 && mhz_filter != MHZ19_FILTER_OFF) {
  120. // Not the "B" version of the sensor, S value is used.
  121. // S==0 => "B" version, else "A" version
  122. // The S value is an indication of the stability of the reading.
  123. // S == 64 represents a stable reading and any lower value indicates (unusual) fast change.
  124. // Now we increase the delay filter for low values of S and increase response time when the
  125. // value is more stable.
  126. // This will make the reading useful in more turbulent environments,
  127. // where the sensor would report more rapid change of measured values.
  128. difference *= s;
  129. difference /= 64;
  130. }
  131. if (MHZ19_FILTER_OFF == mhz_filter) {
  132. if (s != 0 && s != 64) {
  133. return false;
  134. }
  135. } else {
  136. difference >>= (mhz_filter -1);
  137. }
  138. mhz_last_ppm = static_cast<uint16_t>(mhz_last_ppm + difference);
  139. return true;
  140. }
  141. void MhzEverySecond(void)
  142. {
  143. mhz_state++;
  144. if (8 == mhz_state) { // Every 8 sec start a MH-Z19 measuring cycle (which takes 1005 +5% ms)
  145. mhz_state = 0;
  146. if (mhz_retry) {
  147. mhz_retry--;
  148. if (!mhz_retry) {
  149. mhz_last_ppm = 0;
  150. mhz_temperature = 0;
  151. }
  152. }
  153. MhzSerial->flush(); // Sync reception
  154. MhzSendCmd(MHZ_CMND_READPPM);
  155. mhz_received = 0;
  156. }
  157. if ((mhz_state > 2) && !mhz_received) { // Start reading response after 3 seconds every second until received
  158. uint8_t mhz_response[9];
  159. unsigned long start = millis();
  160. uint8_t counter = 0;
  161. while (((millis() - start) < MHZ19_READ_TIMEOUT) && (counter < 9)) {
  162. if (MhzSerial->available() > 0) {
  163. mhz_response[counter++] = MhzSerial->read();
  164. } else {
  165. delay(5);
  166. }
  167. }
  168. AddLogSerial(LOG_LEVEL_DEBUG_MORE, mhz_response, counter);
  169. if (counter < 9) {
  170. // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 comms timeout"));
  171. return;
  172. }
  173. byte crc = MhzCalculateChecksum(mhz_response);
  174. if (mhz_response[8] != crc) {
  175. // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 crc error"));
  176. return;
  177. }
  178. if (0xFF != mhz_response[0] || 0x86 != mhz_response[1]) {
  179. // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 bad response"));
  180. return;
  181. }
  182. mhz_received = 1;
  183. uint16_t u = (mhz_response[6] << 8) | mhz_response[7];
  184. if (15000 == u) { // During (and only ever at) sensor boot, 'u' is reported as 15000
  185. if (!mhz_abc_enable) {
  186. // After bootup of the sensor the ABC will be enabled.
  187. // Thus only actively disable after bootup.
  188. mhz_abc_must_apply = true;
  189. }
  190. } else {
  191. uint16_t ppm = (mhz_response[2] << 8) | mhz_response[3];
  192. mhz_temperature = ConvertTemp((float)mhz_response[4] - 40);
  193. uint8_t s = mhz_response[5];
  194. mhz_type = (s) ? 1 : 2;
  195. if (MhzCheckAndApplyFilter(ppm, s)) {
  196. mhz_retry = MHZ19_RETRY_COUNT;
  197. LightSetSignal(CO2_LOW, CO2_HIGH, mhz_last_ppm);
  198. if (0 == s || 64 == s) { // Reading is stable.
  199. if (mhz_abc_must_apply) {
  200. mhz_abc_must_apply = false;
  201. if (mhz_abc_enable) {
  202. MhzSendCmd(MHZ_CMND_ABCENABLE);
  203. } else {
  204. MhzSendCmd(MHZ_CMND_ABCDISABLE);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. /*********************************************************************************************\
  213. * Command Sensor15
  214. *
  215. * 0 - (Not implemented) ABC Off
  216. * 1 - (Not implemented) ABC On
  217. * 2 - Manual start = ABC Off
  218. * 3 - (Not implemented) Optional filter settings
  219. * 9 - Reset
  220. * 1000 - Range
  221. * 2000 - Range
  222. * 3000 - Range
  223. * 5000 - Range
  224. \*********************************************************************************************/
  225. #define D_JSON_RANGE_1000 "1000 ppm range"
  226. #define D_JSON_RANGE_2000 "2000 ppm range"
  227. #define D_JSON_RANGE_3000 "3000 ppm range"
  228. #define D_JSON_RANGE_5000 "5000 ppm range"
  229. bool MhzCommandSensor(void)
  230. {
  231. boolean serviced = true;
  232. switch (XdrvMailbox.payload) {
  233. case 2:
  234. MhzSendCmd(MHZ_CMND_ZEROPOINT);
  235. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_15, D_JSON_ZERO_POINT_CALIBRATION);
  236. break;
  237. case 9:
  238. MhzSendCmd(MHZ_CMND_RESET);
  239. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_15, D_JSON_RESET);
  240. break;
  241. case 1000:
  242. MhzSendCmd(MHZ_CMND_RANGE_1000);
  243. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_15, D_JSON_RANGE_1000);
  244. break;
  245. case 2000:
  246. MhzSendCmd(MHZ_CMND_RANGE_2000);
  247. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_15, D_JSON_RANGE_2000);
  248. break;
  249. case 3000:
  250. MhzSendCmd(MHZ_CMND_RANGE_3000);
  251. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_15, D_JSON_RANGE_3000);
  252. break;
  253. case 5000:
  254. MhzSendCmd(MHZ_CMND_RANGE_5000);
  255. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_15, D_JSON_RANGE_5000);
  256. break;
  257. default:
  258. serviced = false;
  259. }
  260. return serviced;
  261. }
  262. /*********************************************************************************************/
  263. void MhzInit(void)
  264. {
  265. mhz_type = 0;
  266. if ((pin[GPIO_MHZ_RXD] < 99) && (pin[GPIO_MHZ_TXD] < 99)) {
  267. MhzSerial = new TasmotaSerial(pin[GPIO_MHZ_RXD], pin[GPIO_MHZ_TXD], 1);
  268. if (MhzSerial->begin(9600)) {
  269. if (MhzSerial->hardwareSerial()) { ClaimSerial(); }
  270. mhz_type = 1;
  271. }
  272. }
  273. }
  274. void MhzShow(boolean json)
  275. {
  276. char temperature[33];
  277. dtostrfd(mhz_temperature, Settings.flag2.temperature_resolution, temperature);
  278. GetTextIndexed(mhz_types, sizeof(mhz_types), mhz_type -1, kMhzTypes);
  279. if (json) {
  280. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, mhz_types, mhz_last_ppm, temperature);
  281. #ifdef USE_DOMOTICZ
  282. if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, mhz_last_ppm);
  283. #endif // USE_DOMOTICZ
  284. #ifdef USE_WEBSERVER
  285. } else {
  286. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_CO2, mqtt_data, mhz_types, mhz_last_ppm);
  287. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, mhz_types, temperature, TempUnit());
  288. #endif // USE_WEBSERVER
  289. }
  290. }
  291. /*********************************************************************************************\
  292. * Interface
  293. \*********************************************************************************************/
  294. boolean Xsns15(byte function)
  295. {
  296. boolean result = false;
  297. if (mhz_type) {
  298. switch (function) {
  299. case FUNC_INIT:
  300. MhzInit();
  301. break;
  302. case FUNC_EVERY_SECOND:
  303. MhzEverySecond();
  304. break;
  305. case FUNC_COMMAND:
  306. if (XSNS_15 == XdrvMailbox.index) {
  307. result = MhzCommandSensor();
  308. }
  309. break;
  310. case FUNC_JSON_APPEND:
  311. MhzShow(1);
  312. break;
  313. #ifdef USE_WEBSERVER
  314. case FUNC_WEB_APPEND:
  315. MhzShow(0);
  316. break;
  317. #endif // USE_WEBSERVER
  318. }
  319. }
  320. return result;
  321. }
  322. #endif // USE_MHZ19