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.

152 lines
4.3 KiB

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