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.

117 lines
2.7 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. EmonLiteESP emon;
  8. double current;
  9. char power[8];
  10. // -----------------------------------------------------------------------------
  11. // EMON
  12. // -----------------------------------------------------------------------------
  13. void setCurrentRatio(float value) {
  14. emon.setCurrentRatio(value);
  15. }
  16. char * getPower() {
  17. return power;
  18. }
  19. double getCurrent() {
  20. return current;
  21. }
  22. unsigned int currentCallback() {
  23. return analogRead(EMON_CURRENT_PIN);
  24. }
  25. void powerMonitorSetup() {
  26. // backwards compatibility
  27. String tmp;
  28. tmp = getSetting("pwMainsVoltage", EMON_MAINS_VOLTAGE);
  29. setSetting("emonMains", tmp);
  30. delSetting("pwMainsVoltage");
  31. tmp = getSetting("pwCurrentRatio", EMON_CURRENT_RATIO);
  32. setSetting("emonRatio", tmp);
  33. delSetting("pwCurrentRatio");
  34. emon.initCurrent(
  35. currentCallback,
  36. EMON_ADC_BITS,
  37. EMON_REFERENCE_VOLTAGE,
  38. getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat()
  39. );
  40. emon.setPrecision(EMON_CURRENT_PRECISION);
  41. }
  42. void powerMonitorLoop() {
  43. static unsigned long next_measurement = millis();
  44. static bool warmup = true;
  45. static byte measurements = 0;
  46. static double max = 0;
  47. static double min = 0;
  48. static double sum = 0;
  49. if (!mqttConnected()) return;
  50. if (warmup) {
  51. warmup = false;
  52. emon.warmup();
  53. }
  54. if (millis() > next_measurement) {
  55. // Safety check: do not read current if relay is OFF
  56. if (!relayStatus(0)) {
  57. current = 0;
  58. } else {
  59. current = emon.getCurrent(EMON_SAMPLES);
  60. current -= EMON_CURRENT_OFFSET;
  61. if (current < 0) current = 0;
  62. }
  63. if (measurements == 0) {
  64. max = min = current;
  65. } else {
  66. if (current > max) max = current;
  67. if (current < min) min = current;
  68. }
  69. sum += current;
  70. ++measurements;
  71. float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat();
  72. //DEBUG_MSG("[ENERGY] Power now: %dW\n", int(current * mainsVoltage));
  73. // Update websocket clients
  74. char text[20];
  75. sprintf_P(text, PSTR("{\"emonPower\": %d}"), int(current * mainsVoltage));
  76. wsSend(text);
  77. // Send MQTT messages averaged every EMON_MEASUREMENTS
  78. if (measurements == EMON_MEASUREMENTS) {
  79. double p = (sum - max - min) * mainsVoltage / (measurements - 2);
  80. sprintf(power, "%d", int(p));
  81. mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power);
  82. sum = 0;
  83. measurements = 0;
  84. }
  85. next_measurement += EMON_INTERVAL;
  86. }
  87. }
  88. #endif