Skip to content

Commit

Permalink
Moved begin to into own method to have that logic in a single place.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasschuerg committed Nov 4, 2018
1 parent ec70958 commit 53f509d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
23 changes: 14 additions & 9 deletions InfluxDb.cpp
Expand Up @@ -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();
}

/**
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion InfluxDb.h
Expand Up @@ -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);
Expand All @@ -34,4 +33,6 @@ class Influxdb {
String _user;
String _pass;
std::list<InfluxData> prepared;

void begin();
};
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -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
```
Expand Down

0 comments on commit 53f509d

Please sign in to comment.