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.

332 lines
11 KiB

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