File tree Expand file tree Collapse file tree 4 files changed +72
-3
lines changed
example/Writing_Single_with_Auth Expand file tree Collapse file tree 4 files changed +72
-3
lines changed Original file line number Diff line number Diff line change @@ -29,8 +29,18 @@ void Influxdb::setDb(String db) {
29
29
http.addHeader (" Content-Type" , " text/plain" );
30
30
}
31
31
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
+ }
34
44
/* *
35
45
* Prepare a measurement to be sent.
36
46
*/
Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ class Influxdb {
18
18
19
19
void setDb (String db);
20
20
21
+ void setDbAuth (String db, String user, String pass);
22
+
21
23
void prepare (InfluxData data);
22
24
boolean write ();
23
25
@@ -29,5 +31,7 @@ class Influxdb {
29
31
String _host;
30
32
uint16_t _port;
31
33
String _db;
34
+ String _user;
35
+ String _pass;
32
36
std::list<InfluxData> prepared;
33
- };
37
+ };
Original file line number Diff line number Diff line change @@ -7,6 +7,9 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
7
7
#define INFLUXDB_HOST "192.168.0.32"
8
8
#define INFLUXDB_PORT "1337"
9
9
#define INFLUXDB_DATABASE "test"
10
+ //if used with authentication
11
+ #define INFLUXDB_USER "user"
12
+ #define INFLUXDB_PASS "password"
10
13
11
14
// TODO: connect to WiFi
12
15
@@ -16,6 +19,8 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
16
19
17
20
// set the target database
18
21
influx.setDb(INFLUXDB_DATABASE);
22
+ // or
23
+ influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS) // with authentication
19
24
```
20
25
21
26
## Sending a single measurement
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments