Skip to content

Commit ec70958

Browse files
Merge pull request #4 from jotzt/master
InfluxDb authentication support
2 parents 9f76290 + c2507dc commit ec70958

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

InfluxDb.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,18 @@ void Influxdb::setDb(String db) {
2929
http.addHeader("Content-Type", "text/plain");
3030
}
3131

32-
// TODO: set db with user & password
33-
32+
/**
33+
* Set the database to be used with authentication.
34+
*/
35+
void Influxdb::setDbAuth(String db, String user, String pass) {
36+
_db = String(db);
37+
_user = user;
38+
_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 );
42+
http.addHeader("Content-Type", "text/plain");
43+
}
3444
/**
3545
* Prepare a measurement to be sent.
3646
*/

InfluxDb.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Influxdb {
1818

1919
void setDb(String db);
2020

21+
void setDbAuth(String db, String user, String pass);
22+
2123
void prepare(InfluxData data);
2224
boolean write();
2325

@@ -29,5 +31,7 @@ class Influxdb {
2931
String _host;
3032
uint16_t _port;
3133
String _db;
34+
String _user;
35+
String _pass;
3236
std::list<InfluxData> prepared;
33-
};
37+
};

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
77
#define INFLUXDB_HOST "192.168.0.32"
88
#define INFLUXDB_PORT "1337"
99
#define INFLUXDB_DATABASE "test"
10+
//if used with authentication
11+
#define INFLUXDB_USER "user"
12+
#define INFLUXDB_PASS "password"
1013

1114
// TODO: connect to WiFi
1215

@@ -16,6 +19,8 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
1619

1720
// set the target database
1821
influx.setDb(INFLUXDB_DATABASE);
22+
// or
23+
influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS) // with authentication
1924
```
2025
2126
## Sending a single measurement
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
#include <ESP8266WiFi.h>
3+
#include <ESP8266WiFiMulti.h>
4+
#include <InfluxDb.h>
5+
6+
#define INFLUXDB_HOST "192.168.0.32"
7+
#define INFLUXDB_USER "xxx"
8+
#define INFLUXDB_PASS "xxx"
9+
#define WIFI_SSID "xxx"
10+
#define WIFI_PASS "xxx"
11+
12+
ESP8266WiFiMulti WiFiMulti;
13+
Influxdb influx(INFLUXDB_HOST);
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
Serial.println(" ### Hello ###");
18+
19+
WiFiMulti.addAP(WIFI_SSID, WIFI_PASS);
20+
Serial.print("Connecting to WIFI");
21+
while (WiFiMulti.run() != WL_CONNECTED) {
22+
Serial.print(".");
23+
delay(100);
24+
}
25+
Serial.println("WiFi connected");
26+
Serial.println("IP address: ");
27+
Serial.println(WiFi.localIP());
28+
29+
influx.setDbAuth("test", INFLUXDB_USER, INFLUXDB_PASS);
30+
31+
Serial.println("Setup done");
32+
}
33+
34+
35+
int loopCount = 0;
36+
37+
void loop() {
38+
loopCount++;
39+
40+
InfluxData row("temperature");
41+
row.addTag("device", "alpha");
42+
row.addTag("sensor", "one");
43+
row.addTag("mode", "pwm");
44+
row.addValue("loopCount", loopCount);
45+
row.addValue("value", random(10, 40));
46+
47+
influx.write(row);
48+
49+
delay(5000);
50+
}

0 commit comments

Comments
 (0)