Skip to content

Commit

Permalink
Merge pull request #4 from jotzt/master
Browse files Browse the repository at this point in the history
InfluxDb authentication support
  • Loading branch information
tobiasschuerg committed Nov 4, 2018
2 parents 9f76290 + c2507dc commit ec70958
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
14 changes: 12 additions & 2 deletions InfluxDb.cpp
Expand Up @@ -29,8 +29,18 @@ void Influxdb::setDb(String db) {
http.addHeader("Content-Type", "text/plain");
}

// TODO: set db with user & password

/**
* Set the database to be used with authentication.
*/
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 );
http.addHeader("Content-Type", "text/plain");
}
/**
* Prepare a measurement to be sent.
*/
Expand Down
6 changes: 5 additions & 1 deletion InfluxDb.h
Expand Up @@ -18,6 +18,8 @@ class Influxdb {

void setDb(String db);

void setDbAuth(String db, String user, String pass);

void prepare(InfluxData data);
boolean write();

Expand All @@ -29,5 +31,7 @@ class Influxdb {
String _host;
uint16_t _port;
String _db;
String _user;
String _pass;
std::list<InfluxData> prepared;
};
};
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -7,6 +7,9 @@ Library for NodeMcu / ESP8266 (and Arduino?) for sending measurements to an Infl
#define INFLUXDB_HOST "192.168.0.32"
#define INFLUXDB_PORT "1337"
#define INFLUXDB_DATABASE "test"
//if used with authentication
#define INFLUXDB_USER "user"
#define INFLUXDB_PASS "password"

// TODO: connect to WiFi

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

// set the target database
influx.setDb(INFLUXDB_DATABASE);
// or
influx.setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS) // with authentication
```
## Sending a single measurement
Expand Down
50 changes: 50 additions & 0 deletions example/Writing_Single_with_Auth/Writing_Single_with_Auth.ino
@@ -0,0 +1,50 @@

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <InfluxDb.h>

#define INFLUXDB_HOST "192.168.0.32"
#define INFLUXDB_USER "xxx"
#define INFLUXDB_PASS "xxx"
#define WIFI_SSID "xxx"
#define WIFI_PASS "xxx"

ESP8266WiFiMulti WiFiMulti;
Influxdb influx(INFLUXDB_HOST);

void setup() {
Serial.begin(9600);
Serial.println(" ### Hello ###");

WiFiMulti.addAP(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting to WIFI");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

influx.setDbAuth("test", INFLUXDB_USER, INFLUXDB_PASS);

Serial.println("Setup done");
}


int loopCount = 0;

void loop() {
loopCount++;

InfluxData row("temperature");
row.addTag("device", "alpha");
row.addTag("sensor", "one");
row.addTag("mode", "pwm");
row.addValue("loopCount", loopCount);
row.addValue("value", random(10, 40));

influx.write(row);

delay(5000);
}

0 comments on commit ec70958

Please sign in to comment.