Skip to content

Commit 53f509d

Browse files
committed
Moved begin to into own method to have that logic in a single place.
1 parent ec70958 commit 53f509d

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

InfluxDb.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ Influxdb::Influxdb(String host, uint16_t port) {
2323
*/
2424
void Influxdb::setDb(String db) {
2525
_db = String(db);
26-
// TODO: recreate client on db change
27-
// http = new HTTPClient();
28-
http.begin(_host, _port, "/write?db=" + _db);
29-
http.addHeader("Content-Type", "text/plain");
26+
begin();
3027
}
3128

3229
/**
@@ -36,11 +33,19 @@ void Influxdb::setDbAuth(String db, String user, String pass) {
3633
_db = String(db);
3734
_user = user;
3835
_pass = pass;
39-
// TODO: recreate client on db change
40-
// http = new HTTPClient();
41-
http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db );
36+
begin();
37+
}
38+
39+
void Influxdb::begin() {
40+
// TODO: recreate HttpClient on db change?
41+
if (_user && _pass) {
42+
http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db);
43+
} else {
44+
http.begin(_host, _port, "/write?db=" + _db);
45+
}
4246
http.addHeader("Content-Type", "text/plain");
4347
}
48+
4449
/**
4550
* Prepare a measurement to be sent.
4651
*/
@@ -71,11 +76,11 @@ boolean Influxdb::write(InfluxData data) { return write(data.toString()); }
7176
* for a list of error codes.
7277
*/
7378
boolean Influxdb::write(String data) {
74-
Serial.print(" -> writing to " + _db + ":\n");
79+
Serial.print(" --> writing to " + _db + ":\n");
7580
Serial.println(data);
7681

7782
int httpResponseCode = http.POST(data);
78-
Serial.print(" <- Response: ");
83+
Serial.print(" <-- Response: ");
7984
Serial.print(httpResponseCode);
8085

8186
String response = http.getString();

InfluxDb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Influxdb {
1717
Influxdb(String host, uint16_t port = 8086);
1818

1919
void setDb(String db);
20-
2120
void setDbAuth(String db, String user, String pass);
2221

2322
void prepare(InfluxData data);
@@ -34,4 +33,6 @@ class Influxdb {
3433
String _user;
3534
String _pass;
3635
std::list<InfluxData> prepared;
36+
37+
void begin();
3738
};

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
1111
#define INFLUXDB_USER "user"
1212
#define INFLUXDB_PASS "password"
1313

14-
// TODO: connect to WiFi
14+
// connect to WiFi
1515

1616
Influxdb influx(INFLUXDB_HOST); // port defaults to 8086
17-
// or
17+
// or to use a custom port
1818
Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
1919

2020
// set the target database
2121
influx.setDb(INFLUXDB_DATABASE);
22-
// or
22+
// or use a db with auth
2323
influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS) // with authentication
2424
```
2525

0 commit comments

Comments
 (0)