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.

101 lines
3.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // PMSX003 Dust sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #include <PMS.h>
  8. #include <SoftwareSerial.h>
  9. class PMSX003Sensor : public BaseSensor {
  10. public:
  11. PMSX003Sensor(int pin_rx = PMS_RX_PIN, int pin_tx = PMS_TX_PIN): BaseSensor() {
  12. _pmsSerial = new SoftwareSerial(pin_rx, pin_tx, false, 256);
  13. _pmsSerial->begin(9600);
  14. _pms = new PMS(* _pmsSerial);
  15. _pms->passiveMode();
  16. _pin_rx = pin_rx;
  17. _pin_tx = pin_tx;
  18. _count = 3;
  19. _startTime = millis();
  20. }
  21. // Descriptive name of the sensor
  22. String name() {
  23. char buffer[28];
  24. snprintf(buffer, sizeof(buffer), "PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  25. return String(buffer);
  26. }
  27. // Descriptive name of the slot # index
  28. String slot(unsigned char index) {
  29. if (index < _count) {
  30. _error = SENSOR_ERROR_OK;
  31. char buffer[36];
  32. if (index == 0) snprintf(buffer, sizeof(buffer), "PM1.0 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  33. if (index == 1) snprintf(buffer, sizeof(buffer), "PM2.5 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  34. if (index == 2) snprintf(buffer, sizeof(buffer), "PM10 @ PMSX003 @ SwSerial(%i,%i)", _pin_rx, _pin_tx);
  35. return String(buffer);
  36. }
  37. _error = SENSOR_ERROR_OUT_OF_RANGE;
  38. return String();
  39. }
  40. // Type for slot # index
  41. magnitude_t type(unsigned char index) {
  42. if (index < _count) {
  43. _error = SENSOR_ERROR_OK;
  44. if (index == 0) return MAGNITUDE_PM1dot0;
  45. if (index == 1) return MAGNITUDE_PM2dot5;
  46. if (index == 2) return MAGNITUDE_PM10;
  47. }
  48. _error = SENSOR_ERROR_OUT_OF_RANGE;
  49. return MAGNITUDE_NONE;
  50. }
  51. void pre() {
  52. if(millis() - _startTime > 30000) {
  53. _error = SENSOR_ERROR_OK;
  54. } else {
  55. _error = SENSOR_ERROR_WARM_UP;
  56. }
  57. _pms->requestRead();
  58. }
  59. void tick() {
  60. if(_pms->read(_data)) {
  61. _pm1dot0 = _data.PM_AE_UG_1_0;
  62. _pm2dot5 = _data.PM_AE_UG_2_5;
  63. _pm10 = _data.PM_AE_UG_10_0;
  64. }
  65. }
  66. // Current value for slot # index
  67. double value(unsigned char index) {
  68. if (index < _count) {
  69. _error = SENSOR_ERROR_OK;
  70. if(index == 0) return _pm1dot0;
  71. if(index == 1) return _pm2dot5;
  72. if(index == 2) return _pm10;
  73. }
  74. _error = SENSOR_ERROR_OUT_OF_RANGE;
  75. return 0;
  76. }
  77. protected:
  78. unsigned int _pm1dot0;
  79. unsigned int _pm2dot5;
  80. unsigned int _pm10;
  81. unsigned int _pin_rx;
  82. unsigned int _pin_tx;
  83. unsigned long _startTime;
  84. SoftwareSerial * _pmsSerial;
  85. PMS * _pms;
  86. PMS::DATA _data;
  87. };