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.

144 lines
4.2 KiB

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