Skip to content

Commit bd4dad0

Browse files
Merge pull request #10 from davidgs/master
Added v2 support
2 parents 44f8b5a + d84fb01 commit bd4dad0

18 files changed

+343
-141
lines changed

InfluxData.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ class InfluxData {
2727
_timestamp = " " + String(seconds) + "000000000";
2828
}
2929

30-
String toString() const { return _measurement + _tags + _values + _time; }
30+
String toString() const { return _measurement + _tags + _values + _timestamp; }
3131

3232
private:
3333
String _measurement;
3434
String _tags;
3535
String _values;
3636
String _timestamp;
37+
String _bucket;
38+
String _org;
3739
};

InfluxDb.cpp

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Influxdb::Influxdb(String host, uint16_t port) {
2020

2121
/**
2222
* Set the database to be used.
23+
* @param db the Influx Database to be written to.
2324
*/
2425
void Influxdb::setDb(String db) {
2526
_db = String(db);
@@ -36,12 +37,61 @@ void Influxdb::setDbAuth(String db, String user, String pass) {
3637
begin();
3738
}
3839

40+
/**
41+
* Set the Bucket to be used v2.0 ONLY.
42+
* @param bucket the InfluxDB Bucket which must already exist
43+
*/
44+
void Influxdb::setBucket(String bucket) {
45+
_bucket = String(bucket);
46+
begin();
47+
}
48+
49+
/**
50+
* Set the influxDB port.
51+
* @param port v1.x uses 8086, v2 uses 9999
52+
*/
53+
void Influxdb::setPort(uint16 port){
54+
_port = port;
55+
begin();
56+
}
57+
/**
58+
* Set the Organization to be used v2.0 ONLY
59+
* @param org the Name of the organization unit to use which must already exist
60+
*/
61+
void Influxdb::setOrg(String org){
62+
_org = String(org);
63+
begin();
64+
}
65+
66+
/**
67+
* Set the authorization token v2.0 ONLY
68+
* @param token the Auth Token from InfluxDBv2 *required*
69+
*/
70+
void Influxdb::setToken(String token){
71+
_token = String(token);
72+
begin();
73+
}
74+
75+
/**
76+
* Set the version of InfluxDB to write to
77+
* @param version accepts 1 for version 1.x or 2 for version 2.x
78+
*/
79+
void Influxdb::setVersion(uint16_t version){
80+
_db_v = version;
81+
begin();
82+
}
83+
3984
void Influxdb::begin() {
4085
// TODO: recreate HttpClient on db change?
41-
if (_user && _pass) {
42-
http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db);
86+
if(_db_v == 2){
87+
http.begin(_host, _port, "/api/v2/write?org=" + _org + "&bucket=" + _bucket);
88+
http.addHeader("Authorization", "Token " + _token);
4389
} else {
44-
http.begin(_host, _port, "/write?db=" + _db);
90+
if (_user && _pass) {
91+
http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db);
92+
} else {
93+
http.begin(_host, _port, "/write?db=" + _db);
94+
}
4595
}
4696
http.addHeader("Content-Type", "text/plain");
4797
}
@@ -76,9 +126,17 @@ boolean Influxdb::write(InfluxData data) { return write(data.toString()); }
76126
* for a list of error codes.
77127
*/
78128
boolean Influxdb::write(String data) {
79-
Serial.print(" --> writing to " + _db + ":\n");
80-
Serial.println(data);
81-
129+
if(_db_v == 2){
130+
if(_token == NULL || _token.length() < 10){
131+
Serial.println("#####\nInvalid Access Token\n#####");
132+
return false;
133+
}
134+
Serial.print(" --> writing to host: " + _host + " Port: " + _port + " URL: /api/v2/write?org=" + _org + "&bucket=" + _bucket + ":\n");
135+
Serial.println(data);
136+
} else {
137+
Serial.print(" --> writing to " + _db + ":\n");
138+
Serial.println(data);
139+
}
82140
int httpResponseCode = http.POST(data);
83141
Serial.print(" <-- Response: ");
84142
Serial.print(httpResponseCode);

InfluxDb.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class Influxdb {
2525
void setDb(String db);
2626
void setDbAuth(String db, String user, String pass);
2727

28+
void setVersion(uint16_t version);
29+
void setBucket(String bucket);
30+
void setOrg(String org);
31+
void setToken(String token);
32+
void setPort(uint16_t port);
33+
2834
void prepare(InfluxData data);
2935
boolean write();
3036

@@ -38,6 +44,11 @@ class Influxdb {
3844
String _db;
3945
String _user;
4046
String _pass;
47+
String _bucket;
48+
String _org;
49+
String _token;
50+
uint16_t _db_v;
51+
4152
std::list<InfluxData> prepared;
4253

4354
void begin();

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
1313

1414
// connect to WiFi
1515

16-
Influxdb influx(INFLUXDB_HOST); // port defaults to 8086
16+
Influxdb influx(INFLUXDB_HOST); // port defaults to 8086, use 9999 for v2
1717
// or to use a custom port
1818
Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
1919

2020
// set the target database
2121
influx.setDb(INFLUXDB_DATABASE);
2222
// or use a db with auth
2323
influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS) // with authentication
24+
25+
// To use the v2.0 InfluxDB
26+
influx.setVersion(2);
27+
influx.setOrg("myOrganization");
28+
influx.setBucket("myBucket");
29+
influx.setToken("myToken");
30+
influx.setPort(9999);
2431
```
2532
2633
## Sending a single measurement

example/Writing_Batched/Writing_Batched.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ void setup() {
2525
Serial.println(WiFi.localIP());
2626

2727
influx.setDb("test");
28-
28+
// Uncomment the following lines to use the v2.0 InfluxDB
29+
// influx.setVersion(2);
30+
// influx.setOrg("myOrganization");
31+
// influx.setBucket("myBucket");
32+
// influx.setToken("myToken");
2933
Serial.println("Setup done");
3034
}
3135

example/Writing_Single/Writing_Single.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ void setup() {
2525
Serial.println(WiFi.localIP());
2626

2727
influx.setDb("test");
28-
28+
// Uncomment the following lines to use the v2.0 InfluxDB
29+
// influx.setVersion(2);
30+
// influx.setOrg("myOrganization");
31+
// influx.setBucket("myBucket");
32+
// influx.setToken("myToken");
2933
Serial.println("Setup done");
3034
}
3135

0 commit comments

Comments
 (0)