Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple influxdb servers support #1638

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.light.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfbridge.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfm69.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.sensor.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.small.html.gz
Binary file not shown.
93 changes: 52 additions & 41 deletions code/espurna/influxdb.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
*/

#if INFLUXDB_SUPPORT

#ifndef MAX_IDBS
#define MAX_IDBS 5
#endif
#include "ESPAsyncTCP.h"
#include "SyncClient.h"

Expand All @@ -21,13 +23,19 @@ bool _idbWebSocketOnReceive(const char * key, JsonVariant& value) {
}

void _idbWebSocketOnSend(JsonObject& root) {
root["maxIdbs"] = MAX_IDBS;
root["idbVisible"] = 1;
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", INFLUXDB_DATABASE);
root["idbUsername"] = getSetting("idbUsername", INFLUXDB_USERNAME);
root["idbPassword"] = getSetting("idbPassword", INFLUXDB_PASSWORD);
JsonArray& idbs = root.createNestedArray("idbs");
for (byte i=0; i < MAX_IDBS; i++) {
if (!hasSetting("idbHost", i)) break;
JsonObject& influxdb = idbs.createNestedObject();
influxdb["idbHost"] = getSetting("idbHost", i, INFLUXDB_HOST);
influxdb["idbPort"] = getSetting("idbPort", i, INFLUXDB_PORT).toInt();
influxdb["idbDatabase"] = getSetting("idbDatabase", i, INFLUXDB_DATABASE);
influxdb["idbUsername"] = getSetting("idbUsername", i, INFLUXDB_USERNAME);
influxdb["idbPassword"] = getSetting("idbPassword", INFLUXDB_PASSWORD);
}
}

void _idbConfigure() {
Expand Down Expand Up @@ -55,49 +63,52 @@ bool idbSend(const char * topic, const char * payload) {

if (!_idb_enabled) return true;
if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;

String h = getSetting("idbHost", INFLUXDB_HOST);
bool success = true;
for (int i = 0; i< MAX_IDBS; i++) {
if (getSetting("idbHost" + String(i)).length() == 0) break;

String h = getSetting("idbHost" + String(i), INFLUXDB_HOST);
#if MDNS_CLIENT_SUPPORT
h = mdnsResolve(h);
#endif
char * host = strdup(h.c_str());
unsigned int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
DEBUG_MSG_P(PSTR("[INFLUXDB] Sending to %s:%u\n"), host, port);

bool success = false;

_idb_client.setTimeout(2);
if (_idb_client.connect((const char *) host, port)) {

char data[128];
snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
DEBUG_MSG_P(PSTR("[INFLUXDB] Data: %s\n"), data);

char request[256];
snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%u\r\nContent-Length: %d\r\n\r\n%s",
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);
while (_idb_client.available()) _idb_client.read();
if (_idb_client.connected()) _idb_client.stop();
success = true;
char * host = strdup(h.c_str());
unsigned int port = getSetting("idbPort" + String(i), INFLUXDB_PORT).toInt();
DEBUG_MSG_P(PSTR("[INFLUXDB] Sending to %s:%u\n"), host, port);


_idb_client.setTimeout(2);
if (_idb_client.connect((const char *) host, port)) {

char data[128];
snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname" + String(i)).c_str(), String(payload).c_str());
DEBUG_MSG_P(PSTR("[INFLUXDB] Data: %s\n"), data);

char request[256];
snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%u\r\nContent-Length: %d\r\n\r\n%s",
getSetting("idbDatabase" + String(i), INFLUXDB_DATABASE).c_str(),
getSetting("idbUsername" + String(i), INFLUXDB_USERNAME).c_str(), getSetting("idbPassword" + String(i), 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);
while (_idb_client.available()) _idb_client.read();
if (_idb_client.connected()) _idb_client.stop();
success = success & true;
} else {
DEBUG_MSG_P(PSTR("[INFLUXDB] Sent failed\n"));
success = success & false;
}

_idb_client.stop();
while (_idb_client.connected()) yield();

} else {
DEBUG_MSG_P(PSTR("[INFLUXDB] Sent failed\n"));
DEBUG_MSG_P(PSTR("[INFLUXDB] Connection failed\n"));
}

_idb_client.stop();
while (_idb_client.connected()) yield();

} else {
DEBUG_MSG_P(PSTR("[INFLUXDB] Connection failed\n"));
free(host);
}

free(host);
return success;

}

bool idbSend(const char * topic, unsigned char id, const char * payload) {
Expand Down
Loading