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.

152 lines
4.4 KiB

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