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.

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