diff --git a/InfluxDb.cpp b/InfluxDb.cpp index 51bad5b..45825de 100644 --- a/InfluxDb.cpp +++ b/InfluxDb.cpp @@ -23,10 +23,7 @@ Influxdb::Influxdb(String host, uint16_t port) { */ void Influxdb::setDb(String db) { _db = String(db); - // TODO: recreate client on db change - // http = new HTTPClient(); - http.begin(_host, _port, "/write?db=" + _db); - http.addHeader("Content-Type", "text/plain"); + begin(); } /** @@ -36,11 +33,19 @@ void Influxdb::setDbAuth(String db, String user, String pass) { _db = String(db); _user = user; _pass = pass; - // TODO: recreate client on db change - // http = new HTTPClient(); - http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db ); + begin(); +} + +void Influxdb::begin() { + // TODO: recreate HttpClient on db change? + if (_user && _pass) { + http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db); + } else { + http.begin(_host, _port, "/write?db=" + _db); + } http.addHeader("Content-Type", "text/plain"); } + /** * Prepare a measurement to be sent. */ @@ -71,11 +76,11 @@ boolean Influxdb::write(InfluxData data) { return write(data.toString()); } * for a list of error codes. */ boolean Influxdb::write(String data) { - Serial.print(" -> writing to " + _db + ":\n"); + Serial.print(" --> writing to " + _db + ":\n"); Serial.println(data); int httpResponseCode = http.POST(data); - Serial.print(" <- Response: "); + Serial.print(" <-- Response: "); Serial.print(httpResponseCode); String response = http.getString(); diff --git a/InfluxDb.h b/InfluxDb.h index 2962172..9af7232 100644 --- a/InfluxDb.h +++ b/InfluxDb.h @@ -17,7 +17,6 @@ class Influxdb { Influxdb(String host, uint16_t port = 8086); void setDb(String db); - void setDbAuth(String db, String user, String pass); void prepare(InfluxData data); @@ -34,4 +33,6 @@ class Influxdb { String _user; String _pass; std::list prepared; + + void begin(); }; diff --git a/README.md b/README.md index dffc4d3..c03b888 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,15 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl #define INFLUXDB_USER "user" #define INFLUXDB_PASS "password" - // TODO: connect to WiFi + // connect to WiFi Influxdb influx(INFLUXDB_HOST); // port defaults to 8086 - // or + // or to use a custom port Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT); // set the target database influx.setDb(INFLUXDB_DATABASE); - // or + // or use a db with auth influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS) // with authentication ```