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.

150 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->begin(9600);
  53. _pms = new PMS(* _serial);
  54. _pms->passiveMode();
  55. _startTime = millis();
  56. }
  57. // Descriptive name of the sensor
  58. String description() {
  59. char buffer[28];
  60. snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  61. return String(buffer);
  62. }
  63. // Descriptive name of the slot # index
  64. String slot(unsigned char index) {
  65. char buffer[36] = {0};
  66. if (index == 0) snprintf(buffer, sizeof(buffer), "PM1.0 @ PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  67. if (index == 1) snprintf(buffer, sizeof(buffer), "PM2.5 @ PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  68. if (index == 2) snprintf(buffer, sizeof(buffer), "PM10 @ PMSX003 @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  69. return String(buffer);
  70. }
  71. // Address of the sensor (it could be the GPIO or I2C address)
  72. String address(unsigned char index) {
  73. char buffer[6];
  74. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  75. return String(buffer);
  76. }
  77. // Type for slot # index
  78. unsigned char type(unsigned char index) {
  79. if (index == 0) return MAGNITUDE_PM1dot0;
  80. if (index == 1) return MAGNITUDE_PM2dot5;
  81. if (index == 2) return MAGNITUDE_PM10;
  82. return MAGNITUDE_NONE;
  83. }
  84. void pre() {
  85. if (millis() - _startTime < 30000) {
  86. _error = SENSOR_ERROR_WARM_UP;
  87. return;
  88. }
  89. _error = SENSOR_ERROR_OK;
  90. if(_pms->read(_data)) {
  91. _pm1dot0 = _data.PM_AE_UG_1_0;
  92. _pm2dot5 = _data.PM_AE_UG_2_5;
  93. _pm10 = _data.PM_AE_UG_10_0;
  94. }
  95. _pms->requestRead();
  96. }
  97. // Current value for slot # index
  98. double value(unsigned char index) {
  99. if(index == 0) return _pm1dot0;
  100. if(index == 1) return _pm2dot5;
  101. if(index == 2) return _pm10;
  102. return 0;
  103. }
  104. protected:
  105. unsigned int _pm1dot0;
  106. unsigned int _pm2dot5;
  107. unsigned int _pm10;
  108. unsigned int _pin_rx;
  109. unsigned int _pin_tx;
  110. unsigned long _startTime;
  111. SoftwareSerial * _serial = NULL;
  112. PMS * _pms = NULL;
  113. PMS::DATA _data;
  114. };
  115. #endif // SENSOR_SUPPORT && PMSX003_SUPPORT