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.

156 lines
4.5 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, 256);
  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(%i,%i)", _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(%i,%i)", _pin_rx, _pin_tx);
  69. if (index == 1) snprintf(buffer, sizeof(buffer), "PM2.5 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  70. if (index == 2) snprintf(buffer, sizeof(buffer), "PM10 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  71. return String(buffer);
  72. }
  73. _error = SENSOR_ERROR_OUT_OF_RANGE;
  74. return String();
  75. }
  76. // Type for slot # index
  77. unsigned char type(unsigned char index) {
  78. if (index < _count) {
  79. _error = SENSOR_ERROR_OK;
  80. if (index == 0) return MAGNITUDE_PM1dot0;
  81. if (index == 1) return MAGNITUDE_PM2dot5;
  82. if (index == 2) return MAGNITUDE_PM10;
  83. }
  84. _error = SENSOR_ERROR_OUT_OF_RANGE;
  85. return MAGNITUDE_NONE;
  86. }
  87. void pre() {
  88. if (millis() - _startTime < 30000) {
  89. _error = SENSOR_ERROR_WARM_UP;
  90. return;
  91. }
  92. _error = SENSOR_ERROR_OK;
  93. if(_pms->read(_data)) {
  94. _pm1dot0 = _data.PM_AE_UG_1_0;
  95. _pm2dot5 = _data.PM_AE_UG_2_5;
  96. _pm10 = _data.PM_AE_UG_10_0;
  97. }
  98. _pms->requestRead();
  99. }
  100. // Current value for slot # index
  101. double value(unsigned char index) {
  102. if (index < _count) {
  103. _error = SENSOR_ERROR_OK;
  104. if(index == 0) return _pm1dot0;
  105. if(index == 1) return _pm2dot5;
  106. if(index == 2) return _pm10;
  107. }
  108. _error = SENSOR_ERROR_OUT_OF_RANGE;
  109. return 0;
  110. }
  111. protected:
  112. unsigned int _pm1dot0;
  113. unsigned int _pm2dot5;
  114. unsigned int _pm10;
  115. unsigned int _pin_rx;
  116. unsigned int _pin_tx;
  117. unsigned long _startTime;
  118. SoftwareSerial * _serial = NULL;
  119. PMS * _pms = NULL;
  120. PMS::DATA _data;
  121. };
  122. #endif // SENSOR_SUPPORT && PMSX003_SUPPORT