Browse Source

Report driver values on websocket connection

fastled
Xose Pérez 8 years ago
parent
commit
ecfc2b8521
3 changed files with 32 additions and 6 deletions
  1. +11
    -2
      code/src/dht.ino
  2. +15
    -4
      code/src/emon.ino
  3. +6
    -0
      code/src/websockets.ino

+ 11
- 2
code/src/dht.ino View File

@ -13,10 +13,21 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING); DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
char temperature[6];
char humidity[6];
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// DHT // DHT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
char * getTemperature() {
return temperature;
}
char * getHumidity() {
return humidity;
}
void dhtSetup() { void dhtSetup() {
dht.begin(); dht.begin();
} }
@ -41,8 +52,6 @@ void dhtLoop() {
} else { } else {
char temperature[6];
char humidity[6];
dtostrf(t, 4, 1, temperature); dtostrf(t, 4, 1, temperature);
itoa((int) h, humidity, 10); itoa((int) h, humidity, 10);


+ 15
- 4
code/src/emon.ino View File

@ -13,6 +13,7 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
EmonLiteESP emon; EmonLiteESP emon;
double current; double current;
char power[8];
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// EMON // EMON
@ -21,6 +22,11 @@ double current;
void setCurrentRatio(float value) { void setCurrentRatio(float value) {
emon.setCurrentRatio(value); emon.setCurrentRatio(value);
} }
char * getPower() {
return power;
}
double getCurrent() { double getCurrent() {
return current; return current;
} }
@ -42,6 +48,7 @@ void powerMonitorSetup() {
void powerMonitorLoop() { void powerMonitorLoop() {
static unsigned long next_measurement = millis(); static unsigned long next_measurement = millis();
static bool warmup = true;
static byte measurements = 0; static byte measurements = 0;
static double max = 0; static double max = 0;
static double min = 0; static double min = 0;
@ -49,6 +56,11 @@ void powerMonitorLoop() {
if (!mqttConnected()) return; if (!mqttConnected()) return;
if (warmup) {
warmup = false;
emon.warmup();
}
if (millis() > next_measurement) { if (millis() > next_measurement) {
// Safety check: do not read current if relay is OFF // Safety check: do not read current if relay is OFF
@ -80,10 +92,9 @@ void powerMonitorLoop() {
// Send MQTT messages averaged every EMON_MEASUREMENTS // Send MQTT messages averaged every EMON_MEASUREMENTS
if (measurements == 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; sum = 0;
measurements = 0; measurements = 0;
} }


+ 6
- 0
code/src/websockets.ino View File

@ -55,12 +55,18 @@ void webSocketStart(uint8_t num) {
root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH; root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH;
root["relayMode"] = getSetting("relayMode", String(RELAY_MODE)); root["relayMode"] = getSetting("relayMode", String(RELAY_MODE));
#if ENABLE_DHT
root["temperature"] = getTemperature();
root["humidity"] = getHumidity();
#endif
#if ENABLE_RF #if ENABLE_RF
root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL)); root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL));
root["rfDevice"] = getSetting("rfDevice", String(RF_DEVICE)); root["rfDevice"] = getSetting("rfDevice", String(RF_DEVICE));
#endif #endif
#if ENABLE_EMON #if ENABLE_EMON
root["power"] = getPower();
root["pwMainsVoltage"] = getSetting("pwMainsVoltage", String(EMON_MAINS_VOLTAGE)); root["pwMainsVoltage"] = getSetting("pwMainsVoltage", String(EMON_MAINS_VOLTAGE));
root["pwCurrentRatio"] = getSetting("pwCurrentRatio", String(EMON_CURRENT_RATIO)); root["pwCurrentRatio"] = getSetting("pwCurrentRatio", String(EMON_CURRENT_RATIO));
#endif #endif


Loading…
Cancel
Save