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.

224 lines
6.7 KiB

  1. /*
  2. EMON MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if EMON_SUPPORT
  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. #if WEB_SUPPORT
  92. apiRegister(EMON_APOWER_TOPIC, EMON_APOWER_TOPIC, [](char * buffer, size_t len) {
  93. if (_emonReady) {
  94. snprintf_P(buffer, len, PSTR("%d"), _emonPower);
  95. } else {
  96. buffer = NULL;
  97. }
  98. });
  99. apiRegister(EMON_CURRENT_TOPIC, EMON_CURRENT_TOPIC, [](char * buffer, size_t len) {
  100. if (_emonReady) {
  101. dtostrf(_emonCurrent, len-1, 3, buffer);
  102. } else {
  103. buffer = NULL;
  104. }
  105. });
  106. #endif // WEB_SUPPORT
  107. }
  108. void powerMonitorLoop() {
  109. static unsigned long next_measurement = millis();
  110. static bool warmup = true;
  111. static byte measurements = 0;
  112. static double max = 0;
  113. static double min = 0;
  114. static double sum = 0;
  115. if (warmup) {
  116. warmup = false;
  117. emon.warmup();
  118. }
  119. if (millis() > next_measurement) {
  120. int voltage = getVoltage();
  121. {
  122. double current = getCurrent();
  123. if (measurements == 0) {
  124. max = min = current;
  125. } else {
  126. if (_emonCurrent > max) max = current;
  127. if (_emonCurrent < min) min = current;
  128. }
  129. sum += current;
  130. ++measurements;
  131. DEBUG_MSG_P(PSTR("[ENERGY] Current: %sA\n"), String(current, 3).c_str());
  132. DEBUG_MSG_P(PSTR("[ENERGY] Power: %dW\n"), int(current * voltage));
  133. // Update websocket clients
  134. #if WEB_SUPPORT
  135. char text[100];
  136. sprintf_P(text, PSTR("{\"emonVisible\": 1, \"emonApparentPower\": %d, \"emonCurrent\": %s}"), int(current * voltage), String(current, 3).c_str());
  137. wsSend(text);
  138. #endif
  139. }
  140. // Send MQTT messages averaged every EMON_MEASUREMENTS
  141. if (measurements == EMON_MEASUREMENTS) {
  142. // Calculate average current (removing max and min values)
  143. _emonCurrent = (sum - max - min) / (measurements - 2);
  144. _emonPower = (int) (_emonCurrent * voltage);
  145. _emonReady = true;
  146. // Calculate energy increment (ppower times time)
  147. double energy_delta = (double) _emonPower * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  148. // Report values to MQTT broker
  149. mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), String(_emonPower).c_str());
  150. mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  151. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  152. // Report values to Domoticz
  153. #if DOMOTICZ_SUPPORT
  154. {
  155. char buffer[20];
  156. snprintf_P(buffer, strlen(buffer), PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str());
  157. domoticzSend("dczPowIdx", 0, buffer);
  158. snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(energy_delta, 3).c_str());
  159. domoticzSend("dczEnergyIdx", 0, buffer);
  160. snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(_emonCurrent, 3).c_str());
  161. domoticzSend("dczCurrentIdx", 0, buffer);
  162. }
  163. #endif
  164. #if INFLUXDB_SUPPORT
  165. influxDBSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), _emonPower);
  166. influxDBSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  167. influxDBSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  168. #endif
  169. // Reset counters
  170. sum = measurements = 0;
  171. }
  172. next_measurement += EMON_INTERVAL;
  173. }
  174. }
  175. #endif