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.

170 lines
5.3 KiB

  1. /*
  2. POWER HLW8012 MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if POWER_PROVIDER == POWER_PROVIDER_HLW8012
  6. // -----------------------------------------------------------------------------
  7. // MODULE GLOBALS AND CACHE
  8. // -----------------------------------------------------------------------------
  9. #include <HLW8012.h>
  10. #include <ESP8266WiFi.h>
  11. HLW8012 _hlw8012;
  12. WiFiEventHandler _power_wifi_onconnect;
  13. WiFiEventHandler _power_wifi_ondisconnect;
  14. // -----------------------------------------------------------------------------
  15. // HAL
  16. // -----------------------------------------------------------------------------
  17. void ICACHE_RAM_ATTR _hlw_cf1_isr() {
  18. _hlw8012.cf1_interrupt();
  19. }
  20. void ICACHE_RAM_ATTR _hlw_cf_isr() {
  21. _hlw8012.cf_interrupt();
  22. }
  23. void _hlwRestoreCalibration() {
  24. double value;
  25. value = getSetting("pwrRatioP", 0).toFloat();
  26. if (value > 0) _hlw8012.setPowerMultiplier(value);
  27. value = getSetting("pwrRatioC", 0).toFloat();
  28. if (value > 0) _hlw8012.setCurrentMultiplier(value);
  29. value = getSetting("pwrRatioV", 0).toFloat();
  30. if (value > 0) _hlw8012.setVoltageMultiplier(value);
  31. }
  32. void _hlwPersistCalibration() {
  33. setSetting("pwrRatioP", _hlw8012.getPowerMultiplier());
  34. setSetting("pwrRatioC", _hlw8012.getCurrentMultiplier());
  35. setSetting("pwrRatioV", _hlw8012.getVoltageMultiplier());
  36. saveSettings();
  37. }
  38. // -----------------------------------------------------------------------------
  39. // POWER API
  40. // -----------------------------------------------------------------------------
  41. double _powerCurrent() {
  42. return _hlw8012.getCurrent();
  43. }
  44. double _powerVoltage() {
  45. return _hlw8012.getVoltage();
  46. }
  47. double _powerActivePower() {
  48. return _hlw8012.getActivePower();
  49. }
  50. double _powerApparentPower() {
  51. return _hlw8012.getApparentPower();
  52. }
  53. double _powerReactivePower() {
  54. return _hlw8012.getReactivePower();
  55. }
  56. double _powerPowerFactor() {
  57. return _hlw8012.getPowerFactor();
  58. }
  59. double _powerEnergy() {
  60. return _hlw8012.getEnergy();
  61. }
  62. void _powerEnabledProvider() {
  63. #if HLW8012_USE_INTERRUPTS
  64. if (_power_enabled) {
  65. attachInterrupt(HLW8012_CF1_PIN, _hlw_cf1_isr, CHANGE);
  66. attachInterrupt(HLW8012_CF_PIN, _hlw_cf_isr, CHANGE);
  67. } else {
  68. detachInterrupt(HLW8012_CF1_PIN);
  69. detachInterrupt(HLW8012_CF_PIN);
  70. }
  71. #endif
  72. }
  73. void _powerCalibrateProvider(unsigned char magnitude, double value) {
  74. if (value <= 0) return;
  75. if (magnitude == POWER_MAGNITUDE_ACTIVE) _hlw8012.expectedActivePower(value);
  76. if (magnitude == POWER_MAGNITUDE_CURRENT) _hlw8012.expectedCurrent(value);
  77. if (magnitude == POWER_MAGNITUDE_VOLTAGE) _hlw8012.expectedVoltage(value);
  78. _hlwPersistCalibration();
  79. }
  80. void _powerResetCalibrationProvider() {
  81. _hlw8012.resetMultipliers();
  82. delSetting("pwrRatioC");
  83. delSetting("pwrRatioV");
  84. delSetting("pwrRatioP");
  85. saveSettings();
  86. }
  87. void _powerConfigureProvider() {
  88. // Nothing to do
  89. }
  90. void _powerSetupProvider() {
  91. // Initialize HLW8012
  92. // 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);
  93. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  94. // * currentWhen is the value in sel_pin to select current sampling
  95. // * set use_interrupts to true to use interrupts to monitor pulse widths
  96. // * leave pulse_timeout to the default value, recommended when using interrupts
  97. #if HLW8012_USE_INTERRUPTS
  98. _hlw8012.begin(HLW8012_CF_PIN, HLW8012_CF1_PIN, HLW8012_SEL_PIN, HLW8012_SEL_CURRENT, true);
  99. #else
  100. _hlw8012.begin(HLW8012_CF_PIN, HLW8012_CF1_PIN, HLW8012_SEL_PIN, HLW8012_SEL_CURRENT, false, 1000000);
  101. #endif
  102. // These values are used to calculate current, voltage and power factors as per datasheet formula
  103. // These are the nominal values for the Sonoff POW resistors:
  104. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  105. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  106. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  107. _hlw8012.setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
  108. _hlwRestoreCalibration();
  109. #if HLW8012_USE_INTERRUPTS
  110. powerEnabled(true); //Always keep measurement active to keep track of energy used
  111. #else
  112. _power_wifi_onconnect = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
  113. powerEnabled(true);
  114. });
  115. _power_wifi_ondisconnect = WiFi.onStationModeDisconnected([](WiFiEventStationModeDisconnected ipInfo) {
  116. powerEnabled(false);
  117. });
  118. #endif
  119. }
  120. void _powerLoopProvider(bool before) {
  121. if (before) {
  122. static unsigned long last = 0;
  123. if (millis() - last > POWER_READ_INTERVAL) {
  124. last = millis();
  125. _power_newdata = true;
  126. // Toggle between current and voltage monitoring
  127. #if (HLW8012_USE_INTERRUPTS == 0)
  128. _hlw8012.toggleMode();
  129. #endif // (HLW8012_USE_INTERRUPTS == 0)
  130. }
  131. }
  132. }
  133. #endif // POWER_PROVIDER == POWER_PROVIDER_HLW8012