-
Notifications
You must be signed in to change notification settings - Fork 95
/
SecureBatchWrite.ino
167 lines (142 loc) · 5.93 KB
/
SecureBatchWrite.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Secure Write Example code for InfluxDBClient library for Arduino
* Enter WiFi and InfluxDB parameters below
*
* Demonstrates connection to any InfluxDB instance accesible via:
* - unsecured http://...
* - secure https://... (appropriate certificate is required)
* - InfluxDB 2 Cloud at https://cloud2.influxdata.com/ (certificate is preconfigured)
* Measures signal level of all visible WiFi networks including signal level of the actually connected one
* This example demonstrates time handling, how to write measures with different priorities, batching and retry
* Data can be immediately seen in a InfluxDB 2 Cloud UI - measurements wifi_status and wifi_networks
**/
#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#define WIFI_AUTH_OPEN ENC_TYPE_NONE
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
// WiFi AP SSID
#define WIFI_SSID "SSID"
// WiFi password
#define WIFI_PASSWORD "PASSWORD"
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL "server-url"
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
#define INFLUXDB_TOKEN "server token"
// InfluxDB v2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
#define INFLUXDB_ORG "org id"
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
#define INFLUXDB_BUCKET "bucket name"
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// Examples:
// Pacific Time: "PST8PDT"
// Eastern: "EST5EDT"
// Japanesse: "JST-9"
// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3"
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
// NTP servers the for time synchronization.
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
#define NTP_SERVER1 "pool.ntp.org"
#define NTP_SERVER2 "time.nis.gov"
#define WRITE_PRECISION WritePrecision::S
#define MAX_BATCH_SIZE 10
#define WRITE_BUFFER_SIZE 30
// InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// InfluxDB client instance without preconfigured InfluxCloud certificate for insecure connection
//InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
// Data point
Point sensorStatus("wifi_status");
// Number for loops to sync time using NTP
int iterations = 0;
void setup() {
Serial.begin(115200);
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
// Add tags
sensorStatus.addTag("device", DEVICE);
sensorStatus.addTag("SSID", WiFi.SSID());
// Alternatively, set insecure connection to skip server certificate validation
//client.setInsecure();
// Accurate time is necessary for certificate validation and writing in batches
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, NTP_SERVER1, NTP_SERVER2);
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
// Enable messages batching and retry buffer
client.setWriteOptions(WriteOptions().writePrecision(WRITE_PRECISION).batchSize(MAX_BATCH_SIZE).bufferSize(WRITE_BUFFER_SIZE));
}
void loop() {
// Sync time for batching once per hour
if (iterations++ >= 360) {
timeSync(TZ_INFO, NTP_SERVER1, NTP_SERVER2);
iterations = 0;
}
// Report networks (low priority data) just in case we successfully wrote the previous batch
if (client.isBufferEmpty()) {
// Report all the detected wifi networks
int networks = WiFi.scanNetworks();
// Set identical time for the whole network scan
time_t tnow = time(nullptr);
for (int i = 0; i < networks; i++) {
Point sensorNetworks("wifi_networks");
sensorNetworks.addTag("device", DEVICE);
sensorNetworks.addTag("SSID", WiFi.SSID(i));
sensorNetworks.addTag("channel", String(WiFi.channel(i)));
sensorNetworks.addTag("open", String(WiFi.encryptionType(i) == WIFI_AUTH_OPEN));
sensorNetworks.addField("rssi", WiFi.RSSI(i));
sensorNetworks.setTime(tnow); //set the time
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(client.pointToLineProtocol(sensorNetworks));
// Write point into buffer - low priority measures
client.writePoint(sensorNetworks);
}
} else
Serial.println("Wifi networks reporting skipped due to communication issues");
// Report RSSI of currently connected network
sensorStatus.setTime(time(nullptr));
sensorStatus.addField("rssi", WiFi.RSSI());
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(client.pointToLineProtocol(sensorStatus));
// Write point into buffer - high priority measure
client.writePoint(sensorStatus);
// Clear fields for next usage. Tags remain the same.
sensorStatus.clearFields();
// If no Wifi signal, try to reconnect it
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// End of the iteration - force write of all the values into InfluxDB as single transaction
Serial.println("Flushing data into InfluxDB");
if (!client.flushBuffer()) {
Serial.print("InfluxDB flush failed: ");
Serial.println(client.getLastErrorMessage());
Serial.print("Full buffer: ");
Serial.println(client.isBufferFull() ? "Yes" : "No");
}
// Wait 10s
Serial.println("Wait 10s");
delay(10000);
}