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.

162 lines
4.9 KiB

  1. /*
  2. ESPurna
  3. POW MODULE
  4. Support for Sonoff POW HLW8012-based power monitor
  5. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  6. */
  7. #if ENABLE_POW
  8. #include <HLW8012.h>
  9. HLW8012 hlw8012;
  10. // -----------------------------------------------------------------------------
  11. // POW
  12. // -----------------------------------------------------------------------------
  13. // When using interrupts we have to call the library entry point
  14. // whenever an interrupt is triggered
  15. void hlw8012_cf1_interrupt() {
  16. hlw8012.cf1_interrupt();
  17. }
  18. void hlw8012_cf_interrupt() {
  19. hlw8012.cf_interrupt();
  20. }
  21. void powAttachInterrupts() {
  22. //attachInterrupt(POW_CF1_PIN, hlw8012_cf1_interrupt, CHANGE);
  23. attachInterrupt(POW_CF_PIN, hlw8012_cf_interrupt, CHANGE);
  24. DEBUG_MSG("[POW] Enabled\n");
  25. }
  26. void powDettachInterrupts() {
  27. //detachInterrupt(POW_CF1_PIN);
  28. detachInterrupt(POW_CF_PIN);
  29. DEBUG_MSG("[POW] Disabled\n");
  30. }
  31. void powSaveCalibration() {
  32. setSetting("powPowerMult", String() + hlw8012.getPowerMultiplier());
  33. setSetting("powCurrentMult", String() + hlw8012.getCurrentMultiplier());
  34. setSetting("powVoltageMult", String() + hlw8012.getVoltageMultiplier());
  35. }
  36. void powRetrieveCalibration() {
  37. double value;
  38. value = getSetting("powPowerMult", "0").toFloat();
  39. if (value > 0) hlw8012.setPowerMultiplier((int) value);
  40. value = getSetting("powCurrentMult", "0").toFloat();
  41. if (value > 0) hlw8012.setCurrentMultiplier((int) value);
  42. value = getSetting("powVoltageMult", "0").toFloat();
  43. if (value > 0) hlw8012.setVoltageMultiplier((int) value);
  44. }
  45. void powSetExpectedActivePower(unsigned int power) {
  46. if (power > 0) {
  47. hlw8012.expectedActivePower(power);
  48. powSaveCalibration();
  49. }
  50. }
  51. void powSetExpectedCurrent(double current) {
  52. if (current > 0) {
  53. hlw8012.expectedCurrent(current);
  54. powSaveCalibration();
  55. }
  56. }
  57. void powSetExpectedVoltage(unsigned int voltage) {
  58. if (voltage > 0) {
  59. hlw8012.expectedVoltage(voltage);
  60. powSaveCalibration();
  61. }
  62. }
  63. unsigned int getActivePower() {
  64. return hlw8012.getActivePower();
  65. }
  66. unsigned int getApparentPower() {
  67. return hlw8012.getApparentPower();
  68. }
  69. double getCurrent() {
  70. return hlw8012.getCurrent();
  71. }
  72. unsigned int getVoltage() {
  73. return hlw8012.getVoltage();
  74. }
  75. unsigned int getPowerFactor() {
  76. return (int) (100 * hlw8012.getPowerFactor());
  77. }
  78. void powSetup() {
  79. // Initialize HLW8012
  80. // void begin(unsigned char cf_pin, unsigned char cf1_pin, unsigned char sel_pin, unsigned char currentWhen = HIGH, bool use_interrupts = false, unsigned long pulse_timeout = PULSE_TIMEOUT);
  81. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  82. // * currentWhen is the value in sel_pin to select current sampling
  83. // * set use_interrupts to true to use interrupts to monitor pulse widths
  84. // * leave pulse_timeout to the default value, recommended when using interrupts
  85. hlw8012.begin(POW_CF_PIN, POW_CF1_PIN, POW_SEL_PIN, POW_SEL_CURRENT, false, 1000000);
  86. // These values are used to calculate current, voltage and power factors as per datasheet formula
  87. // These are the nominal values for the Sonoff POW resistors:
  88. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  89. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  90. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  91. hlw8012.setResistors(POW_CURRENT_R, POW_VOLTAGE_R_UP, POW_VOLTAGE_R_DOWN);
  92. powRetrieveCalibration();
  93. /*
  94. static WiFiEventHandler e1 = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
  95. powDettachInterrupts();
  96. });
  97. static WiFiEventHandler e2 = WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& event) {
  98. powDettachInterrupts();
  99. });
  100. static WiFiEventHandler e3 = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected& event) {
  101. powAttachInterrupts();
  102. });
  103. static WiFiEventHandler e4 = WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& event) {
  104. powAttachInterrupts();
  105. });
  106. */
  107. }
  108. void powLoop() {
  109. static unsigned long last_update = 0;
  110. static unsigned char report_count = POW_REPORT_EVERY;
  111. if ((millis() - last_update > POW_UPDATE_INTERVAL) || (last_update == 0 )){
  112. last_update = millis();
  113. unsigned int power = getActivePower();
  114. char buffer[100];
  115. sprintf_P(buffer, PSTR("{\"powVisible\": 1, \"powActivePower\": %d}"), power);
  116. wsSend(buffer);
  117. if (--report_count == 0) {
  118. mqttSend((char *) getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), (char *) String(power).c_str());
  119. report_count = POW_REPORT_EVERY;
  120. }
  121. }
  122. }
  123. #endif