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.

218 lines
6.5 KiB

7 years ago
  1. /*
  2. EMON MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if EMON_SUPPORT
  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. bool _emonReady = false;
  22. double _emonCurrent = 0;
  23. unsigned int _emonPower = 0;
  24. unsigned int _emonVoltage = 0;
  25. // -----------------------------------------------------------------------------
  26. // Provider
  27. // -----------------------------------------------------------------------------
  28. unsigned int currentCallback() {
  29. #if EMON_PROVIDER == EMON_ANALOG_PROVIDER
  30. return analogRead(EMON_CURRENT_PIN);
  31. #endif
  32. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  33. uint8_t buffer[2];
  34. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  35. buffer[0] = ADC121_REG_RESULT;
  36. brzo_i2c_write(buffer, 1, false);
  37. brzo_i2c_read(buffer, 2, false);
  38. brzo_i2c_end_transaction();
  39. unsigned int value;
  40. value = (buffer[0] & 0x0F) << 8;
  41. value |= buffer[1];
  42. return value;
  43. #endif
  44. }
  45. // -----------------------------------------------------------------------------
  46. // HAL
  47. // -----------------------------------------------------------------------------
  48. void setCurrentRatio(float value) {
  49. emon.setCurrentRatio(value);
  50. }
  51. unsigned int getApparentPower() {
  52. return int(getCurrent() * getVoltage());
  53. }
  54. double getCurrent() {
  55. double current = emon.getCurrent(EMON_SAMPLES);
  56. current -= EMON_CURRENT_OFFSET;
  57. if (current < 0) current = 0;
  58. return current;
  59. }
  60. unsigned int getVoltage() {
  61. return getSetting("emonVoltage", EMON_MAINS_VOLTAGE).toInt();
  62. }
  63. // -----------------------------------------------------------------------------
  64. void powerMonitorSetup() {
  65. // backwards compatibility
  66. String tmp;
  67. moveSetting("pwMainsVoltage", "emonVoltage");
  68. moveSetting("emonMains", "emonVoltage");
  69. moveSetting("pwCurrentRatio", "emonRatio");
  70. emon.initCurrent(
  71. currentCallback,
  72. EMON_ADC_BITS,
  73. EMON_REFERENCE_VOLTAGE,
  74. getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat()
  75. );
  76. emon.setPrecision(EMON_CURRENT_PRECISION);
  77. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  78. uint8_t buffer[2];
  79. buffer[0] = ADC121_REG_CONFIG;
  80. buffer[1] = 0x00;
  81. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  82. brzo_i2c_write(buffer, 2, false);
  83. brzo_i2c_end_transaction();
  84. #endif
  85. #if WEB_SUPPORT
  86. apiRegister(EMON_APOWER_TOPIC, EMON_APOWER_TOPIC, [](char * buffer, size_t len) {
  87. if (_emonReady) {
  88. snprintf_P(buffer, len, PSTR("%d"), _emonPower);
  89. } else {
  90. buffer = NULL;
  91. }
  92. });
  93. apiRegister(EMON_CURRENT_TOPIC, EMON_CURRENT_TOPIC, [](char * buffer, size_t len) {
  94. if (_emonReady) {
  95. dtostrf(_emonCurrent, len-1, 3, buffer);
  96. } else {
  97. buffer = NULL;
  98. }
  99. });
  100. #endif // WEB_SUPPORT
  101. }
  102. void powerMonitorLoop() {
  103. static unsigned long next_measurement = millis();
  104. static bool warmup = true;
  105. static byte measurements = 0;
  106. static double max = 0;
  107. static double min = 0;
  108. static double sum = 0;
  109. if (warmup) {
  110. warmup = false;
  111. emon.warmup();
  112. }
  113. if (millis() > next_measurement) {
  114. int voltage = getVoltage();
  115. {
  116. double current = getCurrent();
  117. if (measurements == 0) {
  118. max = min = current;
  119. } else {
  120. if (_emonCurrent > max) max = current;
  121. if (_emonCurrent < min) min = current;
  122. }
  123. sum += current;
  124. ++measurements;
  125. DEBUG_MSG_P(PSTR("[ENERGY] Current: %sA\n"), String(current, 3).c_str());
  126. DEBUG_MSG_P(PSTR("[ENERGY] Power: %dW\n"), int(current * voltage));
  127. // Update websocket clients
  128. #if WEB_SUPPORT
  129. char buffer[100];
  130. snprintf_P(buffer, sizeof(buffer), PSTR("{\"emonVisible\": 1, \"emonApparentPower\": %d, \"emonCurrent\": %s}"), int(current * voltage), String(current, 3).c_str());
  131. wsSend(buffer);
  132. #endif
  133. }
  134. // Send MQTT messages averaged every EMON_MEASUREMENTS
  135. if (measurements == EMON_MEASUREMENTS) {
  136. // Calculate average current (removing max and min values)
  137. _emonCurrent = (sum - max - min) / (measurements - 2);
  138. _emonPower = (int) (_emonCurrent * voltage);
  139. _emonReady = true;
  140. // Calculate energy increment (ppower times time)
  141. double energy_delta = (double) _emonPower * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  142. // Report values to MQTT broker
  143. mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), String(_emonPower).c_str());
  144. mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  145. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  146. // Report values to Domoticz
  147. #if DOMOTICZ_SUPPORT
  148. {
  149. char buffer[20];
  150. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str());
  151. domoticzSend("dczPowIdx", 0, buffer);
  152. snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(energy_delta, 3).c_str());
  153. domoticzSend("dczEnergyIdx", 0, buffer);
  154. snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(_emonCurrent, 3).c_str());
  155. domoticzSend("dczCurrentIdx", 0, buffer);
  156. }
  157. #endif
  158. #if INFLUXDB_SUPPORT
  159. influxDBSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), _emonPower);
  160. influxDBSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  161. influxDBSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  162. #endif
  163. // Reset counters
  164. sum = measurements = 0;
  165. }
  166. next_measurement += EMON_INTERVAL;
  167. }
  168. }
  169. #endif