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.

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