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.

352 lines
11 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
  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. // 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. };
  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. //debugSend("[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. debugSend(("[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. //debugSend(("[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. debugSend(("[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. if (_serial) delete _serial;
  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) delete _serial;
  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. _error = SENSOR_ERROR_OK;
  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. debugSend("[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. }
  239. #endif
  240. uint16_t data[PMS_DATA_MAX];
  241. if (readData(data, pms_specs[_type].data_count)) {
  242. if (_type == PMS_TYPE_5003ST) {
  243. _slot_values[0] = data[4];
  244. _slot_values[1] = (double)data[13] / 10;
  245. _slot_values[2] = (double)data[14] / 10;
  246. _slot_values[3] = (double)data[12] / 1000;
  247. } else if (_type == PMS_TYPE_5003T) {
  248. _slot_values[0] = data[4];
  249. _slot_values[1] = (double)data[10] / 10;
  250. _slot_values[2] = (double)data[11] / 10;
  251. } else {
  252. _slot_values[0] = data[3];
  253. _slot_values[1] = data[4];
  254. _slot_values[2] = data[5];
  255. }
  256. }
  257. #if PMS_SMART_SLEEP
  258. if (readCycle == 6) {
  259. sleep();
  260. #if SENSOR_DEBUG
  261. debugSend("[SENSOR] %s: Enter sleep mode: %d\n", pms_specs[_type].name, _readCount);
  262. #endif
  263. return;
  264. }
  265. #endif
  266. requestRead();
  267. }
  268. // Current value for slot # index
  269. double value(unsigned char index) {
  270. return _slot_values[index];
  271. }
  272. protected:
  273. bool _soft = true;
  274. unsigned int _pin_rx;
  275. unsigned int _pin_tx;
  276. unsigned long _startTime;
  277. unsigned char _type = PMS_TYPE_X003;
  278. double _slot_values[PMS_SLOT_MAX] = {0};
  279. #if PMS_SMART_SLEEP
  280. unsigned int _readCount = 0;
  281. #endif
  282. };
  283. #endif // SENSOR_SUPPORT && PMSX003_SUPPORT