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.

307 lines
9.3 KiB

  1. /*
  2. POWER MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  6. // -----------------------------------------------------------------------------
  7. // MODULE GLOBALS AND CACHE
  8. // -----------------------------------------------------------------------------
  9. #include "power.h"
  10. #include <Hash.h>
  11. #include <ArduinoJson.h>
  12. bool _power_enabled = false;
  13. bool _power_ready = false;
  14. bool _power_newdata = false;
  15. double _power_current = 0;
  16. double _power_voltage = 0;
  17. double _power_apparent = 0;
  18. MedianFilter _filter_current = MedianFilter(POWER_REPORT_BUFFER);
  19. #if POWER_HAS_ACTIVE
  20. double _power_active = 0;
  21. double _power_reactive = 0;
  22. double _power_factor = 0;
  23. MedianFilter _filter_voltage = MedianFilter(POWER_REPORT_BUFFER);
  24. MedianFilter _filter_active = MedianFilter(POWER_REPORT_BUFFER);
  25. MedianFilter _filter_apparent = MedianFilter(POWER_REPORT_BUFFER);
  26. #endif
  27. // -----------------------------------------------------------------------------
  28. // PRIVATE METHODS
  29. // -----------------------------------------------------------------------------
  30. #if WEB_SUPPORT
  31. void _powerAPISetup() {
  32. apiRegister(MQTT_TOPIC_CURRENT, MQTT_TOPIC_CURRENT, [](char * buffer, size_t len) {
  33. if (_power_ready) {
  34. dtostrf(getCurrent(), len-1, POWER_CURRENT_PRECISION, buffer);
  35. } else {
  36. buffer = NULL;
  37. }
  38. });
  39. apiRegister(MQTT_TOPIC_VOLTAGE, MQTT_TOPIC_VOLTAGE, [](char * buffer, size_t len) {
  40. if (_power_ready) {
  41. snprintf_P(buffer, len, PSTR("%d"), getVoltage());
  42. } else {
  43. buffer = NULL;
  44. }
  45. });
  46. apiRegister(MQTT_TOPIC_POWER_APPARENT, MQTT_TOPIC_POWER_APPARENT, [](char * buffer, size_t len) {
  47. if (_power_ready) {
  48. snprintf_P(buffer, len, PSTR("%d"), getApparentPower());
  49. } else {
  50. buffer = NULL;
  51. }
  52. });
  53. #if POWER_HAS_ACTIVE
  54. apiRegister(MQTT_TOPIC_POWER_ACTIVE, MQTT_TOPIC_POWER_ACTIVE, [](char * buffer, size_t len) {
  55. if (_power_ready) {
  56. snprintf_P(buffer, len, PSTR("%d"), getActivePower());
  57. } else {
  58. buffer = NULL;
  59. }
  60. });
  61. #endif
  62. }
  63. #endif // WEB_SUPPORT
  64. void _powerReset() {
  65. _filter_current.reset();
  66. #if POWER_HAS_ACTIVE
  67. _filter_apparent.reset();
  68. _filter_voltage.reset();
  69. _filter_active.reset();
  70. #endif
  71. }
  72. void _powerRead() {
  73. // Get instantaneous values from HAL
  74. double current = _powerCurrent();
  75. double voltage = _powerVoltage();
  76. double apparent = _powerApparentPower();
  77. #if POWER_HAS_ACTIVE
  78. double active = _powerActivePower();
  79. double reactive = (apparent > active) ? sqrt(apparent * apparent - active * active) : 0;
  80. double factor = (apparent > 0) ? active / apparent : 1;
  81. #endif
  82. // Filters
  83. _filter_current.add(current);
  84. #if POWER_HAS_ACTIVE
  85. _filter_apparent.add(apparent);
  86. _filter_voltage.add(voltage);
  87. _filter_active.add(active);
  88. #endif
  89. /* THERE IS A BUG HERE SOMEWHERE :)
  90. char current_buffer[10];
  91. dtostrf(current, sizeof(current_buffer)-1, POWER_CURRENT_PRECISION, current_buffer);
  92. DEBUG_MSG_P(PSTR("[POWER] Current: %sA\n"), current_buffer);
  93. DEBUG_MSG_P(PSTR("[POWER] Voltage: %sA\n"), int(voltage));
  94. DEBUG_MSG_P(PSTR("[POWER] Apparent Power: %dW\n"), int(apparent));
  95. #if POWER_HAS_ACTIVE
  96. DEBUG_MSG_P(PSTR("[POWER] Active Power: %dW\n"), int(active));
  97. DEBUG_MSG_P(PSTR("[POWER] Reactive Power: %dW\n"), int(reactive));
  98. DEBUG_MSG_P(PSTR("[POWER] Power Factor: %d%%\n"), int(100 * factor));
  99. #endif
  100. */
  101. // Update websocket clients
  102. #if WEB_SUPPORT
  103. if (wsConnected()) {
  104. DynamicJsonBuffer jsonBuffer;
  105. JsonObject& root = jsonBuffer.createObject();
  106. root["pwrVisible"] = 1;
  107. root["pwrCurrent"] = roundTo(current, POWER_CURRENT_DECIMALS);
  108. root["pwrVoltage"] = roundTo(voltage, POWER_VOLTAGE_DECIMALS);
  109. root["pwrApparent"] = roundTo(apparent, POWER_POWER_DECIMALS);
  110. #if POWER_HAS_ACTIVE
  111. root["pwrFullVisible"] = 1;
  112. root["pwrActive"] = roundTo(active, POWER_POWER_DECIMALS);
  113. root["pwrReactive"] = roundTo(reactive, POWER_POWER_DECIMALS);
  114. root["pwrFactor"] = int(100 * factor);
  115. #endif
  116. String output;
  117. root.printTo(output);
  118. wsSend(output.c_str());
  119. }
  120. #endif
  121. }
  122. void _powerReport() {
  123. // Get the fitered values
  124. _power_current = _filter_current.average(true);
  125. #if POWER_HAS_ACTIVE
  126. _power_apparent = _filter_apparent.average(true);
  127. _power_voltage = _filter_voltage.average(true);
  128. _power_active = _filter_active.average(true);
  129. if (_power_active > _power_apparent) _power_apparent = _power_active;
  130. #else
  131. _power_apparent = _power_current * _power_voltage;
  132. _power_active = _power_apparent;
  133. #endif
  134. _power_reactive = (_power_apparent > _power_active) ? sqrt(_power_apparent * _power_apparent - _power_active * _power_active) : 0;
  135. _power_factor = (_power_apparent > 0) ? _power_active / _power_apparent : 1;
  136. if (_power_factor > 1) _power_factor = 1;
  137. _power_ready = true;
  138. char buf_current[10];
  139. dtostrf(_power_current, 6, POWER_CURRENT_PRECISION, buf_current);
  140. double energy_delta = _power_active * POWER_ENERGY_FACTOR;
  141. char buf_energy[10];
  142. dtostrf(energy_delta, 6, POWER_CURRENT_PRECISION, buf_energy);
  143. {
  144. mqttSend(MQTT_TOPIC_CURRENT, buf_current);
  145. mqttSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  146. mqttSend(MQTT_TOPIC_ENERGY, buf_energy);
  147. #if POWER_HAS_ACTIVE
  148. mqttSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  149. mqttSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  150. mqttSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  151. mqttSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  152. #endif
  153. }
  154. #if DOMOTICZ_SUPPORT
  155. {
  156. char buffer[20];
  157. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _power_active, buf_energy);
  158. domoticzSend("dczPowIdx", 0, buffer);
  159. domoticzSend("dczCurrentIdx", 0, buf_current);
  160. domoticzSend("dczEnergyIdx", 0, buf_energy);
  161. #if POWER_HAS_ACTIVE
  162. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _power_voltage);
  163. domoticzSend("dczVoltIdx", 0, buffer);
  164. #endif
  165. }
  166. #endif
  167. #if INFLUXDB_SUPPORT
  168. {
  169. influxDBSend(MQTT_TOPIC_CURRENT, buf_current);
  170. influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  171. influxDBSend(MQTT_TOPIC_ENERGY, buf_energy);
  172. #if POWER_HAS_ACTIVE
  173. influxDBSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  174. influxDBSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  175. influxDBSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  176. influxDBSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  177. #endif
  178. }
  179. #endif
  180. }
  181. // -----------------------------------------------------------------------------
  182. // MAGNITUDE API
  183. // -----------------------------------------------------------------------------
  184. bool hasActivePower() {
  185. return POWER_HAS_ACTIVE;
  186. }
  187. double getCurrent() {
  188. return roundTo(_power_current, POWER_CURRENT_DECIMALS);
  189. }
  190. double getVoltage() {
  191. return roundTo(_power_voltage, POWER_VOLTAGE_DECIMALS);
  192. }
  193. double getApparentPower() {
  194. return roundTo(_power_apparent, POWER_POWER_DECIMALS);
  195. }
  196. double getActivePower() {
  197. return roundTo(_power_active, POWER_POWER_DECIMALS);
  198. }
  199. double getReactivePower() {
  200. return roundTo(_power_reactive, POWER_POWER_DECIMALS);
  201. }
  202. double getPowerFactor() {
  203. return roundTo(_power_factor, 2);
  204. }
  205. // -----------------------------------------------------------------------------
  206. // PUBLIC API
  207. // -----------------------------------------------------------------------------
  208. bool powerEnabled() {
  209. return _power_enabled;
  210. }
  211. void powerEnabled(bool enabled) {
  212. if (enabled & !_power_enabled) _powerReset();
  213. _power_enabled = enabled;
  214. _powerEnabledProvider();
  215. }
  216. void powerConfigure() {
  217. _powerConfigureProvider();
  218. }
  219. void powerSetup() {
  220. // backwards compatibility
  221. moveSetting("pwMainsVoltage", "powerVoltage");
  222. moveSetting("emonMains", "powerVoltage");
  223. moveSetting("emonVoltage", "powerVoltage");
  224. moveSetting("pwCurrentRatio", "powerRatioC");
  225. moveSetting("emonRatio", "powerRatioC");
  226. moveSetting("powPowerMult", "powerRatioP");
  227. moveSetting("powCurrentMult", "powerRatioC");
  228. moveSetting("powVoltageMult", "powerRatioV");
  229. _powerSetupProvider();
  230. // API
  231. #if WEB_SUPPORT
  232. _powerAPISetup();
  233. #endif
  234. DEBUG_MSG_P(PSTR("[POWER] POWER_PROVIDER = %d\n"), POWER_PROVIDER);
  235. }
  236. void powerLoop() {
  237. _powerLoopProvider(true);
  238. if (_power_newdata) {
  239. _power_newdata = false;
  240. _powerRead();
  241. }
  242. static unsigned long last = 0;
  243. if (millis() - last > POWER_REPORT_INTERVAL) {
  244. last = millis();
  245. _powerReport();
  246. }
  247. _powerLoopProvider(false);
  248. }
  249. #endif // POWER_PROVIDER != POWER_PROVIDER_NONE