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.

136 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. if (_pzem) delete _pzem;
  54. if (_serial == NULL) {
  55. _pzem = PZEM004T(_pin_rx, _pin_tx);
  56. } else {
  57. _pzem = PZEM004T(_serial);
  58. }
  59. _pzem->setAddress(_ip);
  60. _ready = true;
  61. _dirty = false;
  62. }
  63. // Descriptive name of the sensor
  64. String description() {
  65. char buffer[28];
  66. snprintf(buffer, sizeof(buffer), "PZEM004T @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  67. return String(buffer);
  68. }
  69. // Descriptive name of the slot # index
  70. String slot(unsigned char index) {
  71. return description();
  72. };
  73. // Address of the sensor (it could be the GPIO or I2C address)
  74. String address(unsigned char index) {
  75. return _ip.toString();
  76. }
  77. // Type for slot # index
  78. unsigned char type(unsigned char index) {
  79. if (index == 0) return MAGNITUDE_CURRENT;
  80. if (index == 1) return MAGNITUDE_VOLTAGE;
  81. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  82. if (index == 3) return MAGNITUDE_ENERGY;
  83. return MAGNITUDE_NONE;
  84. }
  85. // Current value for slot # index
  86. double value(unsigned char index) {
  87. if (index == 0) return _pzem->current(_ip);
  88. if (index == 1) return _pzem->voltage(_ip);
  89. if (index == 2) return _pzem->power(_ip);
  90. if (index == 3) return _pzem->energy(_ip);
  91. return 0;
  92. }
  93. protected:
  94. // ---------------------------------------------------------------------
  95. // Protected
  96. // ---------------------------------------------------------------------
  97. unsigned int _pin_rx = PZEM004T_RX_PIN;
  98. unsigned int _pin_tx = PZEM004T_TX_PIN;
  99. Stream & _serial = NULL;
  100. IPAddress _ip(192,168,1,1);
  101. PZEM004T * _pzem = NULL;
  102. };
  103. #endif // SENSOR_SUPPORT && PZEM004T_SUPPORT