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.

325 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] PMS: Packet available = %d\n", 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 (size != PMS_PAYLOAD_SIZE(data_count)) {
  80. #if SENSOR_DEBUG
  81. debugSend(("[SENSOR] PMS: Payload size: %d != %d.\n"), size, PMS_PAYLOAD_SIZE(data_count));
  82. #endif
  83. break;
  84. }
  85. for (int i = 0; i < data_count; i++) {
  86. data[i] = read16(sum);
  87. #if SENSOR_DEBUG
  88. //debugSend(("[SENSOR] PMS: data[%d] = %d\n"), i, data[i]);
  89. #endif
  90. }
  91. uint16_t checksum = read16();
  92. if (sum == checksum) {
  93. return true;
  94. } else {
  95. #if SENSOR_DEBUG
  96. debugSend(("[SENSOR] PMS checksum: %04X != %04X\n"), sum, checksum);
  97. #endif
  98. }
  99. break;
  100. }
  101. } while (true);
  102. return false;
  103. }
  104. private:
  105. // Read 16-bit
  106. inline uint16_t read16() {
  107. return ((uint16_t) _serial->read()) << 8 | _serial->read();
  108. }
  109. // Read 16-bit and calculate checksum
  110. uint16_t read16(uint16_t &checksum) {
  111. uint8_t high = _serial->read();
  112. uint8_t low = _serial->read();
  113. checksum += high;
  114. checksum += low;
  115. return ((uint16_t) high) << 8 | low;
  116. }
  117. };
  118. class PMSX003Sensor : public BaseSensor, PMSX003 {
  119. public:
  120. // ---------------------------------------------------------------------
  121. // Public
  122. // ---------------------------------------------------------------------
  123. PMSX003Sensor(): BaseSensor() {
  124. _count = pms_specs[_type].slot_count;
  125. _sensor_id = SENSOR_PMSX003_ID;
  126. }
  127. ~PMSX003Sensor() {
  128. if (_serial) delete _serial;
  129. }
  130. void setRX(unsigned char pin_rx) {
  131. if (_pin_rx == pin_rx) return;
  132. _pin_rx = pin_rx;
  133. _dirty = true;
  134. }
  135. void setTX(unsigned char pin_tx) {
  136. if (_pin_tx == pin_tx) return;
  137. _pin_tx = pin_tx;
  138. _dirty = true;
  139. }
  140. // Should call setType after constrcutor immediately to enable corresponding slot count
  141. void setType(unsigned char type) {
  142. _type = type;
  143. _count = pms_specs[_type].slot_count;
  144. }
  145. // ---------------------------------------------------------------------
  146. unsigned char getRX() {
  147. return _pin_rx;
  148. }
  149. unsigned char getTX() {
  150. return _pin_tx;
  151. }
  152. unsigned char getType() {
  153. return _type;
  154. }
  155. // ---------------------------------------------------------------------
  156. // Sensor API
  157. // ---------------------------------------------------------------------
  158. // Initialization method, must be idempotent
  159. void begin() {
  160. if (!_dirty) return;
  161. if (_serial) delete _serial;
  162. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 64);
  163. _serial->enableIntTx(false);
  164. _serial->begin(9600);
  165. passiveMode();
  166. _startTime = millis();
  167. _ready = true;
  168. _dirty = false;
  169. }
  170. // Descriptive name of the sensor
  171. String description() {
  172. char buffer[28];
  173. snprintf(buffer, sizeof(buffer), "%s @ SwSerial(%u,%u)", pms_specs[_type].name, _pin_rx, _pin_tx);
  174. return String(buffer);
  175. }
  176. // Descriptive name of the slot # index
  177. String slot(unsigned char index) {
  178. char buffer[36] = {0};
  179. snprintf(buffer, sizeof(buffer), "%d @ %s @ SwSerial(%u,%u)", int(index + 1), pms_specs[_type].name, _pin_rx, _pin_tx);
  180. return String(buffer);
  181. }
  182. // Address of the sensor (it could be the GPIO or I2C address)
  183. String address(unsigned char index) {
  184. char buffer[6];
  185. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  186. return String(buffer);
  187. }
  188. // Type for slot # index
  189. unsigned char type(unsigned char index) {
  190. return pms_specs[_type].slot_types[index];
  191. }
  192. void pre() {
  193. if (millis() - _startTime < 30000) {
  194. _error = SENSOR_ERROR_WARM_UP;
  195. return;
  196. }
  197. _error = SENSOR_ERROR_OK;
  198. #if PMS_SMART_SLEEP
  199. unsigned int readCycle;
  200. if (_readCount++ > 30) {
  201. readCycle = _readCount % 30;
  202. if (readCycle == 0) {
  203. #if SENSOR_DEBUG
  204. debugSend("[SENSOR] %s: Wake up: %d\n", pms_specs[_type].name, _readCount);
  205. #endif
  206. wakeUp();
  207. return;
  208. } else if (readCycle == 1) {
  209. requestRead();
  210. } else if (readCycle > 6) {
  211. return;
  212. }
  213. } else {
  214. readCycle = -1;
  215. }
  216. #endif
  217. uint16_t data[PMS_DATA_MAX];
  218. if (readData(data, pms_specs[_type].data_count)) {
  219. if (_type == PMS_TYPE_5003ST) {
  220. _slot_values[0] = data[4];
  221. _slot_values[1] = (double)data[13] / 10;
  222. _slot_values[2] = (double)data[14] / 10;
  223. _slot_values[3] = (double)data[12] / 1000;
  224. } else if (_type == PMS_TYPE_5003T) {
  225. _slot_values[0] = data[4];
  226. _slot_values[1] = (double)data[10] / 10;
  227. _slot_values[2] = (double)data[11] / 10;
  228. } else {
  229. _slot_values[0] = data[3];
  230. _slot_values[1] = data[4];
  231. _slot_values[2] = data[5];
  232. }
  233. }
  234. #if PMS_SMART_SLEEP
  235. if (readCycle == 6) {
  236. sleep();
  237. #if SENSOR_DEBUG
  238. debugSend("[SENSOR] %s: Enter sleep mode: %d\n", pms_specs[_type].name, _readCount);
  239. #endif
  240. return;
  241. }
  242. #endif
  243. requestRead();
  244. }
  245. // Current value for slot # index
  246. double value(unsigned char index) {
  247. return _slot_values[index];
  248. }
  249. protected:
  250. unsigned int _pin_rx;
  251. unsigned int _pin_tx;
  252. unsigned long _startTime;
  253. unsigned char _type = PMS_TYPE_X003;
  254. double _slot_values[PMS_SLOT_MAX] = {0};
  255. #if PMS_SMART_SLEEP
  256. unsigned int _readCount = 0;
  257. #endif
  258. };
  259. #endif // SENSOR_SUPPORT && PMS_SUPPORT