Browse Source

Option to show real-time values in API, returning 0 if no value read yet

fastled
Xose Pérez 7 years ago
parent
commit
2210e6382f
6 changed files with 2791 additions and 2775 deletions
  1. +2
    -1
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +20
    -22
      code/espurna/power.ino
  4. +2757
    -2752
      code/espurna/static/index.html.gz.h
  5. +1
    -0
      code/espurna/web.ino
  6. +11
    -0
      code/html/index.html

+ 2
- 1
code/espurna/config/general.h View File

@ -311,8 +311,9 @@ PROGMEM const char* const custom_reset_string[] = {
// This will only be enabled if WEB_SUPPORT is 1 (this is the default value)
#define API_ENABLED 0 // Do not enable API by default
#define API_ENABLED 0 // Do not enable API by default
#define API_BUFFER_SIZE 10 // Size of the buffer for HTTP GET API responses
#define API_REAL_TIME_VALUES 0 // Show filtered/median values by default (0 => median, 1 => real time)
// -----------------------------------------------------------------------------
// MDNS


BIN
code/espurna/data/index.html.gz View File


+ 20
- 22
code/espurna/power.ino View File

@ -17,8 +17,8 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <ArduinoJson.h>
bool _power_enabled = false;
bool _power_ready = false;
bool _power_newdata = false;
bool _power_realtime = API_REAL_TIME_VALUES;
unsigned long _power_read_interval = POWER_READ_INTERVAL;
unsigned long _power_report_interval = POWER_REPORT_INTERVAL;
@ -51,37 +51,35 @@ MedianFilter _filter_current = MedianFilter();
void _powerAPISetup() {
apiRegister(MQTT_TOPIC_CURRENT, MQTT_TOPIC_CURRENT, [](char * buffer, size_t len) {
if (_power_ready) {
dtostrf(getCurrent(), len-1, POWER_CURRENT_DECIMALS, buffer);
} else {
buffer = NULL;
}
dtostrf(_power_realtime ? _powerCurrent() : getCurrent(), 1-len, POWER_CURRENT_DECIMALS, buffer);
});
apiRegister(MQTT_TOPIC_VOLTAGE, MQTT_TOPIC_VOLTAGE, [](char * buffer, size_t len) {
if (_power_ready) {
snprintf_P(buffer, len, PSTR("%d"), getVoltage());
} else {
buffer = NULL;
}
snprintf_P(buffer, len, PSTR("%d"), (int) (_power_realtime ? _powerVoltage() : getVoltage()));
});
apiRegister(MQTT_TOPIC_POWER_APPARENT, MQTT_TOPIC_POWER_APPARENT, [](char * buffer, size_t len) {
if (_power_ready) {
snprintf_P(buffer, len, PSTR("%d"), getApparentPower());
} else {
buffer = NULL;
}
snprintf_P(buffer, len, PSTR("%d"), (int) (_power_realtime ? _powerApparentPower() : getApparentPower()));
});
#if POWER_HAS_ENERGY
apiRegister(MQTT_TOPIC_ENERGY_TOTAL, MQTT_TOPIC_ENERGY_TOTAL, [](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("%lu"), (int) (_power_realtime ? _powerEnergy() : getPowerEnergy()));
});
#endif
#if POWER_HAS_ACTIVE
apiRegister(MQTT_TOPIC_POWER_ACTIVE, MQTT_TOPIC_POWER_ACTIVE, [](char * buffer, size_t len) {
if (_power_ready) {
snprintf_P(buffer, len, PSTR("%d"), getActivePower());
} else {
buffer = NULL;
}
snprintf_P(buffer, len, PSTR("%d"), (int) (_power_realtime ? _powerActivePower() : getActivePower()));
});
apiRegister(MQTT_TOPIC_POWER_FACTOR, MQTT_TOPIC_POWER_FACTOR, [](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("%d"), (int) (100 * (_power_realtime ? _powerPowerFactor() : getPowerFactor())));
});
#endif
}
@ -195,7 +193,6 @@ void _powerReport() {
double energy_delta = power * (_power_report_interval / 1000.);
_power_energy += energy_delta;
#endif
_power_ready = true;
char buf_current[10];
char buf_energy_delta[10];
@ -323,6 +320,7 @@ void powerResetCalibration() {
}
void powerConfigure() {
_power_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
_power_read_interval = atol(getSetting("pwrReadEvery", POWER_READ_INTERVAL).c_str());
_power_report_interval = atol(getSetting("pwrReportEvery", POWER_REPORT_INTERVAL).c_str());
if (_power_read_interval < POWER_MIN_READ_INTERVAL) {


+ 2757
- 2752
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 1
- 0
code/espurna/web.ino View File

@ -565,6 +565,7 @@ void _wsStart(uint32_t client_id) {
root["apiEnabled"] = getSetting("apiEnabled", API_ENABLED).toInt() == 1;
root["apiKey"] = getSetting("apiKey");
root["apiRealTime"] = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt();


+ 11
- 0
code/html/index.html View File

@ -469,6 +469,17 @@
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="apiEnabled" /></div>
</div>
<div class="pure-g">
<div class="pure-u-1 pure-u-sm-1-4"><label for="apiRealTime">Real time API</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="apiRealTime" /></div>
<div class="pure-u-0 pure-u-md-1-2">&nbsp;</div>
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint">
By default, some magnitudes are being preprocessed and filtered to avoid spurious values.<br />
If you want to get real-time values (not preprocessed) in the API turn on this setting.
</div>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="apiKey">HTTP API Key</label>
<input name="apiKey" class="pure-u-3-4 pure-u-md-1-2" type="text" tabindex="14" />


Loading…
Cancel
Save