Skip to content

Commit 43df725

Browse files
Merge pull request #23 from cail/v2
InfluxDB V2 HTTPS cloud access
2 parents bd4dad0 + b2b932c commit 43df725

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

InfluxDb.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,31 @@ void Influxdb::setVersion(uint16_t version){
8181
begin();
8282
}
8383

84+
#if defined(ESP8266)
85+
/**
86+
* Set servers finger print for HTTPS v2 Influx proto
87+
* @param version accepts 1 for version 1.x or 2 for version 2.x
88+
*/
89+
void Influxdb::setFingerPrint(char *fingerPrint){
90+
_fingerPrint = fingerPrint;
91+
begin();
92+
}
93+
#endif
94+
8495
void Influxdb::begin() {
8596
// TODO: recreate HttpClient on db change?
8697
if(_db_v == 2){
87-
http.begin(_host, _port, "/api/v2/write?org=" + _org + "&bucket=" + _bucket);
88-
http.addHeader("Authorization", "Token " + _token);
98+
#if defined(ESP8266)
99+
if (_port == 443) {
100+
if (_fingerPrint)
101+
client.setFingerprint(_fingerPrint);
102+
http.begin(client, _host, _port, "/api/v2/write?org=" + _org + "&bucket=" + _bucket, true);
103+
}
104+
else
105+
#endif
106+
{
107+
http.begin(_host, _port, "/api/v2/write?org=" + _org + "&bucket=" + _bucket);
108+
}
89109
} else {
90110
if (_user && _pass) {
91111
http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db);
@@ -137,6 +157,8 @@ boolean Influxdb::write(String data) {
137157
Serial.print(" --> writing to " + _db + ":\n");
138158
Serial.println(data);
139159
}
160+
if(_db_v == 2)
161+
http.addHeader("Authorization", "Token " + _token);
140162
int httpResponseCode = http.POST(data);
141163
Serial.print(" <-- Response: ");
142164
Serial.print(httpResponseCode);

InfluxDb.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
#if defined(ESP8266)
1010
#include <ESP8266HTTPClient.h>
11+
#include <WiFiClientSecure.h>
1112
#elif defined(ESP32)
1213
#include <WiFi.h>
1314
#include <HTTPClient.h>
@@ -30,6 +31,9 @@ class Influxdb {
3031
void setOrg(String org);
3132
void setToken(String token);
3233
void setPort(uint16_t port);
34+
#if defined(ESP8266)
35+
void setFingerPrint(char *fingerPrint);
36+
#endif
3337

3438
void prepare(InfluxData data);
3539
boolean write();
@@ -39,6 +43,9 @@ class Influxdb {
3943

4044
private:
4145
HTTPClient http;
46+
#if defined(ESP8266)
47+
WiFiClientSecure client;
48+
#endif
4249
String _host;
4350
uint16_t _port;
4451
String _db;
@@ -48,6 +55,9 @@ class Influxdb {
4855
String _org;
4956
String _token;
5057
uint16_t _db_v;
58+
#if defined(ESP8266)
59+
char *_fingerPrint;
60+
#endif
5161

5262
std::list<InfluxData> prepared;
5363

0 commit comments

Comments
 (0)