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.

163 lines
4.7 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. _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. if (index < _count) {
  66. _error = SENSOR_ERROR_OK;
  67. char buffer[36];
  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. _error = SENSOR_ERROR_OUT_OF_RANGE;
  74. return String();
  75. }
  76. // Address of the sensor (it could be the GPIO or I2C address)
  77. String address(unsigned char index) {
  78. char buffer[6];
  79. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  80. return String(buffer);
  81. }
  82. // Type for slot # index
  83. unsigned char type(unsigned char index) {
  84. if (index < _count) {
  85. _error = SENSOR_ERROR_OK;
  86. if (index == 0) return MAGNITUDE_PM1dot0;
  87. if (index == 1) return MAGNITUDE_PM2dot5;
  88. if (index == 2) return MAGNITUDE_PM10;
  89. }
  90. _error = SENSOR_ERROR_OUT_OF_RANGE;
  91. return MAGNITUDE_NONE;
  92. }
  93. void pre() {
  94. if (millis() - _startTime < 30000) {
  95. _error = SENSOR_ERROR_WARM_UP;
  96. return;
  97. }
  98. _error = SENSOR_ERROR_OK;
  99. if(_pms->read(_data)) {
  100. _pm1dot0 = _data.PM_AE_UG_1_0;
  101. _pm2dot5 = _data.PM_AE_UG_2_5;
  102. _pm10 = _data.PM_AE_UG_10_0;
  103. }
  104. _pms->requestRead();
  105. }
  106. // Current value for slot # index
  107. double value(unsigned char index) {
  108. if (index < _count) {
  109. _error = SENSOR_ERROR_OK;
  110. if(index == 0) return _pm1dot0;
  111. if(index == 1) return _pm2dot5;
  112. if(index == 2) return _pm10;
  113. }
  114. _error = SENSOR_ERROR_OUT_OF_RANGE;
  115. return 0;
  116. }
  117. protected:
  118. unsigned int _pm1dot0;
  119. unsigned int _pm2dot5;
  120. unsigned int _pm10;
  121. unsigned int _pin_rx;
  122. unsigned int _pin_tx;
  123. unsigned long _startTime;
  124. SoftwareSerial * _serial = NULL;
  125. PMS * _pms = NULL;
  126. PMS::DATA _data;
  127. };
  128. #endif // SENSOR_SUPPORT && PMSX003_SUPPORT