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.

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