Skip to content

Commit fc14731

Browse files
committed
Merge branch 'master' into fix/timestamp
2 parents 722c6a5 + 6e47386 commit fc14731

File tree

12 files changed

+161
-118
lines changed

12 files changed

+161
-118
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Changelog
22

33
## Version 3.3.0 (in progress)
4-
- [FIX] More precice default timestamp generating, up to microseconds
5-
4+
- [NEW] Added possibility skip server certification validation (`setInsecure()` method)
5+
- [NEW] Added possibility to query flux on secured InfuxDB 1.8 using V1 approach
6+
- [NEW] `validateConnection()` can be used also for the [forward compatibility](https://docs.influxdata.com/influxdb/latest/tools/api/#influxdb-2-0-api-compatibility-endpoints) connection to InfluxDB 1.8
7+
- [FIX] More precice default timestamp generating, up to microseconds
8+
- [FIX] Debug compilation error
9+
- [FIX] SecureBatchWrite compile error
10+
611
## Version 3.2.0 (2020-06-09)
712
- [NEW] Added possibility to read data from InfluxDB using Flux queries
813
- [NEW] `timeSync` utility function for synchronous time synchronization using NTP

examples/SecureBatchWrite/SecureBatchWrite.ino

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ESP8266WiFiMulti wifiMulti;
2222
#define WIFI_AUTH_OPEN ENC_TYPE_NONE
2323
#endif
2424

25-
#include <InfluxDbClient.h
25+
#include <InfluxDbClient.h>
2626
#include <InfluxDbCloud.h>
2727

2828
// WiFi AP SSID
@@ -44,6 +44,10 @@ ESP8266WiFiMulti wifiMulti;
4444
// Japanesse: "JST-9"
4545
// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3"
4646
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
47+
// NTP servers the for time syncronozation.
48+
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
49+
#define NTP_SERVER1 "pool.ntp.org"
50+
#define NTP_SERVER2 "time.nis.gov"
4751
#define WRITE_PRECISION WritePrecision::S
4852
#define MAX_BATCH_SIZE 10
4953
#define WRITE_BUFFER_SIZE 30
@@ -76,9 +80,8 @@ void setup() {
7680
sensorStatus.addTag("SSID", WiFi.SSID());
7781

7882
// Accurate time is necessary for certificate validation and writing in batches
79-
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
8083
// Syncing progress and the time will be printed to Serial.
81-
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
84+
timeSync(TZ_INFO, NTP_SERVER1, NTP_SERVER2);
8285

8386
// Check server connection
8487
if (client.validateConnection()) {
@@ -89,22 +92,22 @@ void setup() {
8992
Serial.println(client.getLastErrorMessage());
9093
}
9194

92-
//Enable messages batching and retry buffer
95+
// Enable messages batching and retry buffer
9396
client.setWriteOptions(WRITE_PRECISION, MAX_BATCH_SIZE, WRITE_BUFFER_SIZE);
9497
}
9598

9699
void loop() {
97100
// Sync time for batching once per hour
98101
if (iterations++ >= 360) {
99-
timeSync();
102+
timeSync(TZ_INFO, NTP_SERVER1, NTP_SERVER2);
100103
iterations = 0;
101104
}
102105

103-
//Report networks (low priority data) just in case we successfully wrote the previous batch
106+
// Report networks (low priority data) just in case we successfully wrote the previous batch
104107
if (client.isBufferEmpty()) {
105108
// Report all the detected wifi networks
106109
int networks = WiFi.scanNetworks();
107-
//Set identical time for the whole network scan
110+
// Set identical time for the whole network scan
108111
time_t tnow = time(nullptr);
109112
for (int i = 0; i < networks; i++) {
110113
Point sensorNetworks("wifi_networks");
@@ -152,7 +155,7 @@ void loop() {
152155
Serial.println(client.isBufferFull() ? "Yes" : "No");
153156
}
154157

155-
//Wait 10s
158+
// Wait 10s
156159
Serial.println("Wait 10s");
157160
delay(10000);
158161
}

src/InfluxDbClient.cpp

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
* SOFTWARE.
2626
*/
27-
#include <core_version.h>
2827
#include "InfluxDbClient.h"
28+
#include <core_version.h>
2929

3030
#define STRHELPER(x) #x
3131
#define STR(x) STRHELPER(x) // stringifier
@@ -41,17 +41,14 @@
4141
static const char UserAgent[] PROGMEM = "influxdb-client-arduino/" INFLUXDB_CLIENT_VERSION " (" INFLUXDB_CLIENT_PLATFORM " " INFLUXDB_CLIENT_PLATFORM_VERSION ")";
4242

4343
// Uncomment bellow in case of a problem and rebuild sketch
44-
//#define INFLUXDB_CLIENT_DEBUG
45-
44+
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
4645
#include "util/debug.h"
4746

4847
static const char UnitialisedMessage[] PROGMEM = "Unconfigured instance";
4948
// This cannot be put to PROGMEM due to the way how it is used
5049
static const char RetryAfter[] = "Retry-After";
5150
static const char TransferEnconding[] = "Transfer-Encoding";
5251

53-
static String escapeKey(String key);
54-
static String escapeValue(const char *value);
5552
static String escapeJSONString(String &value);
5653

5754
static String precisionToString(WritePrecision precision, uint8_t version = 2) {
@@ -70,7 +67,7 @@ static String precisionToString(WritePrecision precision, uint8_t version = 2) {
7067
}
7168

7269
Point::Point(String measurement):
73-
_measurement(measurement),
70+
_measurement(escapeKey(measurement, false)),
7471
_tags(""),
7572
_fields(""),
7673
_timestamp("")
@@ -194,12 +191,12 @@ void InfluxDBClient::setConnectionParamsV1(const char *serverUrl, const char *db
194191
}
195192

196193
bool InfluxDBClient::init() {
197-
INFLUXDB_CLIENT_DEBUG(F("Init\n"));
198-
INFLUXDB_CLIENT_DEBUG(F(" Server url: %s\n"), _serverUrl.c_str());
199-
INFLUXDB_CLIENT_DEBUG(F(" Org: %s\n"), _org.c_str());
200-
INFLUXDB_CLIENT_DEBUG(F(" Bucket: %s\n"), _bucket.c_str());
201-
INFLUXDB_CLIENT_DEBUG(F(" Token: %s\n"), _authToken.c_str());
202-
INFLUXDB_CLIENT_DEBUG(F(" DB version: %d\n"), _dbVersion);
194+
INFLUXDB_CLIENT_DEBUG("Init\n");
195+
INFLUXDB_CLIENT_DEBUG(" Server url: %s\n", _serverUrl.c_str());
196+
INFLUXDB_CLIENT_DEBUG(" Org: %s\n", _org.c_str());
197+
INFLUXDB_CLIENT_DEBUG(" Bucket: %s\n", _bucket.c_str());
198+
INFLUXDB_CLIENT_DEBUG(" Token: %s\n", _authToken.c_str());
199+
INFLUXDB_CLIENT_DEBUG(" DB version: %d\n", _dbVersion);
203200
if(_serverUrl.length() == 0 || (_dbVersion == 2 && (_org.length() == 0 || _bucket.length() == 0 || _authToken.length() == 0))) {
204201
INFLUXDB_CLIENT_DEBUG("[E] Invalid parameters\n");
205202
return false;
@@ -220,8 +217,9 @@ bool InfluxDBClient::init() {
220217
wifiClientSec->setFingerprint(_certInfo);
221218
}
222219
}
223-
if (_insecure)
220+
if (_insecure) {
224221
wifiClientSec->setInsecure();
222+
}
225223
#elif defined(ESP32)
226224
WiFiClientSecure *wifiClientSec = new WiFiClientSecure;
227225
if(_certInfo && strlen_P(_certInfo) > 0) {
@@ -269,18 +267,18 @@ void InfluxDBClient::clean() {
269267
}
270268

271269
void InfluxDBClient::setUrls() {
272-
INFLUXDB_CLIENT_DEBUG(F("setUrls\n"));
270+
INFLUXDB_CLIENT_DEBUG("setUrls\n");
273271
if(_dbVersion == 2) {
274272
_writeUrl = _serverUrl;
275273
_writeUrl += "/api/v2/write?org=";
276274
_writeUrl += _org ;
277275
_writeUrl += "&bucket=";
278276
_writeUrl += _bucket;
279-
INFLUXDB_CLIENT_DEBUG(F(" writeUrl: %s\n"), _writeUrl.c_str());
277+
INFLUXDB_CLIENT_DEBUG(" writeUrl: %s\n", _writeUrl.c_str());
280278
_queryUrl = _serverUrl;
281279
_queryUrl += "/api/v2/query?org=";
282280
_queryUrl += _org;
283-
INFLUXDB_CLIENT_DEBUG(F(" queryUrl: %s\n"), _queryUrl.c_str());
281+
INFLUXDB_CLIENT_DEBUG(" queryUrl: %s\n", _queryUrl.c_str());
284282
} else {
285283
_writeUrl = _serverUrl;
286284
_writeUrl += "/write?db=";
@@ -293,15 +291,16 @@ void InfluxDBClient::setUrls() {
293291
auth += "&p=";
294292
auth += _password;
295293
_writeUrl += auth;
294+
_queryUrl += "?";
296295
_queryUrl += auth;
297296
}
298-
INFLUXDB_CLIENT_DEBUG(F(" writeUrl: %s\n"), _writeUrl.c_str());
299-
INFLUXDB_CLIENT_DEBUG(F(" queryUrl: %s\n"), _queryUrl.c_str());
297+
INFLUXDB_CLIENT_DEBUG(" writeUrl: %s\n", _writeUrl.c_str());
298+
INFLUXDB_CLIENT_DEBUG(" queryUrl: %s\n", _queryUrl.c_str());
300299
}
301300
if(_writePrecision != WritePrecision::NoTime) {
302301
_writeUrl += "&precision=";
303302
_writeUrl += precisionToString(_writePrecision, _dbVersion);
304-
INFLUXDB_CLIENT_DEBUG(F(" writeUrl: %s\n"), _writeUrl.c_str());
303+
INFLUXDB_CLIENT_DEBUG(" writeUrl: %s\n", _writeUrl.c_str());
305304
}
306305

307306
}
@@ -505,7 +504,7 @@ bool InfluxDBClient::validateConnection() {
505504
return false;
506505
}
507506
// on version 1.x /ping will by default return status code 204, without verbose
508-
String url = _serverUrl + (_dbVersion==2?"/ready":"/ping?verbose=true");
507+
String url = _serverUrl + (_dbVersion==2?"/health":"/ping?verbose=true");
509508
INFLUXDB_CLIENT_DEBUG("[D] Validating connection to %s\n", url.c_str());
510509

511510
if(!_httpClient.begin(*_wifiClient, url)) {
@@ -649,63 +648,6 @@ void InfluxDBClient::postRequest(int expectedStatusCode) {
649648
}
650649
}
651650

652-
static String escapeKey(String key) {
653-
String ret;
654-
ret.reserve(key.length()+5); //5 is estimate of chars needs to escape,
655-
656-
for (char c: key)
657-
{
658-
switch (c)
659-
{
660-
case ' ':
661-
case ',':
662-
case '=':
663-
ret += '\\';
664-
break;
665-
}
666-
667-
ret += c;
668-
}
669-
return ret;
670-
}
671-
672-
static String escapeValue(const char *value) {
673-
String ret;
674-
int len = strlen_P(value);
675-
ret.reserve(len+5); //5 is estimate of max chars needs to escape,
676-
for(int i=0;i<len;i++)
677-
{
678-
switch (value[i])
679-
{
680-
case '\\':
681-
case '\"':
682-
ret += '\\';
683-
break;
684-
}
685-
686-
ret += value[i];
687-
}
688-
return ret;
689-
}
690-
static String escapeTagValue(const char *value) {
691-
String ret;
692-
int len = strlen_P(value);
693-
ret.reserve(len+5); //5 is estimate of max chars needs to escape,
694-
for(int i=0;i<len;i++)
695-
{
696-
switch (value[i])
697-
{
698-
case '\\':
699-
case '\"':
700-
ret += '\\';
701-
break;
702-
}
703-
704-
ret += value[i];
705-
}
706-
return ret;
707-
}
708-
709651
static String escapeJSONString(String &value) {
710652
String ret;
711653
int d = 0;

src/InfluxDbClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
#define INFLUXDB_CLIENT_VERSION "3.2.0"
3131

3232
#include <Arduino.h>
33-
#include "query/FluxParser.h"
34-
#include "util/helpers.h"
35-
3633
#if defined(ESP8266)
3734
# include <WiFiClientSecureBearSSL.h>
3835
# include <ESP8266HTTPClient.h>
@@ -42,6 +39,10 @@
4239
# error "This library currently supports only ESP8266 and ESP32."
4340
#endif
4441

42+
#include "query/FluxParser.h"
43+
#include "util/helpers.h"
44+
45+
4546
#ifdef USING_AXTLS
4647
#error AxTLS does not work
4748
#endif

src/query/CsvReader.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
* SOFTWARE.
2626
*/
2727
#include "CsvReader.h"
28-
// Uncomment bellow in case of a problem and rebuild sketch
29-
#define INFLUXDB_CLIENT_DEBUG
30-
#include "util/debug.h"
3128

3229
CsvReader::CsvReader(HttpStreamScanner *scanner) {
3330
_scanner = scanner;
@@ -108,4 +105,4 @@ bool CsvReader::next() {
108105
}
109106
_row = fields;
110107
return true;
111-
}
108+
}

src/query/FluxParser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "FluxParser.h"
2929
// Uncomment bellow in case of a problem and rebuild sketch
30-
//#define INFLUXDB_CLIENT_DEBUG
30+
//#define INFLUXDB_CLIENT_DEBUG_ENABLE
3131
#include "util/debug.h"
3232

3333
FluxQueryResult::FluxQueryResult(CsvReader *reader) {
@@ -123,12 +123,12 @@ bool FluxQueryResult::next() {
123123
if(!stat) {
124124
if(_data->_reader->getError()< 0) {
125125
_data->_error = HTTPClient::errorToString(_data->_reader->getError());
126-
INFLUXDB_CLIENT_DEBUG(F("Error '%s'\n"), _data->_error.c_str());
126+
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
127127
}
128128
return false;
129129
}
130130
std::vector<String> vals = _data->_reader->getRow();
131-
INFLUXDB_CLIENT_DEBUG(F("[D] FluxQueryResult: vals.size %d\n"), vals.size());
131+
INFLUXDB_CLIENT_DEBUG("[D] FluxQueryResult: vals.size %d\n", vals.size());
132132
if(vals.size() < 2) {
133133
goto readRow;
134134
}
@@ -145,15 +145,15 @@ bool FluxQueryResult::next() {
145145
reference = "," + vals[2];
146146
}
147147
_data->_error = message + reference;
148-
INFLUXDB_CLIENT_DEBUG(F("Error '%s'\n"), _data->_error.c_str());
148+
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
149149
return false;
150150
} else if (parsingState == ParsingStateNameRow) {
151151
if (vals[1] == "error") {
152152
parsingState = ParsingStateError;
153153
} else {
154154
if (vals.size()-1 != _data->_columnDatatypes.size()) {
155155
_data->_error = String(F("Parsing error, header has different number of columns than table: ")) + String(vals.size()-1) + " vs " + String(_data->_columnDatatypes.size());
156-
INFLUXDB_CLIENT_DEBUG(F("Error '%s'\n"), _data->_error.c_str());
156+
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
157157
return false;
158158
} else {
159159
for(int i=1;i < vals.size(); i++) {
@@ -166,12 +166,12 @@ bool FluxQueryResult::next() {
166166
}
167167
if(_data->_columnDatatypes.size() == 0) {
168168
_data->_error = F("Parsing error, datatype annotation not found");
169-
INFLUXDB_CLIENT_DEBUG(F("Error '%s'\n"), _data->_error.c_str());
169+
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
170170
return false;
171171
}
172172
if (vals.size()-1 != _data->_columnNames.size()) {
173173
_data->_error = String(F("Parsing error, row has different number of columns than table: ")) + String(vals.size()-1) + " vs " + String(_data->_columnNames.size());
174-
INFLUXDB_CLIENT_DEBUG(F("Error '%s'\n"), _data->_error.c_str());
174+
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
175175
return false;
176176
}
177177
for(int i=1;i < vals.size(); i++) {
@@ -180,7 +180,7 @@ bool FluxQueryResult::next() {
180180
v = convertValue(vals[i], _data->_columnDatatypes[i-1]);
181181
if(!v) {
182182
_data->_error = String(F("Unsupported datatype: ")) + _data->_columnDatatypes[i-1];
183-
INFLUXDB_CLIENT_DEBUG(F("Error '%s'\n"), _data->_error.c_str());
183+
INFLUXDB_CLIENT_DEBUG("Error '%s'\n", _data->_error.c_str());
184184
return false;
185185
}
186186
}

0 commit comments

Comments
 (0)