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.

210 lines
5.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 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. double _energy = 0;
  22. // -----------------------------------------------------------------------------
  23. // Provider
  24. // -----------------------------------------------------------------------------
  25. unsigned int currentCallback() {
  26. #if EMON_PROVIDER == EMON_ANALOG_PROVIDER
  27. return analogRead(EMON_CURRENT_PIN);
  28. #endif
  29. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  30. uint8_t buffer[2];
  31. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  32. buffer[0] = ADC121_REG_RESULT;
  33. brzo_i2c_write(buffer, 1, false);
  34. brzo_i2c_read(buffer, 2, false);
  35. brzo_i2c_end_transaction();
  36. unsigned int value;
  37. value = (buffer[0] & 0x0F) << 8;
  38. value |= buffer[1];
  39. return value;
  40. #endif
  41. }
  42. // -----------------------------------------------------------------------------
  43. // EMON
  44. // -----------------------------------------------------------------------------
  45. void setCurrentRatio(float value) {
  46. emon.setCurrentRatio(value);
  47. }
  48. unsigned int getPower() {
  49. return _power;
  50. }
  51. double getEnergy() {
  52. return _energy;
  53. }
  54. double getCurrent() {
  55. return _current;
  56. }
  57. void retrieveEnergy() {
  58. unsigned long energy = EEPROM.read(EEPROM_POWER_COUNT + 1);
  59. energy = (energy << 8) + EEPROM.read(EEPROM_POWER_COUNT);
  60. if (energy == 0xFFFF) energy = 0;
  61. _energy = energy;
  62. }
  63. void saveEnergy() {
  64. unsigned int energy = (int) _energy;
  65. EEPROM.write(EEPROM_POWER_COUNT, energy & 0xFF);
  66. EEPROM.write(EEPROM_POWER_COUNT + 1, (energy >> 8) & 0xFF);
  67. EEPROM.commit();
  68. }
  69. void powerMonitorSetup() {
  70. // backwards compatibility
  71. String tmp;
  72. tmp = getSetting("pwMainsVoltage", EMON_MAINS_VOLTAGE);
  73. setSetting("emonMains", tmp);
  74. delSetting("pwMainsVoltage");
  75. tmp = getSetting("pwCurrentRatio", EMON_CURRENT_RATIO);
  76. setSetting("emonRatio", tmp);
  77. delSetting("pwCurrentRatio");
  78. emon.initCurrent(
  79. currentCallback,
  80. EMON_ADC_BITS,
  81. EMON_REFERENCE_VOLTAGE,
  82. getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat()
  83. );
  84. emon.setPrecision(EMON_CURRENT_PRECISION);
  85. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  86. uint8_t buffer[2];
  87. buffer[0] = ADC121_REG_CONFIG;
  88. buffer[1] = 0x00;
  89. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  90. brzo_i2c_write(buffer, 2, false);
  91. brzo_i2c_end_transaction();
  92. #endif
  93. apiRegister("/api/power", "power", [](char * buffer, size_t len) {
  94. snprintf(buffer, len, "%d", _power);
  95. });
  96. apiRegister("/api/energy", "energy", [](char * buffer, size_t len) {
  97. snprintf(buffer, len, "%ld", (unsigned long) _energy);
  98. });
  99. retrieveEnergy();
  100. }
  101. void powerMonitorLoop() {
  102. static unsigned long next_measurement = millis();
  103. static bool warmup = true;
  104. static byte measurements = 0;
  105. static double max = 0;
  106. static double min = 0;
  107. static double sum = 0;
  108. if (!mqttConnected()) return;
  109. if (warmup) {
  110. warmup = false;
  111. emon.warmup();
  112. }
  113. if (millis() > next_measurement) {
  114. // Safety check: do not read current if relay is OFF
  115. // You could be monitoring another line with the current clamp...
  116. //if (!relayStatus(0)) {
  117. // _current = 0;
  118. //} else {
  119. _current = emon.getCurrent(EMON_SAMPLES);
  120. _current -= EMON_CURRENT_OFFSET;
  121. if (_current < 0) _current = 0;
  122. //}
  123. if (measurements == 0) {
  124. max = min = _current;
  125. } else {
  126. if (_current > max) max = _current;
  127. if (_current < min) min = _current;
  128. }
  129. sum += _current;
  130. ++measurements;
  131. float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat();
  132. char current[6];
  133. dtostrf(_current, 5, 2, current);
  134. DEBUG_MSG("[ENERGY] Current: %sA\n", current);
  135. DEBUG_MSG("[ENERGY] Power: %dW\n", int(_current * mainsVoltage));
  136. // Update websocket clients
  137. char text[64];
  138. sprintf_P(text, PSTR("{\"emonVisible\": 1, \"powApparentPower\": %d}"), int(_current * mainsVoltage));
  139. wsSend(text);
  140. // Send MQTT messages averaged every EMON_MEASUREMENTS
  141. if (measurements == EMON_MEASUREMENTS) {
  142. _power = (int) ((sum - max - min) * mainsVoltage / (measurements - 2));
  143. double window = (double) EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  144. _energy += _power * window;
  145. saveEnergy();
  146. sum = 0;
  147. measurements = 0;
  148. char power[6];
  149. snprintf(power, 6, "%d", _power);
  150. char energy[8];
  151. snprintf(energy, 8, "%ld", (unsigned long) _energy);
  152. mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power);
  153. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), energy);
  154. #if ENABLE_DOMOTICZ
  155. {
  156. char buffer[20];
  157. snprintf(buffer, 20, "%s;%s", power, energy);
  158. domoticzSend("dczPowIdx", 0, buffer);
  159. }
  160. #endif
  161. }
  162. next_measurement += EMON_INTERVAL;
  163. }
  164. }
  165. #endif