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.

201 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 (warmup) {
  89. warmup = false;
  90. emon.warmup();
  91. }
  92. if (millis() > next_measurement) {
  93. // Safety check: do not read current if relay is OFF
  94. // You could be monitoring another line with the current clamp...
  95. //if (!relayStatus(0)) {
  96. // _current = 0;
  97. //} else {
  98. _current = emon.getCurrent(EMON_SAMPLES);
  99. _current -= EMON_CURRENT_OFFSET;
  100. if (_current < 0) _current = 0;
  101. //}
  102. if (measurements == 0) {
  103. max = min = _current;
  104. } else {
  105. if (_current > max) max = _current;
  106. if (_current < min) min = _current;
  107. }
  108. sum += _current;
  109. ++measurements;
  110. float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat();
  111. char current[6];
  112. dtostrf(_current, 5, 2, current);
  113. DEBUG_MSG("[ENERGY] Current: %sA\n", current);
  114. DEBUG_MSG("[ENERGY] Power: %dW\n", int(_current * mainsVoltage));
  115. // Update websocket clients
  116. char text[64];
  117. sprintf_P(text, PSTR("{\"emonVisible\": 1, \"powApparentPower\": %d}"), int(_current * mainsVoltage));
  118. wsSend(text);
  119. // Send MQTT messages averaged every EMON_MEASUREMENTS
  120. if (measurements == EMON_MEASUREMENTS) {
  121. // Calculate average current (removing max and min values) and create C-string
  122. double average = (sum - max - min) / (measurements - 2);
  123. dtostrf(average, 5, 2, current);
  124. char *c = current;
  125. while ((unsigned char) *c == ' ') ++c;
  126. // Calculate average apparent power from current and create C-string
  127. _power = (int) (average * mainsVoltage);
  128. char power[6];
  129. snprintf(power, 6, "%d", _power);
  130. // Calculate energy increment (ppower times time) and create C-string
  131. double energy_inc = (double) _power * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  132. char energy_buf[11];
  133. dtostrf(energy_inc, 10, 3, energy_buf);
  134. char *e = energy_buf;
  135. while ((unsigned char) *e == ' ') ++e;
  136. // Report values to MQTT broker
  137. mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), power);
  138. mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), c);
  139. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), e);
  140. // Report values to Domoticz
  141. #if ENABLE_DOMOTICZ
  142. {
  143. char buffer[20];
  144. snprintf(buffer, 20, "%s;%s", power, e);
  145. domoticzSend("dczPowIdx", 0, buffer);
  146. snprintf(buffer, 20, "%s", e);
  147. domoticzSend("dczEnergyIdx", 0, buffer);
  148. snprintf(buffer, 20, "%s", c);
  149. domoticzSend("dczCurrentIdx", 0, buffer);
  150. }
  151. #endif
  152. // Reset counters
  153. sum = measurements = 0;
  154. }
  155. next_measurement += EMON_INTERVAL;
  156. }
  157. }
  158. #endif