Browse Source

Handle initial heartbeat from main loop (#318)

fastled
Xose Pérez 7 years ago
parent
commit
6d2f4ce57f
8 changed files with 3094 additions and 3067 deletions
  1. +5
    -0
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +12
    -1
      code/espurna/espurna.ino
  4. +18
    -10
      code/espurna/influxdb.ino
  5. +0
    -3
      code/espurna/mqtt.ino
  6. +1
    -1
      code/espurna/relay.ino
  7. +3053
    -3052
      code/espurna/static/index.html.gz.h
  8. +5
    -0
      code/html/index.html

+ 5
- 0
code/espurna/config/general.h View File

@ -708,7 +708,12 @@ PROGMEM const char* const custom_reset_string[] = {
#define INFLUXDB_SUPPORT 0 // Enable InfluxDB support by default (4.38Kb)
#endif
#define INFLUXDB_ENABLED 0 // InfluxDB disabled by default
#define INFLUXDB_HOST "" // Default server
#define INFLUXDB_PORT 8086 // Default InfluxDB port
#define INFLUXDB_DATABASE "" // Default database
#define INFLUXDB_USERNAME "" // Default username
#define INFLUXDB_PASSWORD "" // Default password
// -----------------------------------------------------------------------------
// NTP


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


+ 12
- 1
code/espurna/espurna.ino View File

@ -57,8 +57,19 @@ void hardwareLoop() {
// Heartbeat
static unsigned long last_uptime = 0;
if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
static bool on_connect = true;
bool send = (last_uptime == 0);
send = send || (millis() - last_uptime > HEARTBEAT_INTERVAL);
if (mqttConnected()) {
send = send || on_connect;
} else {
on_connect = true;
}
if (send) {
last_uptime = millis();
if (mqttConnected()) on_connect = false;
heartbeat();
}


+ 18
- 10
code/espurna/influxdb.ino View File

@ -18,15 +18,20 @@ SyncClient _idb_client;
void _idbWebSocketOnSend(JsonObject& root) {
root["idbVisible"] = 1;
root["idbHost"] = getSetting("idbHost");
root["idbEnabled"] = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
root["idbHost"] = getSetting("idbHost", INFLUXDB_HOST);
root["idbPort"] = getSetting("idbPort", INFLUXDB_PORT).toInt();
root["idbDatabase"] = getSetting("idbDatabase");
root["idbUsername"] = getSetting("idbUsername");
root["idbPassword"] = getSetting("idbPassword");
root["idbDatabase"] = getSetting("idbDatabase", INFLUXDB_DATABASE);
root["idbUsername"] = getSetting("idbUsername", INFLUXDB_USERNAME);
root["idbPassword"] = getSetting("idbPassword", INFLUXDB_PASSWORD);
}
void _idbConfigure() {
_idb_enabled = getSetting("idbHost").length() > 0;
_idb_enabled = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
if (_idb_enabled && (getSetting("idbHost", INFLUXDB_HOST).length() == 0)) {
_idb_enabled = false;
setSetting("idbEnabled", 0);
}
}
// -----------------------------------------------------------------------------
@ -36,10 +41,13 @@ template<typename T> bool idbSend(const char * topic, T payload) {
if (!_idb_enabled) return true;
if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
DEBUG_MSG("[INFLUXDB] Sending\n");
char host[64];
getSetting("idbHost", INFLUXDB_HOST).toCharArray(host, sizeof(host));
int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
DEBUG_MSG("[INFLUXDB] Sending to %s:%d\n", host, port);
_idb_client.setTimeout(2);
if (!_idb_client.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) {
if (!_idb_client.connect(host, port)) {
DEBUG_MSG("[INFLUXDB] Connection failed\n");
return false;
}
@ -50,9 +58,9 @@ template<typename T> bool idbSend(const char * topic, T payload) {
char request[256];
snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s",
getSetting("idbDatabase").c_str(), getSetting("idbUsername").c_str(), getSetting("idbPassword").c_str(),
getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt(),
strlen(data), data);
getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(),
getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(),
host, port, strlen(data), data);
if (_idb_client.printf(request) > 0) {
while (_idb_client.connected() && _idb_client.available() == 0) delay(1);


+ 0
- 3
code/espurna/mqtt.ino View File

@ -256,9 +256,6 @@ void _mqttOnConnect() {
_mqtt_connected_at = millis();
#endif
// Send first Heartbeat
heartbeat();
// Clean subscriptions
mqttUnsubscribeRaw("#");


+ 1
- 1
code/espurna/relay.ino View File

@ -619,7 +619,7 @@ void relaySetupMQTT() {
#if INFLUXDB_SUPPORT
void relayInfluxDB(unsigned char id) {
if (id >= _relays.size()) return;
char buffer[10];
char buffer[20];
snprintf_P(buffer, sizeof(buffer), PSTR("%s,id=%d"), MQTT_TOPIC_RELAY, id);
idbSend(buffer, relayStatus(id) ? "1" : "0");
}


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


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

@ -847,6 +847,11 @@
<fieldset>
<div class="pure-g">
<div class="pure-u-1 pure-u-sm-1-4"><label>Enable InfluxDB</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="idbEnabled" tabindex="40" /></div>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4">Host</label>
<input class="pure-u-1 pure-u-md-3-4" name="idbHost" type="text" tabindex="41" />


Loading…
Cancel
Save