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