diff --git a/code/src/dht.ino b/code/src/dht.ino index f71f157a..1b8eb9d1 100644 --- a/code/src/dht.ino +++ b/code/src/dht.ino @@ -13,10 +13,21 @@ Copyright (C) 2016 by Xose Pérez DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING); +char temperature[6]; +char humidity[6]; + // ----------------------------------------------------------------------------- // DHT // ----------------------------------------------------------------------------- +char * getTemperature() { + return temperature; +} + +char * getHumidity() { + return humidity; +} + void dhtSetup() { dht.begin(); } @@ -41,8 +52,6 @@ void dhtLoop() { } else { - char temperature[6]; - char humidity[6]; dtostrf(t, 4, 1, temperature); itoa((int) h, humidity, 10); diff --git a/code/src/emon.ino b/code/src/emon.ino index ac18d4b9..f5ab31ac 100644 --- a/code/src/emon.ino +++ b/code/src/emon.ino @@ -13,6 +13,7 @@ Copyright (C) 2016 by Xose Pérez EmonLiteESP emon; double current; +char power[8]; // ----------------------------------------------------------------------------- // EMON @@ -21,6 +22,11 @@ double current; void setCurrentRatio(float value) { emon.setCurrentRatio(value); } + +char * getPower() { + return power; +} + double getCurrent() { return current; } @@ -42,6 +48,7 @@ void powerMonitorSetup() { void powerMonitorLoop() { static unsigned long next_measurement = millis(); + static bool warmup = true; static byte measurements = 0; static double max = 0; static double min = 0; @@ -49,6 +56,11 @@ void powerMonitorLoop() { if (!mqttConnected()) return; + if (warmup) { + warmup = false; + emon.warmup(); + } + if (millis() > next_measurement) { // Safety check: do not read current if relay is OFF @@ -80,10 +92,9 @@ void powerMonitorLoop() { // Send MQTT messages averaged every EMON_MEASUREMENTS if (measurements == EMON_MEASUREMENTS) { - char buffer[8]; - double power = (sum - max - min) * mainsVoltage / (measurements - 2); - sprintf(buffer, "%d", int(power)); - mqttSend((char *) getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), buffer); + double p = (sum - max - min) * mainsVoltage / (measurements - 2); + sprintf(power, "%d", int(p)); + mqttSend((char *) getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); sum = 0; measurements = 0; } diff --git a/code/src/websockets.ino b/code/src/websockets.ino index b1b3cca8..0654bd49 100644 --- a/code/src/websockets.ino +++ b/code/src/websockets.ino @@ -55,12 +55,18 @@ void webSocketStart(uint8_t num) { root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH; root["relayMode"] = getSetting("relayMode", String(RELAY_MODE)); + #if ENABLE_DHT + root["temperature"] = getTemperature(); + root["humidity"] = getHumidity(); + #endif + #if ENABLE_RF root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL)); root["rfDevice"] = getSetting("rfDevice", String(RF_DEVICE)); #endif #if ENABLE_EMON + root["power"] = getPower(); root["pwMainsVoltage"] = getSetting("pwMainsVoltage", String(EMON_MAINS_VOLTAGE)); root["pwCurrentRatio"] = getSetting("pwCurrentRatio", String(EMON_CURRENT_RATIO)); #endif