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.

139 lines
4.0 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. if (_serial) {
  65. snprintf(buffer, sizeof(buffer), "PZEM004T @ HwSerial");
  66. } else {
  67. snprintf(buffer, sizeof(buffer), "PZEM004T @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  68. }
  69. return String(buffer);
  70. }
  71. // Descriptive name of the slot # index
  72. String slot(unsigned char index) {
  73. return description();
  74. };
  75. // Address of the sensor (it could be the GPIO or I2C address)
  76. String address(unsigned char index) {
  77. return _ip.toString();
  78. }
  79. // Type for slot # index
  80. unsigned char type(unsigned char index) {
  81. if (index == 0) return MAGNITUDE_CURRENT;
  82. if (index == 1) return MAGNITUDE_VOLTAGE;
  83. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  84. if (index == 3) return MAGNITUDE_ENERGY;
  85. return MAGNITUDE_NONE;
  86. }
  87. // Current value for slot # index
  88. double value(unsigned char index) {
  89. double response = 0;
  90. if (index == 0) response = _pzem->current(_ip);
  91. if (index == 1) response = _pzem->voltage(_ip);
  92. if (index == 2) response = _pzem->power(_ip);
  93. if (index == 3) response = _pzem->energy(_ip) * 3600;
  94. if (response < 0) response = 0;
  95. return response;
  96. }
  97. protected:
  98. // ---------------------------------------------------------------------
  99. // Protected
  100. // ---------------------------------------------------------------------
  101. unsigned int _pin_rx = PZEM004T_RX_PIN;
  102. unsigned int _pin_tx = PZEM004T_TX_PIN;
  103. IPAddress _ip;
  104. HardwareSerial * _serial = NULL;
  105. PZEM004T * _pzem = NULL;
  106. };
  107. #endif // SENSOR_SUPPORT && PZEM004T_SUPPORT