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.

134 lines
3.8 KiB

  1. // -----------------------------------------------------------------------------
  2. // PZEM004T based power monitor
  3. // Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && PZEM004T_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <PZEM004T.h>
  10. class PZEM004TSensor : public BaseSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. PZEM004TSensor(): BaseSensor(), _data() {
  16. _count = 4;
  17. _sensor_id = SENSOR_PZEM004T_ID;
  18. }
  19. ~PZEM004TSensor() {
  20. if (_pzem) delete _pzem;
  21. }
  22. // ---------------------------------------------------------------------
  23. void setRX(unsigned char pin_rx) {
  24. if (_pin_rx == pin_rx) return;
  25. _pin_rx = pin_rx;
  26. _dirty = true;
  27. }
  28. void setTX(unsigned char pin_tx) {
  29. if (_pin_tx == pin_tx) return;
  30. _pin_tx = pin_tx;
  31. _dirty = true;
  32. }
  33. void setSerial(Stream & serial) {
  34. _serial = serial;
  35. _dirty = true;
  36. }
  37. // ---------------------------------------------------------------------
  38. unsigned char getRX() {
  39. return _pin_rx;
  40. }
  41. unsigned char getTX() {
  42. return _pin_tx;
  43. }
  44. Stream & getSerial() {
  45. return _serial;
  46. }
  47. // ---------------------------------------------------------------------
  48. // Sensor API
  49. // ---------------------------------------------------------------------
  50. // Initialization method, must be idempotent
  51. void begin() {
  52. if (!_dirty) return;
  53. _dirty = false;
  54. if (_pzem) delete _pzem;
  55. if (_serial == NULL) {
  56. _pzem = PZEM004T(_pin_rx, _pin_tx);
  57. } else {
  58. _pzem = PZEM004T(_serial);
  59. }
  60. _pzem->setAddress(_ip);
  61. }
  62. // Descriptive name of the sensor
  63. String description() {
  64. char buffer[28];
  65. snprintf(buffer, sizeof(buffer), "PZEM004T @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  66. return String(buffer);
  67. }
  68. // Descriptive name of the slot # index
  69. String slot(unsigned char index) {
  70. return description();
  71. };
  72. // Address of the sensor (it could be the GPIO or I2C address)
  73. String address(unsigned char index) {
  74. return _ip.toString();
  75. }
  76. // Type for slot # index
  77. unsigned char type(unsigned char index) {
  78. if (index == 0) return MAGNITUDE_CURRENT;
  79. if (index == 1) return MAGNITUDE_VOLTAGE;
  80. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  81. if (index == 3) return MAGNITUDE_ENERGY;
  82. return MAGNITUDE_NONE;
  83. }
  84. // Current value for slot # index
  85. double value(unsigned char index) {
  86. if (index == 0) return _pzem->current(_ip);
  87. if (index == 1) return _pzem->voltage(_ip);
  88. if (index == 2) return _pzem->power(_ip);
  89. if (index == 3) return _pzem->energy(_ip);
  90. return 0;
  91. }
  92. protected:
  93. // ---------------------------------------------------------------------
  94. // Protected
  95. // ---------------------------------------------------------------------
  96. unsigned int _pin_rx = PZEM004T_RX_PIN;
  97. unsigned int _pin_tx = PZEM004T_TX_PIN;
  98. Stream & _serial = NULL;
  99. IPAddress _ip(192,168,1,1);
  100. PZEM004T * _pzem = NULL;
  101. };
  102. #endif // SENSOR_SUPPORT && PZEM004T_SUPPORT