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.

361 lines
11 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. double _power_energy = 0;
  19. MedianFilter _filter_current = MedianFilter(POWER_REPORT_BUFFER);
  20. #if POWER_HAS_ACTIVE
  21. double _power_active = 0;
  22. double _power_reactive = 0;
  23. double _power_factor = 0;
  24. MedianFilter _filter_voltage = MedianFilter(POWER_REPORT_BUFFER);
  25. MedianFilter _filter_active = MedianFilter(POWER_REPORT_BUFFER);
  26. MedianFilter _filter_apparent = MedianFilter(POWER_REPORT_BUFFER);
  27. #endif
  28. #if POWER_HAS_ENERGY
  29. double _power_last_energy = 0;
  30. #endif
  31. // -----------------------------------------------------------------------------
  32. // PRIVATE METHODS
  33. // -----------------------------------------------------------------------------
  34. #if WEB_SUPPORT
  35. void _powerAPISetup() {
  36. apiRegister(MQTT_TOPIC_CURRENT, MQTT_TOPIC_CURRENT, [](char * buffer, size_t len) {
  37. if (_power_ready) {
  38. dtostrf(getCurrent(), len-1, POWER_CURRENT_DECIMALS, buffer);
  39. } else {
  40. buffer = NULL;
  41. }
  42. });
  43. apiRegister(MQTT_TOPIC_VOLTAGE, MQTT_TOPIC_VOLTAGE, [](char * buffer, size_t len) {
  44. if (_power_ready) {
  45. snprintf_P(buffer, len, PSTR("%d"), getVoltage());
  46. } else {
  47. buffer = NULL;
  48. }
  49. });
  50. apiRegister(MQTT_TOPIC_POWER_APPARENT, MQTT_TOPIC_POWER_APPARENT, [](char * buffer, size_t len) {
  51. if (_power_ready) {
  52. snprintf_P(buffer, len, PSTR("%d"), getApparentPower());
  53. } else {
  54. buffer = NULL;
  55. }
  56. });
  57. #if POWER_HAS_ACTIVE
  58. apiRegister(MQTT_TOPIC_POWER_ACTIVE, MQTT_TOPIC_POWER_ACTIVE, [](char * buffer, size_t len) {
  59. if (_power_ready) {
  60. snprintf_P(buffer, len, PSTR("%d"), getActivePower());
  61. } else {
  62. buffer = NULL;
  63. }
  64. });
  65. #endif
  66. }
  67. #endif // WEB_SUPPORT
  68. void _powerReset() {
  69. _filter_current.reset();
  70. #if POWER_HAS_ACTIVE
  71. _filter_apparent.reset();
  72. _filter_voltage.reset();
  73. _filter_active.reset();
  74. #endif
  75. }
  76. void _powerRead() {
  77. // Get instantaneous values from HAL
  78. double current = _powerCurrent();
  79. double voltage = _powerVoltage();
  80. double apparent = _powerApparentPower();
  81. #if POWER_HAS_ACTIVE
  82. double active = _powerActivePower();
  83. double reactive = (apparent > active) ? sqrt(apparent * apparent - active * active) : 0;
  84. double factor = (apparent > 0) ? active / apparent : 1;
  85. if (factor > 1) factor = 1;
  86. #endif
  87. #if POWER_HAS_ENERGY
  88. _power_energy = _powerEnergy(); // Due to its nature this value doesn't have to be filtered
  89. #endif
  90. // Filters
  91. _filter_current.add(current);
  92. #if POWER_HAS_ACTIVE
  93. _filter_apparent.add(apparent);
  94. _filter_voltage.add(voltage);
  95. _filter_active.add(active);
  96. #endif
  97. /* THERE IS A BUG HERE SOMEWHERE :)
  98. char current_buffer[10];
  99. dtostrf(current, sizeof(current_buffer)-1, POWER_CURRENT_DECIMALS, current_buffer);
  100. DEBUG_MSG_P(PSTR("[POWER] Current: %sA\n"), current_buffer);
  101. DEBUG_MSG_P(PSTR("[POWER] Voltage: %sA\n"), int(voltage));
  102. DEBUG_MSG_P(PSTR("[POWER] Apparent Power: %dW\n"), int(apparent));
  103. #if POWER_HAS_ACTIVE
  104. DEBUG_MSG_P(PSTR("[POWER] Active Power: %dW\n"), int(active));
  105. DEBUG_MSG_P(PSTR("[POWER] Reactive Power: %dW\n"), int(reactive));
  106. DEBUG_MSG_P(PSTR("[POWER] Power Factor: %d%%\n"), int(100 * factor));
  107. #endif
  108. */
  109. // Update websocket clients
  110. #if WEB_SUPPORT
  111. if (wsConnected()) {
  112. DynamicJsonBuffer jsonBuffer;
  113. JsonObject& root = jsonBuffer.createObject();
  114. root["pwrVisible"] = 1;
  115. root["pwrCurrent"] = roundTo(current, POWER_CURRENT_DECIMALS);
  116. root["pwrVoltage"] = roundTo(voltage, POWER_VOLTAGE_DECIMALS);
  117. root["pwrApparent"] = roundTo(apparent, POWER_POWER_DECIMALS);
  118. root["pwrEnergy"] = roundTo(_power_energy, POWER_ENERGY_DECIMALS);
  119. #if POWER_HAS_ACTIVE
  120. root["pwrActive"] = roundTo(active, POWER_POWER_DECIMALS);
  121. root["pwrReactive"] = roundTo(reactive, POWER_POWER_DECIMALS);
  122. root["pwrFactor"] = int(100 * factor);
  123. #endif
  124. #if (POWER_PROVIDER == POWER_PROVIDER_EMON_ANALOG) || (POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121)
  125. root["emonVisible"] = 1;
  126. #endif
  127. #if POWER_PROVIDER == POWER_PROVIDER_HLW8012
  128. root["hlwVisible"] = 1;
  129. #endif
  130. #if POWER_PROVIDER == POWER_PROVIDER_V9261F
  131. root["v9261fVisible"] = 1;
  132. #endif
  133. #if POWER_PROVIDER == POWER_PROVIDER_ECH1560
  134. root["ech1560Visible"] = 1;
  135. #endif
  136. String output;
  137. root.printTo(output);
  138. wsSend(output.c_str());
  139. }
  140. #endif
  141. }
  142. void _powerReport() {
  143. // Get the fitered values
  144. _power_current = _filter_current.average(true);
  145. #if POWER_HAS_ACTIVE
  146. _power_apparent = _filter_apparent.average(true);
  147. _power_voltage = _filter_voltage.average(true);
  148. _power_active = _filter_active.average(true);
  149. if (_power_active > _power_apparent) _power_apparent = _power_active;
  150. _power_reactive = (_power_apparent > _power_active) ? sqrt(_power_apparent * _power_apparent - _power_active * _power_active) : 0;
  151. _power_factor = (_power_apparent > 0) ? _power_active / _power_apparent : 1;
  152. if (_power_factor > 1) _power_factor = 1;
  153. double power = _power_active;
  154. #else
  155. _power_apparent = _power_current * _power_voltage;
  156. double power = _power_apparent;
  157. #endif
  158. #if POWER_HAS_ENERGY
  159. double energy_delta = _power_energy - _power_last_energy;
  160. _power_last_energy = _power_energy;
  161. #else
  162. double energy_delta = power * (POWER_REPORT_INTERVAL / 1000.);
  163. _power_energy += energy_delta;
  164. #endif
  165. _power_ready = true;
  166. char buf_current[10];
  167. char buf_energy_delta[10];
  168. char buf_energy_total[10];
  169. dtostrf(_power_current, 1-sizeof(buf_current), POWER_CURRENT_DECIMALS, buf_current);
  170. dtostrf(energy_delta * POWER_ENERGY_FACTOR, 1-sizeof(buf_energy_delta), POWER_ENERGY_DECIMALS, buf_energy_delta);
  171. dtostrf(_power_energy * POWER_ENERGY_FACTOR, 1-sizeof(buf_energy_total), POWER_ENERGY_DECIMALS, buf_energy_total);
  172. {
  173. mqttSend(MQTT_TOPIC_CURRENT, buf_current);
  174. mqttSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  175. mqttSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy_delta);
  176. mqttSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total);
  177. #if POWER_HAS_ACTIVE
  178. mqttSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  179. mqttSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  180. mqttSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  181. mqttSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  182. #endif
  183. }
  184. #if DOMOTICZ_SUPPORT
  185. if (domoticzEnabled()) {
  186. // Domoticz expects energy in kWh
  187. char buf_energy_kwh[10];
  188. dtostrf(energy_delta * POWER_ENERGY_FACTOR_KWH, 1-sizeof(buf_energy_kwh), POWER_ENERGY_DECIMALS_KWH, buf_energy_kwh);
  189. char buffer[20];
  190. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), (int) power, buf_energy_kwh);
  191. domoticzSend("dczPowIdx", 0, buffer);
  192. domoticzSend("dczCurrentIdx", 0, buf_current);
  193. domoticzSend("dczEnergyIdx", 0, buf_energy_kwh);
  194. #if POWER_HAS_ACTIVE
  195. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), (int) _power_voltage);
  196. domoticzSend("dczVoltIdx", 0, buffer);
  197. #endif
  198. }
  199. #endif
  200. #if INFLUXDB_SUPPORT
  201. if (influxdbEnabled()) {
  202. influxDBSend(MQTT_TOPIC_CURRENT, buf_current);
  203. influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  204. influxDBSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy_delta);
  205. influxDBSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total);
  206. #if POWER_HAS_ACTIVE
  207. influxDBSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  208. influxDBSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  209. influxDBSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  210. influxDBSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  211. #endif
  212. }
  213. #endif
  214. }
  215. // -----------------------------------------------------------------------------
  216. // MAGNITUDE API
  217. // -----------------------------------------------------------------------------
  218. bool hasActivePower() {
  219. return POWER_HAS_ACTIVE;
  220. }
  221. double getCurrent() {
  222. return roundTo(_power_current, POWER_CURRENT_DECIMALS);
  223. }
  224. double getVoltage() {
  225. return roundTo(_power_voltage, POWER_VOLTAGE_DECIMALS);
  226. }
  227. double getApparentPower() {
  228. return roundTo(_power_apparent, POWER_POWER_DECIMALS);
  229. }
  230. #if POWER_HAS_ACTIVE
  231. double getActivePower() {
  232. return roundTo(_power_active, POWER_POWER_DECIMALS);
  233. }
  234. double getReactivePower() {
  235. return roundTo(_power_reactive, POWER_POWER_DECIMALS);
  236. }
  237. double getPowerFactor() {
  238. return roundTo(_power_factor, 2);
  239. }
  240. double getPowerEnergy() {
  241. roundTo(_power_energy, POWER_ENERGY_DECIMALS);
  242. }
  243. #endif
  244. // -----------------------------------------------------------------------------
  245. // PUBLIC API
  246. // -----------------------------------------------------------------------------
  247. bool powerEnabled() {
  248. return _power_enabled;
  249. }
  250. void powerEnabled(bool enabled) {
  251. if (enabled & !_power_enabled) _powerReset();
  252. _power_enabled = enabled;
  253. _powerEnabledProvider();
  254. }
  255. void powerCalibrate(unsigned char magnitude, double value) {
  256. _powerCalibrateProvider(magnitude, value);
  257. }
  258. void powerResetCalibration() {
  259. _powerResetCalibrationProvider();
  260. }
  261. void powerConfigure() {
  262. _powerConfigureProvider();
  263. }
  264. void powerSetup() {
  265. // backwards compatibility
  266. moveSetting("pwMainsVoltage", "pwrVoltage");
  267. moveSetting("emonMains", "pwrVoltage");
  268. moveSetting("emonVoltage", "pwrVoltage");
  269. moveSetting("pwCurrentRatio", "pwrRatioC");
  270. moveSetting("emonRatio", "pwrRatioC");
  271. moveSetting("powPowerMult", "pwrRatioP");
  272. moveSetting("powCurrentMult", "pwrRatioC");
  273. moveSetting("powVoltageMult", "pwrRatioV");
  274. moveSetting("powerVoltage", "pwrVoltage");
  275. moveSetting("powerRatioC", "pwrRatioC");
  276. moveSetting("powerRatioV", "pwrRatioV");
  277. moveSetting("powerRatioP", "pwrRatioP");
  278. _powerSetupProvider();
  279. // API
  280. #if WEB_SUPPORT
  281. _powerAPISetup();
  282. #endif
  283. DEBUG_MSG_P(PSTR("[POWER] POWER_PROVIDER = %d\n"), POWER_PROVIDER);
  284. }
  285. void powerLoop() {
  286. _powerLoopProvider(true);
  287. if (_power_newdata) {
  288. _power_newdata = false;
  289. _powerRead();
  290. }
  291. static unsigned long last = 0;
  292. if (millis() - last > POWER_REPORT_INTERVAL) {
  293. last = millis();
  294. _powerReport();
  295. }
  296. _powerLoopProvider(false);
  297. }
  298. #endif // POWER_PROVIDER != POWER_PROVIDER_NONE