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.

151 lines
4.3 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // PMSX003 Dust Sensor
  3. // Uses SoftwareSerial library
  4. // Contribution by Òscar Rovira López
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && PMSX003_SUPPORT
  7. #pragma once
  8. #include "Arduino.h"
  9. #include "BaseSensor.h"
  10. #include <PMS.h>
  11. #include <SoftwareSerial.h>
  12. class PMSX003Sensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. PMSX003Sensor(): BaseSensor() {
  18. _count = 3;
  19. _sensor_id = SENSOR_PMSX003_ID;
  20. }
  21. ~PMSX003Sensor() {
  22. if (_serial) delete _serial;
  23. if (_pms) delete _pms;
  24. }
  25. void setRX(unsigned char pin_rx) {
  26. if (_pin_rx == pin_rx) return;
  27. _pin_rx = pin_rx;
  28. _dirty = true;
  29. }
  30. void setTX(unsigned char pin_tx) {
  31. if (_pin_tx == pin_tx) return;
  32. _pin_tx = pin_tx;
  33. _dirty = true;
  34. }
  35. // ---------------------------------------------------------------------
  36. unsigned char getRX() {
  37. return _pin_rx;
  38. }
  39. unsigned char getTX() {
  40. return _pin_tx;
  41. }
  42. // ---------------------------------------------------------------------
  43. // Sensor API
  44. // ---------------------------------------------------------------------
  45. // Initialization method, must be idempotent
  46. void begin() {
  47. if (!_dirty) return;
  48. _dirty = false;
  49. if (_serial) delete _serial;
  50. if (_pms) delete _pms;
  51. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 32);
  52. _serial->enableIntTx(false);
  53. _serial->begin(9600);
  54. _pms = new PMS(* _serial);
  55. _pms->passiveMode();
  56. _startTime = millis();
  57. }
  58. // Descriptive name of the sensor
  59. String description() {
  60. char buffer[28];
  61. snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  62. return String(buffer);
  63. }
  64. // Descriptive name of the slot # index
  65. String slot(unsigned char index) {
  66. char buffer[36] = {0};
  67. if (index == 0) snprintf(buffer, sizeof(buffer), "PM1.0 @ PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  68. if (index == 1) snprintf(buffer, sizeof(buffer), "PM2.5 @ PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  69. if (index == 2) snprintf(buffer, sizeof(buffer), "PM10 @ PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  70. return String(buffer);
  71. }
  72. // Address of the sensor (it could be the GPIO or I2C address)
  73. String address(unsigned char index) {
  74. char buffer[6];
  75. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  76. return String(buffer);
  77. }
  78. // Type for slot # index
  79. unsigned char type(unsigned char index) {
  80. if (index == 0) return MAGNITUDE_PM1dot0;
  81. if (index == 1) return MAGNITUDE_PM2dot5;
  82. if (index == 2) return MAGNITUDE_PM10;
  83. return MAGNITUDE_NONE;
  84. }
  85. void pre() {
  86. if (millis() - _startTime < 30000) {
  87. _error = SENSOR_ERROR_WARM_UP;
  88. return;
  89. }
  90. _error = SENSOR_ERROR_OK;
  91. if(_pms->read(_data)) {
  92. _pm1dot0 = _data.PM_AE_UG_1_0;
  93. _pm2dot5 = _data.PM_AE_UG_2_5;
  94. _pm10 = _data.PM_AE_UG_10_0;
  95. }
  96. _pms->requestRead();
  97. }
  98. // Current value for slot # index
  99. double value(unsigned char index) {
  100. if(index == 0) return _pm1dot0;
  101. if(index == 1) return _pm2dot5;
  102. if(index == 2) return _pm10;
  103. return 0;
  104. }
  105. protected:
  106. unsigned int _pm1dot0;
  107. unsigned int _pm2dot5;
  108. unsigned int _pm10;
  109. unsigned int _pin_rx;
  110. unsigned int _pin_tx;
  111. unsigned long _startTime;
  112. SoftwareSerial * _serial = NULL;
  113. PMS * _pms = NULL;
  114. PMS::DATA _data;
  115. };
  116. #endif // SENSOR_SUPPORT && PMSX003_SUPPORT