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.

203 lines
5.8 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 "brzo_i2c.h"
  8. #include <EEPROM.h>
  9. // ADC121 Registers
  10. #define ADC121_REG_RESULT 0x00
  11. #define ADC121_REG_ALERT 0x01
  12. #define ADC121_REG_CONFIG 0x02
  13. #define ADC121_REG_LIMITL 0x03
  14. #define ADC121_REG_LIMITH 0x04
  15. #define ADC121_REG_HYST 0x05
  16. #define ADC121_REG_CONVL 0x06
  17. #define ADC121_REG_CONVH 0x07
  18. EmonLiteESP emon;
  19. double _current = 0;
  20. unsigned int _power = 0;
  21. // -----------------------------------------------------------------------------
  22. // Provider
  23. // -----------------------------------------------------------------------------
  24. unsigned int currentCallback() {
  25. #if EMON_PROVIDER == EMON_ANALOG_PROVIDER
  26. return analogRead(EMON_CURRENT_PIN);
  27. #endif
  28. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  29. uint8_t buffer[2];
  30. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  31. buffer[0] = ADC121_REG_RESULT;
  32. brzo_i2c_write(buffer, 1, false);
  33. brzo_i2c_read(buffer, 2, false);
  34. brzo_i2c_end_transaction();
  35. unsigned int value;
  36. value = (buffer[0] & 0x0F) << 8;
  37. value |= buffer[1];
  38. return value;
  39. #endif
  40. }
  41. // -----------------------------------------------------------------------------
  42. // EMON
  43. // -----------------------------------------------------------------------------
  44. void setCurrentRatio(float value) {
  45. emon.setCurrentRatio(value);
  46. }
  47. unsigned int getPower() {
  48. return _power;
  49. }
  50. double getCurrent() {
  51. return _current;
  52. }
  53. void powerMonitorSetup() {
  54. // backwards compatibility
  55. String tmp;
  56. tmp = getSetting("pwMainsVoltage", EMON_MAINS_VOLTAGE);
  57. setSetting("emonMains", tmp);
  58. delSetting("pwMainsVoltage");
  59. tmp = getSetting("pwCurrentRatio", EMON_CURRENT_RATIO);
  60. setSetting("emonRatio", tmp);
  61. delSetting("pwCurrentRatio");
  62. emon.initCurrent(
  63. currentCallback,
  64. EMON_ADC_BITS,
  65. EMON_REFERENCE_VOLTAGE,
  66. getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat()
  67. );
  68. emon.setPrecision(EMON_CURRENT_PRECISION);
  69. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  70. uint8_t buffer[2];
  71. buffer[0] = ADC121_REG_CONFIG;
  72. buffer[1] = 0x00;
  73. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  74. brzo_i2c_write(buffer, 2, false);
  75. brzo_i2c_end_transaction();
  76. #endif
  77. apiRegister("/api/power", "power", [](char * buffer, size_t len) {
  78. snprintf(buffer, len, "%d", _power);
  79. });
  80. }
  81. void powerMonitorLoop() {
  82. static unsigned long next_measurement = millis();
  83. static bool warmup = true;
  84. static byte measurements = 0;
  85. static double max = 0;
  86. static double min = 0;
  87. static double sum = 0;
  88. if (!mqttConnected()) return;
  89. if (warmup) {
  90. warmup = false;
  91. emon.warmup();
  92. }
  93. if (millis() > next_measurement) {
  94. // Safety check: do not read current if relay is OFF
  95. // You could be monitoring another line with the current clamp...
  96. //if (!relayStatus(0)) {
  97. // _current = 0;
  98. //} else {
  99. _current = emon.getCurrent(EMON_SAMPLES);
  100. _current -= EMON_CURRENT_OFFSET;
  101. if (_current < 0) _current = 0;
  102. //}
  103. if (measurements == 0) {
  104. max = min = _current;
  105. } else {
  106. if (_current > max) max = _current;
  107. if (_current < min) min = _current;
  108. }
  109. sum += _current;
  110. ++measurements;
  111. float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat();
  112. char current[6];
  113. dtostrf(_current, 5, 2, current);
  114. DEBUG_MSG("[ENERGY] Current: %sA\n", current);
  115. DEBUG_MSG("[ENERGY] Power: %dW\n", int(_current * mainsVoltage));
  116. // Update websocket clients
  117. char text[64];
  118. sprintf_P(text, PSTR("{\"emonVisible\": 1, \"powApparentPower\": %d}"), int(_current * mainsVoltage));
  119. wsSend(text);
  120. // Send MQTT messages averaged every EMON_MEASUREMENTS
  121. if (measurements == EMON_MEASUREMENTS) {
  122. // Calculate average current (removing max and min values) and create C-string
  123. double average = (sum - max - min) / (measurements - 2);
  124. dtostrf(average, 5, 2, current);
  125. char *c = current;
  126. while ((unsigned char) *c == ' ') ++c;
  127. // Calculate average apparent power from current and create C-string
  128. _power = (int) (current_avgd * mainsVoltage);
  129. char power[6];
  130. snprintf(power, 6, "%d", _power);
  131. // Calculate energy increment (ppower times time) and create C-string
  132. double energy_inc = (double) _power * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  133. char energy_buf[11];
  134. dtostrf(energy_inc, 11, 3, energy_buf);
  135. char *e = energy_buf;
  136. while ((unsigned char) *e == ' ') ++e;
  137. // Report values to MQTT broker
  138. mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), power);
  139. mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), c);
  140. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), e);
  141. // Report values to Domoticz
  142. #if ENABLE_DOMOTICZ
  143. {
  144. char buffer[20];
  145. snprintf(buffer, 20, "%s;%s", power, e);
  146. domoticzSend("dczPowIdx", 0, buffer);
  147. snprintf(buffer, 20, "%s", e);
  148. domoticzSend("dczEnergyIdx", 0, buffer);
  149. snprintf(buffer, 20, "%s", c);
  150. domoticzSend("dczCurrentIdx", 0, buffer);
  151. }
  152. #endif
  153. // Reset counters
  154. sum = measurements = 0;
  155. }
  156. next_measurement += EMON_INTERVAL;
  157. }
  158. }
  159. #endif