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.

327 lines
10 KiB

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