Skip to content

Commit 6e47386

Browse files
authored
Merge pull request #76 from bonitoo-io/fix/debug_compilation
Fix/debug compilation
2 parents 6a83e8a + 2897083 commit 6e47386

File tree

10 files changed

+66
-54
lines changed

10 files changed

+66
-54
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Changelog
2+
## Version 3.3.0 (in progress)
3+
- [NEW] Added possibility skip server certification validation (`setInsecure()` method)
4+
- [NEW] Added possibility to query flux on InfuxDB 1.8 using V1 approach
5+
- [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
6+
- [FIX] Debug compilation error
7+
- [FIX] SecureBatchWrite compile error
8+
29
## Version 3.2.0 (2020-06-09)
310
- [NEW] Added possibility to read data from InfluxDB using Flux queries
411
- [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: 18 additions & 17 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,8 +41,7 @@
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";
@@ -194,12 +193,12 @@ void InfluxDBClient::setConnectionParamsV1(const char *serverUrl, const char *db
194193
}
195194

196195
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);
196+
INFLUXDB_CLIENT_DEBUG("Init\n");
197+
INFLUXDB_CLIENT_DEBUG(" Server url: %s\n", _serverUrl.c_str());
198+
INFLUXDB_CLIENT_DEBUG(" Org: %s\n", _org.c_str());
199+
INFLUXDB_CLIENT_DEBUG(" Bucket: %s\n", _bucket.c_str());
200+
INFLUXDB_CLIENT_DEBUG(" Token: %s\n", _authToken.c_str());
201+
INFLUXDB_CLIENT_DEBUG(" DB version: %d\n", _dbVersion);
203202
if(_serverUrl.length() == 0 || (_dbVersion == 2 && (_org.length() == 0 || _bucket.length() == 0 || _authToken.length() == 0))) {
204203
INFLUXDB_CLIENT_DEBUG("[E] Invalid parameters\n");
205204
return false;
@@ -220,8 +219,9 @@ bool InfluxDBClient::init() {
220219
wifiClientSec->setFingerprint(_certInfo);
221220
}
222221
}
223-
if (_insecure)
222+
if (_insecure) {
224223
wifiClientSec->setInsecure();
224+
}
225225
#elif defined(ESP32)
226226
WiFiClientSecure *wifiClientSec = new WiFiClientSecure;
227227
if(_certInfo && strlen_P(_certInfo) > 0) {
@@ -269,18 +269,18 @@ void InfluxDBClient::clean() {
269269
}
270270

271271
void InfluxDBClient::setUrls() {
272-
INFLUXDB_CLIENT_DEBUG(F("setUrls\n"));
272+
INFLUXDB_CLIENT_DEBUG("setUrls\n");
273273
if(_dbVersion == 2) {
274274
_writeUrl = _serverUrl;
275275
_writeUrl += "/api/v2/write?org=";
276276
_writeUrl += _org ;
277277
_writeUrl += "&bucket=";
278278
_writeUrl += _bucket;
279-
INFLUXDB_CLIENT_DEBUG(F(" writeUrl: %s\n"), _writeUrl.c_str());
279+
INFLUXDB_CLIENT_DEBUG(" writeUrl: %s\n", _writeUrl.c_str());
280280
_queryUrl = _serverUrl;
281281
_queryUrl += "/api/v2/query?org=";
282282
_queryUrl += _org;
283-
INFLUXDB_CLIENT_DEBUG(F(" queryUrl: %s\n"), _queryUrl.c_str());
283+
INFLUXDB_CLIENT_DEBUG(" queryUrl: %s\n", _queryUrl.c_str());
284284
} else {
285285
_writeUrl = _serverUrl;
286286
_writeUrl += "/write?db=";
@@ -293,15 +293,16 @@ void InfluxDBClient::setUrls() {
293293
auth += "&p=";
294294
auth += _password;
295295
_writeUrl += auth;
296+
_queryUrl += "?";
296297
_queryUrl += auth;
297298
}
298-
INFLUXDB_CLIENT_DEBUG(F(" writeUrl: %s\n"), _writeUrl.c_str());
299-
INFLUXDB_CLIENT_DEBUG(F(" queryUrl: %s\n"), _queryUrl.c_str());
299+
INFLUXDB_CLIENT_DEBUG(" writeUrl: %s\n", _writeUrl.c_str());
300+
INFLUXDB_CLIENT_DEBUG(" queryUrl: %s\n", _queryUrl.c_str());
300301
}
301302
if(_writePrecision != WritePrecision::NoTime) {
302303
_writeUrl += "&precision=";
303304
_writeUrl += precisionToString(_writePrecision, _dbVersion);
304-
INFLUXDB_CLIENT_DEBUG(F(" writeUrl: %s\n"), _writeUrl.c_str());
305+
INFLUXDB_CLIENT_DEBUG(" writeUrl: %s\n", _writeUrl.c_str());
305306
}
306307

307308
}
@@ -505,7 +506,7 @@ bool InfluxDBClient::validateConnection() {
505506
return false;
506507
}
507508
// on version 1.x /ping will by default return status code 204, without verbose
508-
String url = _serverUrl + (_dbVersion==2?"/ready":"/ping?verbose=true");
509+
String url = _serverUrl + (_dbVersion==2?"/health":"/ping?verbose=true");
509510
INFLUXDB_CLIENT_DEBUG("[D] Validating connection to %s\n", url.c_str());
510511

511512
if(!_httpClient.begin(*_wifiClient, url)) {

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
}

src/query/HttpStreamScanner.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "HttpStreamScanner.h"
2828

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
HttpStreamScanner::HttpStreamScanner(HTTPClient *client, bool chunked)
@@ -37,13 +37,13 @@ HttpStreamScanner::HttpStreamScanner(HTTPClient *client, bool chunked)
3737
_chunked = chunked;
3838
_chunkHeader = chunked;
3939
_len = client->getSize();
40-
INFLUXDB_CLIENT_DEBUG(F("[D] HttpStreamScanner: chunked: %s, size: %d\n"), _chunked?"true":"false", _len);
40+
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner: chunked: %s, size: %d\n", _chunked?"true":"false", _len);
4141
}
4242

4343
bool HttpStreamScanner::next() {
4444
while(_client->connected() && (_len > 0 || _len == -1)) {
4545
_line = _stream->readStringUntil('\n');
46-
INFLUXDB_CLIENT_DEBUG(F("[D] HttpStreamScanner: line: %s\n"), _line.c_str());
46+
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner: line: %s\n", _line.c_str());
4747
++_linesNum;
4848
int lineLen = _line.length();
4949
if(lineLen == 0) {
@@ -68,7 +68,7 @@ bool HttpStreamScanner::next() {
6868
}
6969
if(_chunkHeader){
7070
_chunkLen = (int) strtol((const char *) _line.c_str(), NULL, 16);
71-
INFLUXDB_CLIENT_DEBUG(F("[D] HttpStreamScanner chunk len: %d\n"), _chunkLen);
71+
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner chunk len: %d\n", _chunkLen);
7272
_chunkHeader = false;
7373
_read = 0;
7474
if(_chunkLen == 0) { //last chunk
@@ -86,13 +86,13 @@ bool HttpStreamScanner::next() {
8686

8787
if(_len > 0) {
8888
_len -= r;
89-
INFLUXDB_CLIENT_DEBUG(F("[D] HttpStreamScanner new len: %d\n"), _len);
89+
INFLUXDB_CLIENT_DEBUG("[D] HttpStreamScanner new len: %d\n", _len);
9090
}
9191
return true;
9292
}
9393
if(!_client->connected() && ( (_chunked && _chunkLen > 0) || (!_chunked && _len > 0))) { //report error only if we didn't went to
9494
_error = HTTPC_ERROR_CONNECTION_LOST;
95-
INFLUXDB_CLIENT_DEBUG(F("HttpStreamScanner connection lost\n"));
95+
INFLUXDB_CLIENT_DEBUG("HttpStreamScanner connection lost\n");
9696
}
9797
return false;
9898
}

src/util/debug.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
#ifndef _INFLUXDB_CLIENT_DEBUG_H
2828
#define _INFLUXDB_CLIENT_DEBUG_H
2929

30-
// captilised the A of Arduino - kdriver
3130
#include <Arduino.h>
3231

33-
#ifdef INFLUXDB_CLIENT_DEBUG
32+
#ifdef INFLUXDB_CLIENT_DEBUG_ENABLE
3433
# define INFLUXDB_CLIENT_DEBUG(fmt, ...) Serial.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
3534
#else
3635
# define INFLUXDB_CLIENT_DEBUG(fmt, ...)

test/server/server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ app.get('/ready', (req,res) => {
2727
lastUserAgent = req.get('User-Agent');
2828
res.status(200).send("<html><body><h1>OK</h1></body></html>");
2929
})
30+
app.get('/health', (req,res) => {
31+
lastUserAgent = req.get('User-Agent');
32+
res.status(200).send("<html><body><h1>OK</h1></body></html>");
33+
})
3034

3135
app.get('/ping', (req,res) => {
3236
lastUserAgent = req.get('User-Agent');
@@ -338,7 +342,7 @@ function parsePoints(data) {
338342
const AuthToken = "Token 1234567890";
339343
function handleAuthentication(req, res) {
340344
var auth = req.get('Authorization');
341-
if(auth != AuthToken) {
345+
if(auth && auth != AuthToken) {
342346
res.status(401).send(`{"code":"unauthorized","message":"unauthorized access"}`);
343347
return false;
344348
} else {
@@ -382,7 +386,7 @@ function checkWriteParamsV1(req, res) {
382386

383387
function checkQueryParams(req, res) {
384388
var org = req.query['org'];
385-
if(org != 'my-org') {
389+
if(org && org !== 'my-org') {
386390
res.status(404).send(`{"code":"not found","message":"organization name \"${org}\" not found"}`);
387391
return false;
388392
} else {

0 commit comments

Comments
 (0)