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.

177 lines
5.2 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 _hlwSetCalibration() {
  24. double value;
  25. value = getSetting("powerRatioP", 0).toFloat();
  26. if (value > 0) _hlw8012.setPowerMultiplier(value);
  27. value = getSetting("powerRatioC", 0).toFloat();
  28. if (value > 0) _hlw8012.setCurrentMultiplier(value);
  29. value = getSetting("powerRatioV", 0).toFloat();
  30. if (value > 0) _hlw8012.setVoltageMultiplier(value);
  31. }
  32. void _hlwGetCalibration() {
  33. setSetting("powerRatioP", _hlw8012.getPowerMultiplier());
  34. setSetting("powerRatioC", _hlw8012.getCurrentMultiplier());
  35. setSetting("powerRatioV", _hlw8012.getVoltageMultiplier());
  36. saveSettings();
  37. }
  38. void _hlwResetCalibration() {
  39. _hlw8012.resetMultipliers();
  40. _hlwGetCalibration();
  41. }
  42. void _hlwExpectedPower(unsigned int power) {
  43. if (power > 0) {
  44. _hlw8012.expectedActivePower(power);
  45. _hlwGetCalibration();
  46. }
  47. }
  48. void _hlwExpectedCurrent(double current) {
  49. if (current > 0) {
  50. _hlw8012.expectedCurrent(current);
  51. _hlwGetCalibration();
  52. }
  53. }
  54. void _hlwExpectedVoltage(unsigned int voltage) {
  55. if (voltage > 0) {
  56. _hlw8012.expectedVoltage(voltage);
  57. _hlwGetCalibration();
  58. }
  59. }
  60. // -----------------------------------------------------------------------------
  61. // POWER API
  62. // -----------------------------------------------------------------------------
  63. double _powerCurrent() {
  64. return _hlw8012.getCurrent();
  65. }
  66. double _powerVoltage() {
  67. return _hlw8012.getVoltage();
  68. }
  69. double _powerActivePower() {
  70. return _hlw8012.getActivePower();
  71. }
  72. double _powerApparentPower() {
  73. return _hlw8012.getApparentPower();
  74. }
  75. double _powerReactivePower() {
  76. double active = _powerActivePower();
  77. double apparent = _powerApparentPower();
  78. if (apparent > active) return sqrt(apparent * apparent - active * active);
  79. return 0;
  80. }
  81. double _powerPowerFactor() {
  82. double apparent = _powerApparentPower();
  83. if (apparent > 0) return _powerActivePower() / apparent;
  84. return 1;
  85. }
  86. void _powerEnabledProvider() {
  87. if (_power_enabled) {
  88. attachInterrupt(HLW8012_CF1_PIN, _hlw_cf1_isr, CHANGE);
  89. attachInterrupt(HLW8012_CF_PIN, _hlw_cf_isr, CHANGE);
  90. } else {
  91. detachInterrupt(HLW8012_CF1_PIN);
  92. detachInterrupt(HLW8012_CF_PIN);
  93. }
  94. }
  95. void _powerConfigureProvider() {
  96. _hlwSetCalibration();
  97. _hlwGetCalibration();
  98. }
  99. void _powerSetupProvider() {
  100. // Initialize HLW8012
  101. // 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);
  102. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  103. // * currentWhen is the value in sel_pin to select current sampling
  104. // * set use_interrupts to true to use interrupts to monitor pulse widths
  105. // * leave pulse_timeout to the default value, recommended when using interrupts
  106. #if HLW8012_USE_INTERRUPTS
  107. _hlw8012.begin(HLW8012_CF_PIN, HLW8012_CF1_PIN, HLW8012_SEL_PIN, HLW8012_SEL_CURRENT, true);
  108. #else
  109. _hlw8012.begin(HLW8012_CF_PIN, HLW8012_CF1_PIN, HLW8012_SEL_PIN, HLW8012_SEL_CURRENT, false, 1000000);
  110. #endif
  111. // These values are used to calculate current, voltage and power factors as per datasheet formula
  112. // These are the nominal values for the Sonoff POW resistors:
  113. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  114. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  115. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  116. _hlw8012.setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
  117. powerConfigureProvider();
  118. _power_wifi_onconnect = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
  119. powerEnabled(true);
  120. });
  121. _power_wifi_ondisconnect = WiFi.onStationModeDisconnected([](WiFiEventStationModeDisconnected ipInfo) {
  122. powerEnabled(false);
  123. });
  124. }
  125. void _powerLoopProvider(bool before) {
  126. if (before) {
  127. static unsigned long last = 0;
  128. if (millis() - last > POWER_READ_INTERVAL) {
  129. last = millis();
  130. _power_newdata = true;
  131. }
  132. } else {
  133. // Toggle between current and voltage monitoring
  134. #if (HLW8012_USE_INTERRUPTS == 0)
  135. _hlw8012.toggleMode();
  136. #endif // (HLW8012_USE_INTERRUPTS == 0)
  137. }
  138. }
  139. #endif // POWER_PROVIDER == POWER_PROVIDER_HLW8012