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