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.

328 lines
10 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. if (factor > 1) factor = 1;
  82. #endif
  83. // Filters
  84. _filter_current.add(current);
  85. #if POWER_HAS_ACTIVE
  86. _filter_apparent.add(apparent);
  87. _filter_voltage.add(voltage);
  88. _filter_active.add(active);
  89. #endif
  90. /* THERE IS A BUG HERE SOMEWHERE :)
  91. char current_buffer[10];
  92. dtostrf(current, sizeof(current_buffer)-1, POWER_CURRENT_PRECISION, current_buffer);
  93. DEBUG_MSG_P(PSTR("[POWER] Current: %sA\n"), current_buffer);
  94. DEBUG_MSG_P(PSTR("[POWER] Voltage: %sA\n"), int(voltage));
  95. DEBUG_MSG_P(PSTR("[POWER] Apparent Power: %dW\n"), int(apparent));
  96. #if POWER_HAS_ACTIVE
  97. DEBUG_MSG_P(PSTR("[POWER] Active Power: %dW\n"), int(active));
  98. DEBUG_MSG_P(PSTR("[POWER] Reactive Power: %dW\n"), int(reactive));
  99. DEBUG_MSG_P(PSTR("[POWER] Power Factor: %d%%\n"), int(100 * factor));
  100. #endif
  101. */
  102. // Update websocket clients
  103. #if WEB_SUPPORT
  104. if (wsConnected()) {
  105. DynamicJsonBuffer jsonBuffer;
  106. JsonObject& root = jsonBuffer.createObject();
  107. root["pwrVisible"] = 1;
  108. root["pwrCurrent"] = roundTo(current, POWER_CURRENT_DECIMALS);
  109. root["pwrVoltage"] = roundTo(voltage, POWER_VOLTAGE_DECIMALS);
  110. root["pwrApparent"] = roundTo(apparent, POWER_POWER_DECIMALS);
  111. #if POWER_HAS_ACTIVE
  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. #if POWER_PROVIDER & POWER_PROVIDER_EMON
  117. root["emonVisible"] = 1;
  118. #endif
  119. #if POWER_PROVIDER == POWER_PROVIDER_HLW8012
  120. root["hlwVisible"] = 1;
  121. #endif
  122. #if POWER_PROVIDER == POWER_PROVIDER_V9261F
  123. root["v9261fVisible"] = 1;
  124. #endif
  125. String output;
  126. root.printTo(output);
  127. wsSend(output.c_str());
  128. }
  129. #endif
  130. }
  131. void _powerReport() {
  132. // Get the fitered values
  133. _power_current = _filter_current.average(true);
  134. #if POWER_HAS_ACTIVE
  135. _power_apparent = _filter_apparent.average(true);
  136. _power_voltage = _filter_voltage.average(true);
  137. _power_active = _filter_active.average(true);
  138. if (_power_active > _power_apparent) _power_apparent = _power_active;
  139. #else
  140. _power_apparent = _power_current * _power_voltage;
  141. _power_active = _power_apparent;
  142. #endif
  143. _power_reactive = (_power_apparent > _power_active) ? sqrt(_power_apparent * _power_apparent - _power_active * _power_active) : 0;
  144. _power_factor = (_power_apparent > 0) ? _power_active / _power_apparent : 1;
  145. if (_power_factor > 1) _power_factor = 1;
  146. _power_ready = true;
  147. char buf_current[10];
  148. dtostrf(_power_current, -9, POWER_CURRENT_PRECISION, buf_current);
  149. double energy_delta = _power_active * POWER_ENERGY_FACTOR;
  150. char buf_energy[10];
  151. dtostrf(energy_delta, -9, POWER_CURRENT_PRECISION, buf_energy);
  152. {
  153. mqttSend(MQTT_TOPIC_CURRENT, buf_current);
  154. mqttSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  155. mqttSend(MQTT_TOPIC_ENERGY, buf_energy);
  156. #if POWER_HAS_ACTIVE
  157. mqttSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  158. mqttSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  159. mqttSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  160. mqttSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  161. #endif
  162. }
  163. #if DOMOTICZ_SUPPORT
  164. if (domoticzEnabled()) {
  165. char buffer[20];
  166. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _power_active, buf_energy);
  167. domoticzSend("dczPowIdx", 0, buffer);
  168. domoticzSend("dczCurrentIdx", 0, buf_current);
  169. domoticzSend("dczEnergyIdx", 0, buf_energy);
  170. #if POWER_HAS_ACTIVE
  171. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _power_voltage);
  172. domoticzSend("dczVoltIdx", 0, buffer);
  173. #endif
  174. }
  175. #endif
  176. #if INFLUXDB_SUPPORT
  177. if (influxdbEnabled()) {
  178. influxDBSend(MQTT_TOPIC_CURRENT, buf_current);
  179. influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  180. influxDBSend(MQTT_TOPIC_ENERGY, buf_energy);
  181. #if POWER_HAS_ACTIVE
  182. influxDBSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  183. influxDBSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  184. influxDBSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  185. influxDBSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  186. #endif
  187. }
  188. #endif
  189. }
  190. // -----------------------------------------------------------------------------
  191. // MAGNITUDE API
  192. // -----------------------------------------------------------------------------
  193. bool hasActivePower() {
  194. return POWER_HAS_ACTIVE;
  195. }
  196. double getCurrent() {
  197. return roundTo(_power_current, POWER_CURRENT_DECIMALS);
  198. }
  199. double getVoltage() {
  200. return roundTo(_power_voltage, POWER_VOLTAGE_DECIMALS);
  201. }
  202. double getApparentPower() {
  203. return roundTo(_power_apparent, POWER_POWER_DECIMALS);
  204. }
  205. double getActivePower() {
  206. return roundTo(_power_active, POWER_POWER_DECIMALS);
  207. }
  208. double getReactivePower() {
  209. return roundTo(_power_reactive, POWER_POWER_DECIMALS);
  210. }
  211. double getPowerFactor() {
  212. return roundTo(_power_factor, 2);
  213. }
  214. // -----------------------------------------------------------------------------
  215. // PUBLIC API
  216. // -----------------------------------------------------------------------------
  217. bool powerEnabled() {
  218. return _power_enabled;
  219. }
  220. void powerEnabled(bool enabled) {
  221. if (enabled & !_power_enabled) _powerReset();
  222. _power_enabled = enabled;
  223. _powerEnabledProvider();
  224. }
  225. void powerCalibrate(unsigned char magnitude, double value) {
  226. _powerCalibrateProvider(magnitude, value);
  227. }
  228. void powerResetCalibration() {
  229. _powerResetCalibrationProvider();
  230. }
  231. void powerConfigure() {
  232. _powerConfigureProvider();
  233. }
  234. void powerSetup() {
  235. // backwards compatibility
  236. moveSetting("pwMainsVoltage", "pwrVoltage");
  237. moveSetting("emonMains", "pwrVoltage");
  238. moveSetting("emonVoltage", "pwrVoltage");
  239. moveSetting("pwCurrentRatio", "pwrRatioC");
  240. moveSetting("emonRatio", "pwrRatioC");
  241. moveSetting("powPowerMult", "pwrRatioP");
  242. moveSetting("powCurrentMult", "pwrRatioC");
  243. moveSetting("powVoltageMult", "pwrRatioV");
  244. moveSetting("powerVoltage", "pwrVoltage");
  245. moveSetting("powerRatioC", "pwrRatioC");
  246. moveSetting("powerRatioV", "pwrRatioV");
  247. moveSetting("powerRatioP", "pwrRatioP");
  248. _powerSetupProvider();
  249. // API
  250. #if WEB_SUPPORT
  251. _powerAPISetup();
  252. #endif
  253. DEBUG_MSG_P(PSTR("[POWER] POWER_PROVIDER = %d\n"), POWER_PROVIDER);
  254. }
  255. void powerLoop() {
  256. _powerLoopProvider(true);
  257. if (_power_newdata) {
  258. _power_newdata = false;
  259. _powerRead();
  260. }
  261. static unsigned long last = 0;
  262. if (millis() - last > POWER_REPORT_INTERVAL) {
  263. last = millis();
  264. _powerReport();
  265. }
  266. _powerLoopProvider(false);
  267. }
  268. #endif // POWER_PROVIDER != POWER_PROVIDER_NONE