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.

118 lines
2.8 KiB

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