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.

133 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() {
  16. _count = 4;
  17. _sensor_id = SENSOR_PZEM004T_ID;
  18. _ip = IPAddress(192,168,1,1);
  19. }
  20. ~PZEM004TSensor() {
  21. if (_pzem) delete _pzem;
  22. }
  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. void setSerial(HardwareSerial * serial) {
  35. _serial = serial;
  36. _dirty = true;
  37. }
  38. // ---------------------------------------------------------------------
  39. unsigned char getRX() {
  40. return _pin_rx;
  41. }
  42. unsigned char getTX() {
  43. return _pin_tx;
  44. }
  45. // ---------------------------------------------------------------------
  46. // Sensor API
  47. // ---------------------------------------------------------------------
  48. // Initialization method, must be idempotent
  49. void begin() {
  50. if (!_dirty) return;
  51. if (_pzem) delete _pzem;
  52. if (_serial) {
  53. _pzem = new PZEM004T(_serial);
  54. } else {
  55. _pzem = new PZEM004T(_pin_rx, _pin_tx);
  56. }
  57. _pzem->setAddress(_ip);
  58. _ready = true;
  59. _dirty = false;
  60. }
  61. // Descriptive name of the sensor
  62. String description() {
  63. char buffer[28];
  64. snprintf(buffer, sizeof(buffer), "PZEM004T @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  65. return String(buffer);
  66. }
  67. // Descriptive name of the slot # index
  68. String slot(unsigned char index) {
  69. return description();
  70. };
  71. // Address of the sensor (it could be the GPIO or I2C address)
  72. String address(unsigned char index) {
  73. return _ip.toString();
  74. }
  75. // Type for slot # index
  76. unsigned char type(unsigned char index) {
  77. if (index == 0) return MAGNITUDE_CURRENT;
  78. if (index == 1) return MAGNITUDE_VOLTAGE;
  79. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  80. if (index == 3) return MAGNITUDE_ENERGY;
  81. return MAGNITUDE_NONE;
  82. }
  83. // Current value for slot # index
  84. double value(unsigned char index) {
  85. if (index == 0) return _pzem->current(_ip);
  86. if (index == 1) return _pzem->voltage(_ip);
  87. if (index == 2) return _pzem->power(_ip);
  88. if (index == 3) return _pzem->energy(_ip);
  89. return 0;
  90. }
  91. protected:
  92. // ---------------------------------------------------------------------
  93. // Protected
  94. // ---------------------------------------------------------------------
  95. unsigned int _pin_rx = PZEM004T_RX_PIN;
  96. unsigned int _pin_tx = PZEM004T_TX_PIN;
  97. IPAddress _ip;
  98. HardwareSerial * _serial = NULL;
  99. PZEM004T * _pzem = NULL;
  100. };
  101. #endif // SENSOR_SUPPORT && PZEM004T_SUPPORT