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.

382 lines
13 KiB

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