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.

145 lines
4.3 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. _sensor_id = SENSOR_PMSX003_ID;
  19. }
  20. void setRX(unsigned char pin_rx) {
  21. if (_pin_rx == pin_rx) return;
  22. _pin_rx = pin_rx;
  23. _dirty = true;
  24. }
  25. void setTX(unsigned char pin_tx) {
  26. if (_pin_tx == pin_tx) return;
  27. _pin_tx = pin_tx;
  28. _dirty = true;
  29. }
  30. // ---------------------------------------------------------------------
  31. unsigned char getRX() {
  32. return _pin_rx;
  33. }
  34. unsigned char getTX() {
  35. return _pin_tx;
  36. }
  37. // ---------------------------------------------------------------------
  38. // Sensor API
  39. // ---------------------------------------------------------------------
  40. // Initialization method, must be idempotent
  41. void begin() {
  42. if (!_dirty) return;
  43. _dirty = false;
  44. if (_serial) delete _serial;
  45. if (_pms) delete _pms;
  46. _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 256);
  47. _serial->begin(9600);
  48. _pms = new PMS(* _serial);
  49. _pms->passiveMode();
  50. _startTime = millis();
  51. }
  52. // Descriptive name of the sensor
  53. String description() {
  54. char buffer[28];
  55. snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  56. return String(buffer);
  57. }
  58. // Descriptive name of the slot # index
  59. String slot(unsigned char index) {
  60. if (index < _count) {
  61. _error = SENSOR_ERROR_OK;
  62. char buffer[36];
  63. if (index == 0) snprintf(buffer, sizeof(buffer), "PM1.0 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  64. if (index == 1) snprintf(buffer, sizeof(buffer), "PM2.5 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  65. if (index == 2) snprintf(buffer, sizeof(buffer), "PM10 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  66. return String(buffer);
  67. }
  68. _error = SENSOR_ERROR_OUT_OF_RANGE;
  69. return String();
  70. }
  71. // Type for slot # index
  72. magnitude_t type(unsigned char index) {
  73. if (index < _count) {
  74. _error = SENSOR_ERROR_OK;
  75. if (index == 0) return MAGNITUDE_PM1dot0;
  76. if (index == 1) return MAGNITUDE_PM2dot5;
  77. if (index == 2) return MAGNITUDE_PM10;
  78. }
  79. _error = SENSOR_ERROR_OUT_OF_RANGE;
  80. return MAGNITUDE_NONE;
  81. }
  82. void pre() {
  83. if(millis() - _startTime > 30000) {
  84. _error = SENSOR_ERROR_OK;
  85. } else {
  86. _error = SENSOR_ERROR_WARM_UP;
  87. }
  88. _pms->requestRead();
  89. }
  90. void tick() {
  91. if(_pms->read(_data)) {
  92. _pm1dot0 = _data.PM_AE_UG_1_0;
  93. _pm2dot5 = _data.PM_AE_UG_2_5;
  94. _pm10 = _data.PM_AE_UG_10_0;
  95. }
  96. }
  97. // Current value for slot # index
  98. double value(unsigned char index) {
  99. if (index < _count) {
  100. _error = SENSOR_ERROR_OK;
  101. if(index == 0) return _pm1dot0;
  102. if(index == 1) return _pm2dot5;
  103. if(index == 2) return _pm10;
  104. }
  105. _error = SENSOR_ERROR_OUT_OF_RANGE;
  106. return 0;
  107. }
  108. protected:
  109. unsigned int _pm1dot0;
  110. unsigned int _pm2dot5;
  111. unsigned int _pm10;
  112. unsigned int _pin_rx;
  113. unsigned int _pin_tx;
  114. unsigned long _startTime;
  115. SoftwareSerial * _serial;
  116. PMS * _pms;
  117. PMS::DATA _data;
  118. };