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.

209 lines
5.5 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. if (!relayStatus(0)) {
  116. _current = 0;
  117. } else {
  118. _current = emon.getCurrent(EMON_SAMPLES);
  119. _current -= EMON_CURRENT_OFFSET;
  120. if (_current < 0) _current = 0;
  121. }
  122. if (measurements == 0) {
  123. max = min = _current;
  124. } else {
  125. if (_current > max) max = _current;
  126. if (_current < min) min = _current;
  127. }
  128. sum += _current;
  129. ++measurements;
  130. float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat();
  131. char current[6];
  132. dtostrf(_current, 5, 2, current);
  133. DEBUG_MSG("[ENERGY] Current: %sA\n", current);
  134. DEBUG_MSG("[ENERGY] Power: %dW\n", int(_current * mainsVoltage));
  135. // Update websocket clients
  136. char text[20];
  137. sprintf_P(text, PSTR("{\"emonPower\": %d}"), int(_current * mainsVoltage));
  138. wsSend(text);
  139. // Send MQTT messages averaged every EMON_MEASUREMENTS
  140. if (measurements == EMON_MEASUREMENTS) {
  141. _power = (int) ((sum - max - min) * mainsVoltage / (measurements - 2));
  142. double window = (double) EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  143. _energy += _power * window;
  144. saveEnergy();
  145. sum = 0;
  146. measurements = 0;
  147. char power[6];
  148. snprintf(power, 6, "%d", _power);
  149. char energy[8];
  150. snprintf(energy, 8, "%ld", (unsigned long) _energy);
  151. mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power);
  152. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), energy);
  153. #if ENABLE_DOMOTICZ
  154. {
  155. char buffer[20];
  156. snprintf(buffer, 20, "%s;%s", power, energy);
  157. domoticzSend("dczPowIdx", 0, buffer);
  158. }
  159. #endif
  160. }
  161. next_measurement += EMON_INTERVAL;
  162. }
  163. }
  164. #endif