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.

220 lines
6.6 KiB

  1. /*
  2. EMON MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if ENABLE_EMON
  6. #include <EmonLiteESP.h>
  7. #include <EEPROM.h>
  8. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  9. #include "brzo_i2c.h"
  10. #endif
  11. // ADC121 Registers
  12. #define ADC121_REG_RESULT 0x00
  13. #define ADC121_REG_ALERT 0x01
  14. #define ADC121_REG_CONFIG 0x02
  15. #define ADC121_REG_LIMITL 0x03
  16. #define ADC121_REG_LIMITH 0x04
  17. #define ADC121_REG_HYST 0x05
  18. #define ADC121_REG_CONVL 0x06
  19. #define ADC121_REG_CONVH 0x07
  20. EmonLiteESP emon;
  21. bool _emonReady = false;
  22. double _emonCurrent = 0;
  23. unsigned int _emonPower = 0;
  24. unsigned int _emonVoltage = 0;
  25. // -----------------------------------------------------------------------------
  26. // Provider
  27. // -----------------------------------------------------------------------------
  28. unsigned int currentCallback() {
  29. #if EMON_PROVIDER == EMON_ANALOG_PROVIDER
  30. return analogRead(EMON_CURRENT_PIN);
  31. #endif
  32. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  33. uint8_t buffer[2];
  34. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  35. buffer[0] = ADC121_REG_RESULT;
  36. brzo_i2c_write(buffer, 1, false);
  37. brzo_i2c_read(buffer, 2, false);
  38. brzo_i2c_end_transaction();
  39. unsigned int value;
  40. value = (buffer[0] & 0x0F) << 8;
  41. value |= buffer[1];
  42. return value;
  43. #endif
  44. }
  45. // -----------------------------------------------------------------------------
  46. // HAL
  47. // -----------------------------------------------------------------------------
  48. void setCurrentRatio(float value) {
  49. emon.setCurrentRatio(value);
  50. }
  51. unsigned int getApparentPower() {
  52. return int(getCurrent() * getVoltage());
  53. }
  54. double getCurrent() {
  55. double current = emon.getCurrent(EMON_SAMPLES);
  56. current -= EMON_CURRENT_OFFSET;
  57. if (current < 0) current = 0;
  58. return current;
  59. }
  60. unsigned int getVoltage() {
  61. return getSetting("emonVoltage", EMON_MAINS_VOLTAGE).toInt();
  62. }
  63. // -----------------------------------------------------------------------------
  64. void powerMonitorSetup() {
  65. // backwards compatibility
  66. String tmp;
  67. tmp = getSetting("pwMainsVoltage", EMON_MAINS_VOLTAGE);
  68. setSetting("emonVoltage", tmp);
  69. delSetting("pwMainsVoltage");
  70. tmp = getSetting("emonMains", EMON_MAINS_VOLTAGE);
  71. setSetting("emonVoltage", tmp);
  72. delSetting("emonMains");
  73. tmp = getSetting("pwCurrentRatio", EMON_CURRENT_RATIO);
  74. setSetting("emonRatio", tmp);
  75. delSetting("pwCurrentRatio");
  76. emon.initCurrent(
  77. currentCallback,
  78. EMON_ADC_BITS,
  79. EMON_REFERENCE_VOLTAGE,
  80. getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat()
  81. );
  82. emon.setPrecision(EMON_CURRENT_PRECISION);
  83. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  84. uint8_t buffer[2];
  85. buffer[0] = ADC121_REG_CONFIG;
  86. buffer[1] = 0x00;
  87. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  88. brzo_i2c_write(buffer, 2, false);
  89. brzo_i2c_end_transaction();
  90. #endif
  91. apiRegister(EMON_APOWER_TOPIC, EMON_APOWER_TOPIC, [](char * buffer, size_t len) {
  92. if (_emonReady) {
  93. snprintf(buffer, len, "%d", _emonPower);
  94. } else {
  95. buffer = NULL;
  96. }
  97. });
  98. apiRegister(EMON_CURRENT_TOPIC, EMON_CURRENT_TOPIC, [](char * buffer, size_t len) {
  99. if (_emonReady) {
  100. dtostrf(_emonCurrent, len-1, 3, buffer);
  101. } else {
  102. buffer = NULL;
  103. }
  104. });
  105. }
  106. void powerMonitorLoop() {
  107. static unsigned long next_measurement = millis();
  108. static bool warmup = true;
  109. static byte measurements = 0;
  110. static double max = 0;
  111. static double min = 0;
  112. static double sum = 0;
  113. if (warmup) {
  114. warmup = false;
  115. emon.warmup();
  116. }
  117. if (millis() > next_measurement) {
  118. int voltage = getVoltage();
  119. {
  120. double current = getCurrent();
  121. if (measurements == 0) {
  122. max = min = current;
  123. } else {
  124. if (_emonCurrent > max) max = current;
  125. if (_emonCurrent < min) min = current;
  126. }
  127. sum += current;
  128. ++measurements;
  129. DEBUG_MSG_P(PSTR("[ENERGY] Current: %sA\n"), String(current, 3).c_str());
  130. DEBUG_MSG_P(PSTR("[ENERGY] Power: %dW\n"), int(current * voltage));
  131. // Update websocket clients
  132. if (wsConnected()) {
  133. char text[100];
  134. sprintf_P(text, PSTR("{\"emonVisible\": 1, \"emonApparentPower\": %d, \"emonCurrent\": %s}"), int(current * voltage), String(current, 3).c_str());
  135. wsSend(text);
  136. }
  137. }
  138. // Send MQTT messages averaged every EMON_MEASUREMENTS
  139. if (measurements == EMON_MEASUREMENTS) {
  140. // Calculate average current (removing max and min values)
  141. _emonCurrent = (sum - max - min) / (measurements - 2);
  142. _emonPower = (int) (_emonCurrent * voltage);
  143. _emonReady = true;
  144. // Calculate energy increment (ppower times time)
  145. double energy_delta = (double) _emonPower * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  146. // Report values to MQTT broker
  147. mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), String(_emonPower).c_str());
  148. mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  149. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  150. // Report values to Domoticz
  151. #if ENABLE_DOMOTICZ
  152. {
  153. char buffer[20];
  154. snprintf(buffer, 20, "%d;%s", _emonPower, String(energy_delta, 3).c_str());
  155. domoticzSend("dczPowIdx", 0, buffer);
  156. snprintf(buffer, 20, "%s", String(energy_delta, 3).c_str());
  157. domoticzSend("dczEnergyIdx", 0, buffer);
  158. snprintf(buffer, 20, "%s", String(_emonCurrent, 3).c_str());
  159. domoticzSend("dczCurrentIdx", 0, buffer);
  160. }
  161. #endif
  162. #if ENABLE_INFLUXDB
  163. influxDBSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), _emonPower);
  164. influxDBSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  165. influxDBSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  166. #endif
  167. // Reset counters
  168. sum = measurements = 0;
  169. }
  170. next_measurement += EMON_INTERVAL;
  171. }
  172. }
  173. #endif