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.

356 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[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), POWER_ENERGY_DECIMALS, buf_energy);
  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);
  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. char buffer[20];
  187. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), (int) power, buf_energy);
  188. domoticzSend("dczPowIdx", 0, buffer);
  189. domoticzSend("dczCurrentIdx", 0, buf_current);
  190. domoticzSend("dczEnergyIdx", 0, buf_energy);
  191. #if POWER_HAS_ACTIVE
  192. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), (int) _power_voltage);
  193. domoticzSend("dczVoltIdx", 0, buffer);
  194. #endif
  195. }
  196. #endif
  197. #if INFLUXDB_SUPPORT
  198. if (influxdbEnabled()) {
  199. influxDBSend(MQTT_TOPIC_CURRENT, buf_current);
  200. influxDBSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
  201. influxDBSend(MQTT_TOPIC_ENERGY_DELTA, buf_energy);
  202. influxDBSend(MQTT_TOPIC_ENERGY_TOTAL, buf_energy_total);
  203. #if POWER_HAS_ACTIVE
  204. influxDBSend(MQTT_TOPIC_POWER_ACTIVE, String((int) _power_active).c_str());
  205. influxDBSend(MQTT_TOPIC_POWER_REACTIVE, String((int) _power_reactive).c_str());
  206. influxDBSend(MQTT_TOPIC_VOLTAGE, String((int) _power_voltage).c_str());
  207. influxDBSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
  208. #endif
  209. }
  210. #endif
  211. }
  212. // -----------------------------------------------------------------------------
  213. // MAGNITUDE API
  214. // -----------------------------------------------------------------------------
  215. bool hasActivePower() {
  216. return POWER_HAS_ACTIVE;
  217. }
  218. double getCurrent() {
  219. return roundTo(_power_current, POWER_CURRENT_DECIMALS);
  220. }
  221. double getVoltage() {
  222. return roundTo(_power_voltage, POWER_VOLTAGE_DECIMALS);
  223. }
  224. double getApparentPower() {
  225. return roundTo(_power_apparent, POWER_POWER_DECIMALS);
  226. }
  227. #if POWER_HAS_ACTIVE
  228. double getActivePower() {
  229. return roundTo(_power_active, POWER_POWER_DECIMALS);
  230. }
  231. double getReactivePower() {
  232. return roundTo(_power_reactive, POWER_POWER_DECIMALS);
  233. }
  234. double getPowerFactor() {
  235. return roundTo(_power_factor, 2);
  236. }
  237. double getPowerEnergy() {
  238. roundTo(_power_energy, POWER_ENERGY_DECIMALS);
  239. }
  240. #endif
  241. // -----------------------------------------------------------------------------
  242. // PUBLIC API
  243. // -----------------------------------------------------------------------------
  244. bool powerEnabled() {
  245. return _power_enabled;
  246. }
  247. void powerEnabled(bool enabled) {
  248. if (enabled & !_power_enabled) _powerReset();
  249. _power_enabled = enabled;
  250. _powerEnabledProvider();
  251. }
  252. void powerCalibrate(unsigned char magnitude, double value) {
  253. _powerCalibrateProvider(magnitude, value);
  254. }
  255. void powerResetCalibration() {
  256. _powerResetCalibrationProvider();
  257. }
  258. void powerConfigure() {
  259. _powerConfigureProvider();
  260. }
  261. void powerSetup() {
  262. // backwards compatibility
  263. moveSetting("pwMainsVoltage", "pwrVoltage");
  264. moveSetting("emonMains", "pwrVoltage");
  265. moveSetting("emonVoltage", "pwrVoltage");
  266. moveSetting("pwCurrentRatio", "pwrRatioC");
  267. moveSetting("emonRatio", "pwrRatioC");
  268. moveSetting("powPowerMult", "pwrRatioP");
  269. moveSetting("powCurrentMult", "pwrRatioC");
  270. moveSetting("powVoltageMult", "pwrRatioV");
  271. moveSetting("powerVoltage", "pwrVoltage");
  272. moveSetting("powerRatioC", "pwrRatioC");
  273. moveSetting("powerRatioV", "pwrRatioV");
  274. moveSetting("powerRatioP", "pwrRatioP");
  275. _powerSetupProvider();
  276. // API
  277. #if WEB_SUPPORT
  278. _powerAPISetup();
  279. #endif
  280. DEBUG_MSG_P(PSTR("[POWER] POWER_PROVIDER = %d\n"), POWER_PROVIDER);
  281. }
  282. void powerLoop() {
  283. _powerLoopProvider(true);
  284. if (_power_newdata) {
  285. _power_newdata = false;
  286. _powerRead();
  287. }
  288. static unsigned long last = 0;
  289. if (millis() - last > POWER_REPORT_INTERVAL) {
  290. last = millis();
  291. _powerReport();
  292. }
  293. _powerLoopProvider(false);
  294. }
  295. #endif // POWER_PROVIDER != POWER_PROVIDER_NONE