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.

115 lines
2.7 KiB

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