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.

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