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.

217 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. #if EMON_PROVIDER == EMON_ADC121_PROVIDER
  77. uint8_t buffer[2];
  78. buffer[0] = ADC121_REG_CONFIG;
  79. buffer[1] = 0x00;
  80. brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY);
  81. brzo_i2c_write(buffer, 2, false);
  82. brzo_i2c_end_transaction();
  83. #endif
  84. #if WEB_SUPPORT
  85. apiRegister(EMON_APOWER_TOPIC, EMON_APOWER_TOPIC, [](char * buffer, size_t len) {
  86. if (_emonReady) {
  87. snprintf_P(buffer, len, PSTR("%d"), _emonPower);
  88. } else {
  89. buffer = NULL;
  90. }
  91. });
  92. apiRegister(EMON_CURRENT_TOPIC, EMON_CURRENT_TOPIC, [](char * buffer, size_t len) {
  93. if (_emonReady) {
  94. dtostrf(_emonCurrent, len-1, 3, buffer);
  95. } else {
  96. buffer = NULL;
  97. }
  98. });
  99. #endif // WEB_SUPPORT
  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 (warmup) {
  109. warmup = false;
  110. emon.warmup();
  111. }
  112. if (millis() > next_measurement) {
  113. int voltage = getVoltage();
  114. {
  115. double current = getCurrent();
  116. if (measurements == 0) {
  117. max = min = current;
  118. } else {
  119. if (_emonCurrent > max) max = current;
  120. if (_emonCurrent < min) min = current;
  121. }
  122. sum += current;
  123. ++measurements;
  124. DEBUG_MSG_P(PSTR("[ENERGY] Current: %sA\n"), String(current, 3).c_str());
  125. DEBUG_MSG_P(PSTR("[ENERGY] Power: %dW\n"), int(current * voltage));
  126. // Update websocket clients
  127. #if WEB_SUPPORT
  128. char buffer[100];
  129. snprintf_P(buffer, sizeof(buffer), PSTR("{\"emonVisible\": 1, \"emonApparentPower\": %d, \"emonCurrent\": %s}"), int(current * voltage), String(current, 3).c_str());
  130. wsSend(buffer);
  131. #endif
  132. }
  133. // Send MQTT messages averaged every EMON_MEASUREMENTS
  134. if (measurements == EMON_MEASUREMENTS) {
  135. // Calculate average current (removing max and min values)
  136. _emonCurrent = (sum - max - min) / (measurements - 2);
  137. _emonPower = (int) (_emonCurrent * voltage);
  138. _emonReady = true;
  139. // Calculate energy increment (ppower times time)
  140. double energy_delta = (double) _emonPower * EMON_INTERVAL * EMON_MEASUREMENTS / 1000.0 / 3600.0;
  141. // Report values to MQTT broker
  142. mqttSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), String(_emonPower).c_str());
  143. mqttSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  144. mqttSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  145. // Report values to Domoticz
  146. #if DOMOTICZ_SUPPORT
  147. {
  148. char buffer[20];
  149. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str());
  150. domoticzSend("dczPowIdx", 0, buffer);
  151. snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(energy_delta, 3).c_str());
  152. domoticzSend("dczEnergyIdx", 0, buffer);
  153. snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(_emonCurrent, 3).c_str());
  154. domoticzSend("dczCurrentIdx", 0, buffer);
  155. }
  156. #endif
  157. #if INFLUXDB_SUPPORT
  158. influxDBSend(getSetting("emonPowerTopic", EMON_APOWER_TOPIC).c_str(), _emonPower);
  159. influxDBSend(getSetting("emonCurrTopic", EMON_CURRENT_TOPIC).c_str(), String(_emonCurrent, 3).c_str());
  160. influxDBSend(getSetting("emonEnergyTopic", EMON_ENERGY_TOPIC).c_str(), String(energy_delta, 3).c_str());
  161. #endif
  162. // Reset counters
  163. sum = measurements = 0;
  164. }
  165. next_measurement += EMON_INTERVAL;
  166. }
  167. }
  168. #endif