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.

361 lines
12 KiB

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