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.

217 lines
6.7 KiB

  1. /*
  2. POW MODULE
  3. Support for Sonoff POW HLW8012-based power monitor
  4. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #if ENABLE_POW
  7. #include <HLW8012.h>
  8. #define POW_USE_INTERRUPTS 1
  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. // -----------------------------------------------------------------------------
  32. void powSaveCalibration() {
  33. setSetting("powPowerMult", hlw8012.getPowerMultiplier());
  34. setSetting("powCurrentMult", hlw8012.getCurrentMultiplier());
  35. setSetting("powVoltageMult", hlw8012.getVoltageMultiplier());
  36. }
  37. void powRetrieveCalibration() {
  38. double value;
  39. value = getSetting("powPowerMult", 0).toFloat();
  40. if (value > 0) hlw8012.setPowerMultiplier((int) value);
  41. value = getSetting("powCurrentMult", 0).toFloat();
  42. if (value > 0) hlw8012.setCurrentMultiplier((int) value);
  43. value = getSetting("powVoltageMult", 0).toFloat();
  44. if (value > 0) hlw8012.setVoltageMultiplier((int) value);
  45. }
  46. void powSetExpectedActivePower(unsigned int power) {
  47. if (power > 0) {
  48. hlw8012.expectedActivePower(power);
  49. powSaveCalibration();
  50. }
  51. }
  52. void powSetExpectedCurrent(double current) {
  53. if (current > 0) {
  54. hlw8012.expectedCurrent(current);
  55. powSaveCalibration();
  56. }
  57. }
  58. void powSetExpectedVoltage(unsigned int voltage) {
  59. if (voltage > 0) {
  60. hlw8012.expectedVoltage(voltage);
  61. powSaveCalibration();
  62. }
  63. }
  64. void powReset() {
  65. hlw8012.resetMultipliers();
  66. powSaveCalibration();
  67. }
  68. // -----------------------------------------------------------------------------
  69. unsigned int getActivePower() {
  70. return hlw8012.getActivePower();
  71. }
  72. unsigned int getApparentPower() {
  73. return hlw8012.getApparentPower();
  74. }
  75. unsigned int getReactivePower() {
  76. return hlw8012.getReactivePower();
  77. }
  78. double getCurrent() {
  79. return hlw8012.getCurrent();
  80. }
  81. unsigned int getVoltage() {
  82. return hlw8012.getVoltage();
  83. }
  84. unsigned int getPowerFactor() {
  85. return (int) (100 * hlw8012.getPowerFactor());
  86. }
  87. // -----------------------------------------------------------------------------
  88. void powSetup() {
  89. // Initialize HLW8012
  90. // 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);
  91. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  92. // * currentWhen is the value in sel_pin to select current sampling
  93. // * set use_interrupts to true to use interrupts to monitor pulse widths
  94. // * leave pulse_timeout to the default value, recommended when using interrupts
  95. #if POW_USE_INTERRUPTS
  96. hlw8012.begin(POW_CF_PIN, POW_CF1_PIN, POW_SEL_PIN, POW_SEL_CURRENT, true);
  97. #else
  98. hlw8012.begin(POW_CF_PIN, POW_CF1_PIN, POW_SEL_PIN, POW_SEL_CURRENT, false, 1000000);
  99. #endif
  100. // These values are used to calculate current, voltage and power factors as per datasheet formula
  101. // These are the nominal values for the Sonoff POW resistors:
  102. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  103. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  104. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  105. hlw8012.setResistors(POW_CURRENT_R, POW_VOLTAGE_R_UP, POW_VOLTAGE_R_DOWN);
  106. // Retrieve calibration values
  107. powRetrieveCalibration();
  108. // Attach interrupts
  109. #if POW_USE_INTERRUPTS
  110. powAttachInterrupts();
  111. #endif
  112. }
  113. void powLoop() {
  114. static unsigned long last_update = 0;
  115. static unsigned char report_count = POW_REPORT_EVERY;
  116. static unsigned long power_sum = 0;
  117. static double current_sum = 0;
  118. static unsigned long voltage_sum = 0;
  119. if ((millis() - last_update > POW_UPDATE_INTERVAL) || (last_update == 0 )){
  120. last_update = millis();
  121. unsigned int power = getActivePower();
  122. unsigned int voltage = getVoltage();
  123. double current = getCurrent();
  124. unsigned int apparent = getApparentPower();
  125. unsigned int factor = getPowerFactor();
  126. unsigned int reactive = getReactivePower();
  127. power_sum += power;
  128. current_sum += current;
  129. voltage_sum += voltage;
  130. DynamicJsonBuffer jsonBuffer;
  131. JsonObject& root = jsonBuffer.createObject();
  132. root["powVisible"] = 1;
  133. root["powActivePower"] = power;
  134. root["powCurrent"] = current;
  135. root["powVoltage"] = voltage;
  136. root["powApparentPower"] = apparent;
  137. root["powReactivePower"] = reactive;
  138. root["powPowerFactor"] = factor;
  139. String output;
  140. root.printTo(output);
  141. wsSend(output.c_str());
  142. if (--report_count == 0) {
  143. power = power_sum / POW_REPORT_EVERY;
  144. current = current_sum / POW_REPORT_EVERY;
  145. voltage = voltage_sum / POW_REPORT_EVERY;
  146. apparent = current * voltage;
  147. reactive = (apparent > power) ? sqrt(apparent * apparent - power * power) : 0;
  148. factor = (apparent > 0) ? 100 * power / apparent : 100;
  149. if (factor > 100) factor = 100;
  150. mqttSend(getSetting("powPowerTopic", POW_POWER_TOPIC).c_str(), String(power).c_str());
  151. mqttSend(getSetting("powCurrentTopic", POW_CURRENT_TOPIC).c_str(), String(current).c_str());
  152. mqttSend(getSetting("powVoltageTopic", POW_VOLTAGE_TOPIC).c_str(), String(voltage).c_str());
  153. mqttSend(getSetting("powAPowerTopic", POW_APOWER_TOPIC).c_str(), String(apparent).c_str());
  154. mqttSend(getSetting("powRPowerTopic", POW_RPOWER_TOPIC).c_str(), String(reactive).c_str());
  155. mqttSend(getSetting("powPFactorTopic", POW_PFACTOR_TOPIC).c_str(), String(factor).c_str());
  156. power_sum = current_sum = voltage_sum = 0;
  157. report_count = POW_REPORT_EVERY;
  158. }
  159. // Toggle between current and voltage monitoring
  160. #if POW_USE_INTERRUPTS == 0
  161. hlw8012.toggleMode();
  162. #endif
  163. }
  164. }
  165. #endif