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.

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