Browse Source

Fix bug due to strange behaviour in the ino2cpp converter

fastled
Xose Pérez 8 years ago
parent
commit
48e9f78ca2
2 changed files with 115 additions and 115 deletions
  1. +36
    -36
      code/src/dht.ino
  2. +79
    -79
      code/src/emon.ino

+ 36
- 36
code/src/dht.ino View File

@ -9,59 +9,59 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
#if ENABLE_DHT #if ENABLE_DHT
#include <DHT.h>
#include <DHT.h>
DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
// -----------------------------------------------------------------------------
// DHT
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// DHT
// -----------------------------------------------------------------------------
void dhtSetup() {
dht.begin();
}
void dhtLoop() {
void dhtSetup() {
dht.begin();
}
if (!mqttConnected()) return;
void dhtLoop() {
// Check if we should read new data
static unsigned long last_update = 0;
if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();
if (!mqttConnected()) return;
// Read sensor data
double h = dht.readHumidity();
double t = dht.readTemperature();
// Check if we should read new data
static unsigned long last_update = 0;
if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();
// Check if readings are valid
if (isnan(h) || isnan(t)) {
// Read sensor data
double h = dht.readHumidity();
double t = dht.readTemperature();
DEBUG_MSG("[DHT] Error reading sensor\n");
// Check if readings are valid
if (isnan(h) || isnan(t)) {
} else {
DEBUG_MSG("[DHT] Error reading sensor\n");
char temperature[6];
char humidity[6];
dtostrf(t, 4, 1, temperature);
itoa((int) h, humidity, 10);
} else {
DEBUG_MSG("[DHT] Temperature: %s\n", temperature);
DEBUG_MSG("[DHT] Humidity: %s\n", humidity);
char temperature[6];
char humidity[6];
dtostrf(t, 4, 1, temperature);
itoa((int) h, humidity, 10);
// Send MQTT messages
mqttSend((char *) getSetting("dhtTemperatureTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
mqttSend((char *) getSetting("dhtHumidityTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
DEBUG_MSG("[DHT] Temperature: %s\n", temperature);
DEBUG_MSG("[DHT] Humidity: %s\n", humidity);
// Update websocket clients
char buffer[20];
sprintf_P(buffer, PSTR("{\"temperature\": %s, \"humidity\": %s}"), temperature, humidity);
webSocketSend(buffer);
// Send MQTT messages
mqttSend((char *) getSetting("dhtTemperatureTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
mqttSend((char *) getSetting("dhtHumidityTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
}
// Update websocket clients
char buffer[20];
sprintf_P(buffer, PSTR("{\"temperature\": %s, \"humidity\": %s}"), temperature, humidity);
webSocketSend(buffer);
} }
} }
}
#endif #endif

+ 79
- 79
code/src/emon.ino View File

@ -9,89 +9,89 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
#if ENABLE_EMON #if ENABLE_EMON
#include <EmonLiteESP.h>
EmonLiteESP emon;
double current;
// -----------------------------------------------------------------------------
// EMON
// -----------------------------------------------------------------------------
#include <EmonLiteESP.h>
EmonLiteESP emon;
double current;
// -----------------------------------------------------------------------------
// EMON
// -----------------------------------------------------------------------------
void setCurrentRatio(float value) {
emon.setCurrentRatio(value);
}
double getCurrent() {
return current;
}
unsigned int currentCallback() {
return analogRead(EMON_CURRENT_PIN);
}
void powerMonitorSetup() {
emon.initCurrent(
currentCallback,
EMON_ADC_BITS,
EMON_REFERENCE_VOLTAGE,
getSetting("pwCurrentRatio", String(EMON_CURRENT_RATIO)).toFloat()
);
emon.setPrecision(EMON_CURRENT_PRECISION);
}
void powerMonitorLoop() {
static unsigned long next_measurement = millis();
static byte measurements = 0;
static double max = 0;
static double min = 0;
static double sum = 0;
if (!mqttConnected()) return;
if (millis() > next_measurement) {
// Safety check: do not read current if relay is OFF
if (!digitalRead(RELAY_PIN)) {
current = 0;
} else {
current = emon.getCurrent(EMON_SAMPLES);
current -= EMON_CURRENT_OFFSET;
if (current < 0) current = 0;
}
void setCurrentRatio(float value) {
emon.setCurrentRatio(value);
}
double getCurrent() {
return current;
}
if (measurements == 0) {
max = min = current;
} else {
if (current > max) max = current;
if (current < min) min = current;
}
sum += current;
++measurements;
float mainsVoltage = getSetting("pwMainsVoltage", String(EMON_MAINS_VOLTAGE)).toFloat();
//DEBUG_MSG("[ENERGY] Power now: %dW\n", int(current * mainsVoltage));
// Update websocket clients
char text[20];
sprintf_P(text, PSTR("{\"power\": %d}"), int(current * mainsVoltage));
webSocketSend(text);
// 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);
sum = 0;
measurements = 0;
}
unsigned int currentCallback() {
return analogRead(EMON_CURRENT_PIN);
}
next_measurement += EMON_INTERVAL;
void powerMonitorSetup() {
emon.initCurrent(
currentCallback,
EMON_ADC_BITS,
EMON_REFERENCE_VOLTAGE,
getSetting("pwCurrentRatio", String(EMON_CURRENT_RATIO)).toFloat()
);
emon.setPrecision(EMON_CURRENT_PRECISION);
} }
void powerMonitorLoop() {
static unsigned long next_measurement = millis();
static byte measurements = 0;
static double max = 0;
static double min = 0;
static double sum = 0;
if (!mqttConnected()) return;
if (millis() > next_measurement) {
// Safety check: do not read current if relay is OFF
if (!digitalRead(RELAY_PIN)) {
current = 0;
} else {
current = emon.getCurrent(EMON_SAMPLES);
current -= EMON_CURRENT_OFFSET;
if (current < 0) current = 0;
}
if (measurements == 0) {
max = min = current;
} else {
if (current > max) max = current;
if (current < min) min = current;
}
sum += current;
++measurements;
float mainsVoltage = getSetting("pwMainsVoltage", String(EMON_MAINS_VOLTAGE)).toFloat();
//DEBUG_MSG("[ENERGY] Power now: %dW\n", int(current * mainsVoltage));
// Update websocket clients
char text[20];
sprintf_P(text, PSTR("{\"power\": %d}"), int(current * mainsVoltage));
webSocketSend(text);
// 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);
sum = 0;
measurements = 0;
}
next_measurement += EMON_INTERVAL;
}
}
}
#endif #endif

Loading…
Cancel
Save