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.

381 lines
13 KiB

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