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.

334 lines
11 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // PMSX003 Dust Sensor
  3. // Uses SoftwareSerial library
  4. // Contribution by Òscar Rovira López
  5. // Refine to support PMS5003T/PMS5003ST by Yonsm Guo
  6. // -----------------------------------------------------------------------------
  7. #if SENSOR_SUPPORT && PMSX003_SUPPORT
  8. #pragma once
  9. #include "Arduino.h"
  10. #include "BaseSensor.h"
  11. #include <PMS.h>
  12. #include <SoftwareSerial.h>
  13. //
  14. #define PMS_TYPE_X003 0
  15. #define PMS_TYPE_X003_9 1
  16. #define PMS_TYPE_5003T 2
  17. #define PMS_TYPE_5003ST 3
  18. #ifndef PMS_TYPE
  19. #define PMS_TYPE PMS_TYPE_X003
  20. #endif
  21. // You can enable smart sleep (read 6-times then sleep on 24-reading-cycles) to extend PMS sensor's life.
  22. // Otherwise the default lifetime of PMS sensor is about 8000-hours/1-years.
  23. // The PMS's fan will stop working on sleeping cycle, and will wake up on reading cycle.
  24. #ifndef PMS_SMART_SLEEP
  25. #define PMS_SMART_SLEEP 0
  26. #endif
  27. // [MAGIC][LEN][DATA9|13|17][SUM]
  28. #if PMS_TYPE == PMS_TYPE_5003ST
  29. #define PMS_TYPE_NAME "PMS5003ST"
  30. #define PMS_DATA_COUNT 17
  31. #define PMS_SLOT_COUNT 4
  32. #define PMS_SLOT_NAMES {"PM2.5", "TEMP", "HUMI", "HCHO"}
  33. #define PMS_SLOT_TYPES {MAGNITUDE_PM2dot5, MAGNITUDE_TEMPERATURE, MAGNITUDE_HUMIDITY, MAGNITUDE_ANALOG}
  34. #elif PMS_TYPE == PMS_TYPE_5003T
  35. #define PMS_TYPE_NAME "PMS5003T"
  36. #define PMS_DATA_COUNT 13
  37. #define PMS_SLOT_COUNT 3
  38. #define PMS_SLOT_NAMES {"PM2.5", "TEMP", "HUMI"}
  39. #define PMS_SLOT_TYPES {MAGNITUDE_PM2dot5, MAGNITUDE_TEMPERATURE, MAGNITUDE_HUMIDITY}
  40. #elif PMS_TYPE == PMS_TYPE_X003_9
  41. #define PMS_TYPE_NAME "PMSX003_9"
  42. #define PMS_DATA_COUNT 9
  43. #define PMS_SLOT_COUNT 3
  44. #define PMS_SLOT_NAMES {"PM1.0", "PM2.5", "PM10"}
  45. #define PMS_SLOT_TYPES {MAGNITUDE_PM1dot0, MAGNITUDE_PM2dot5, MAGNITUDE_PM10}
  46. #else
  47. #define PMS_TYPE_NAME "PMSX003"
  48. #define PMS_DATA_COUNT 13
  49. #define PMS_SLOT_COUNT 3
  50. #define PMS_SLOT_NAMES {"PM1.0", "PM2.5", "PM10"}
  51. #define PMS_SLOT_TYPES {MAGNITUDE_PM1dot0, MAGNITUDE_PM2dot5, MAGNITUDE_PM10}
  52. #endif
  53. #define PMS_PACKET_SIZE ((PMS_DATA_COUNT + 3) * 2)
  54. #define PMS_PAYLOAD_SIZE (PMS_DATA_COUNT * 2 + 2)
  55. // PMSX003 sensor utils
  56. // Command functions copied from: https://github.com/fu-hsi/PMS/blob/master/src/PMS.cpp
  57. // Reading function is rewrited to support flexible reading for PMS5003T/PMS5003ST
  58. class PMSX003 {
  59. protected:
  60. SoftwareSerial *_serial = NULL; // Should initialized by child class
  61. public:
  62. // Standby mode. For low power consumption and prolong the life of the sensor.
  63. inline void sleep() {
  64. uint8_t command[] = { 0x42, 0x4D, 0xE4, 0x00, 0x00, 0x01, 0x73 };
  65. _serial->write(command, sizeof(command));
  66. }
  67. // Operating mode. Stable data should be got at least 30 seconds after the sensor wakeup from the sleep mode because of the fan's performance.
  68. inline void wakeUp() {
  69. uint8_t command[] = { 0x42, 0x4D, 0xE4, 0x00, 0x01, 0x01, 0x74 };
  70. _serial->write(command, sizeof(command));
  71. }
  72. // Active mode. Default mode after power up. In this mode sensor would send serial data to the host automatically.
  73. inline void activeMode() {
  74. uint8_t command[] = { 0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71 };
  75. _serial->write(command, sizeof(command));
  76. }
  77. // Passive mode. In this mode, sensor would send serial data to the host only for request.
  78. inline void passiveMode() {
  79. uint8_t command[] = { 0x42, 0x4D, 0xE1, 0x00, 0x00, 0x01, 0x70 };
  80. _serial->write(command, sizeof(command));
  81. }
  82. // Request read, ONLY needed in Passive Mode!!
  83. inline void requestRead() {
  84. uint8_t command[] = { 0x42, 0x4D, 0xE2, 0x00, 0x00, 0x01, 0x71 };
  85. _serial->write(command, sizeof(command));
  86. }
  87. // Read sensor's data
  88. bool readData(uint16_t data[PMS_DATA_COUNT]) {
  89. do
  90. {
  91. int avail = _serial->available();
  92. #if SENSOR_DEBUG
  93. //debugSend("[SENSOR] %s: Packet available = %d\n", PMS_TYPE_NAME, avail);
  94. #endif
  95. if (avail < PMS_PACKET_SIZE)
  96. break;
  97. if (_serial->read() == 0x42 && _serial->read() == 0x4D)
  98. {
  99. uint16_t sum = 0x42 + 0x4D;
  100. uint16_t size = read16(sum);
  101. #if SENSOR_DEBUG
  102. debugSend("[SENSOR] %s: Payload size = %d\n", PMS_TYPE_NAME, size);
  103. #endif
  104. if (size != PMS_PAYLOAD_SIZE)
  105. {
  106. #if SENSOR_DEBUG
  107. debugSend(("[SENSOR] %s: Payload size != %d \n"), PMS_TYPE_NAME, PMS_PAYLOAD_SIZE);
  108. #endif
  109. break;
  110. }
  111. for (int i = 0; i < PMS_DATA_COUNT; i++)
  112. {
  113. data[i] = read16(sum);
  114. #if SENSOR_DEBUG
  115. //debugSend(("[SENSOR] %s: data[%d] = %d\n"), PMS_TYPE_NAME, i, data[i]);
  116. #endif
  117. }
  118. uint16_t checksum = read16();
  119. #if SENSOR_DEBUG
  120. debugSend(("[SENSOR] %s: Sum=%04X, Checksum=%04X\n"), PMS_TYPE_NAME, sum, checksum);
  121. #endif
  122. if (sum == checksum)
  123. {
  124. return true;
  125. }
  126. break;
  127. }
  128. }
  129. while (true);
  130. return false;
  131. }
  132. private:
  133. // Read 16-bit
  134. inline uint16_t read16() {
  135. return ((uint16_t) _serial->read()) << 8 | _serial->read();
  136. }
  137. // Read 16-bit and calculate checksum
  138. uint16_t read16(uint16_t &checksum) {
  139. uint8_t high = _serial->read();
  140. uint8_t low = _serial->read();
  141. checksum += high;
  142. checksum += low;
  143. return ((uint16_t) high) << 8 | low;
  144. }
  145. };
  146. class PMSX003Sensor : public BaseSensor, PMSX003 {
  147. public:
  148. // ---------------------------------------------------------------------
  149. // Public
  150. // ---------------------------------------------------------------------
  151. PMSX003Sensor(): BaseSensor() {
  152. _count = PMS_SLOT_COUNT;
  153. _sensor_id = SENSOR_PMSX003_ID;
  154. }
  155. ~PMSX003Sensor() {
  156. if (_serial) delete _serial;
  157. }
  158. void setRX(unsigned char pin_rx) {
  159. if (_pin_rx == pin_rx) return;
  160. _pin_rx = pin_rx;
  161. _dirty = true;
  162. }
  163. void setTX(unsigned char pin_tx) {
  164. if (_pin_tx == pin_tx) return;
  165. _pin_tx = pin_tx;
  166. _dirty = true;
  167. }
  168. // ---------------------------------------------------------------------
  169. unsigned char getRX() {
  170. return _pin_rx;
  171. }
  172. unsigned char getTX() {
  173. return _pin_tx;
  174. }
  175. // ---------------------------------------------------------------------
  176. // Sensor API
  177. // ---------------------------------------------------------------------
  178. // Initialization method, must be idempotent
  179. void begin() {
  180. if (!_dirty) return;
  181. if (_serial) delete _serial;
  182. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 64);
  183. _serial->enableIntTx(false);
  184. _serial->begin(9600);
  185. passiveMode();
  186. _startTime = millis();
  187. _ready = true;
  188. _dirty = false;
  189. }
  190. // Descriptive name of the sensor
  191. String description() {
  192. char buffer[28];
  193. snprintf(buffer, sizeof(buffer), "%s @ SwSerial(%u,%u)", PMS_TYPE_NAME, _pin_rx, _pin_tx);
  194. return String(buffer);
  195. }
  196. // Descriptive name of the slot # index
  197. String slot(unsigned char index) {
  198. char buffer[36] = {0};
  199. const static char *_slot_names[] = PMS_SLOT_NAMES;
  200. snprintf(buffer, sizeof(buffer), "%s @ %s @ SwSerial(%u,%u)", _slot_names[index], PMS_TYPE_NAME, _pin_rx, _pin_tx);
  201. return String(buffer);
  202. }
  203. // Address of the sensor (it could be the GPIO or I2C address)
  204. String address(unsigned char index) {
  205. char buffer[6];
  206. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  207. return String(buffer);
  208. }
  209. // Type for slot # index
  210. unsigned char type(unsigned char index) {
  211. const static unsigned char _slot_types[] = PMS_SLOT_TYPES;
  212. return _slot_types[index];
  213. }
  214. void pre() {
  215. if (millis() - _startTime < 30000) {
  216. _error = SENSOR_ERROR_WARM_UP;
  217. return;
  218. }
  219. _error = SENSOR_ERROR_OK;
  220. #if PMS_SMART_SLEEP
  221. unsigned int readCycle;
  222. if (_readCount++ > 30) {
  223. readCycle = _readCount % 30;
  224. if (readCycle == 0) {
  225. #if SENSOR_DEBUG
  226. debugSend("[SENSOR] %s: Wake up: %d\n", PMS_TYPE_NAME, _readCount);
  227. #endif
  228. wakeUp();
  229. return;
  230. } else if (readCycle == 1) {
  231. requestRead();
  232. } else if (readCycle > 6) {
  233. return;
  234. }
  235. } else {
  236. readCycle = -1;
  237. }
  238. #endif
  239. uint16_t data[PMS_DATA_COUNT];
  240. if (readData(data)) {
  241. #if PMS_TYPE == PMS_TYPE_5003ST
  242. _slot_values[0] = data[4];
  243. _slot_values[1] = (double)data[13] / 10;
  244. _slot_values[2] = (double)data[14] / 10;
  245. _slot_values[3] = (double)data[12] / 1000;
  246. #elif PMS_TYPE == PMS_TYPE_5003T
  247. _slot_values[0] = data[4];
  248. _slot_values[1] = (double)data[10] / 10;
  249. _slot_values[2] = (double)data[11] / 10;
  250. #else
  251. _slot_values[0] = data[3];
  252. _slot_values[1] = data[4];
  253. _slot_values[2] = data[5];
  254. #endif
  255. }
  256. #if PMS_SMART_SLEEP
  257. if (readCycle == 6) {
  258. sleep();
  259. #if SENSOR_DEBUG
  260. debugSend("[SENSOR] %s: Enter sleep mode: %d\n", PMS_TYPE_NAME, _readCount);
  261. #endif
  262. return;
  263. }
  264. #endif
  265. requestRead();
  266. }
  267. // Current value for slot # index
  268. double value(unsigned char index) {
  269. return _slot_values[index];
  270. }
  271. protected:
  272. unsigned int _pin_rx;
  273. unsigned int _pin_tx;
  274. unsigned long _startTime;
  275. double _slot_values[PMS_SLOT_COUNT] = {0};
  276. #if PMS_SMART_SLEEP
  277. unsigned int _readCount = 0;
  278. #endif
  279. };
  280. #endif // SENSOR_SUPPORT && PMSX003_SUPPORT