#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
// WiFi AP SSID
#define WIFI_SSID "Wokwi-GUEST"
// WiFi password
#define WIFI_PASSWORD ""
#define INFLUXDB_URL "http://localhost:8086"
#define INFLUXDB_TOKEN "6XjBo8WlpGe2MxQQXRZVRCH2fc2VVgBrJLDJ8PxTmH4OKpX2nWjCI4DaD_M4--Uz9Zh8laUxcqalo6MKwl2ttQ=="
#define INFLUXDB_ORG "e725a97df3392407"
#define INFLUXDB_BUCKET "DTS-PROA"
// Time zone info
#define TZ_INFO "WIB-7"
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Declare Data point
Point sensor("wifi_status");
void setup() {
Serial.begin(115200);
// ... code in setup() from Initialize Client
// Add tags to the data point
sensor.addTag("device", DEVICE);
sensor.addTag("SSID", WiFi.SSID());
// 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(100);
}
Serial.println();
// Accurate time is necessary for certificate validation and writing in batches
// We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// 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());
}
}
void loop() {
// ... code from Write Data step
// Query will find the RSSI values for last minute for each connected WiFi network with this device
String query = "from(bucket: \"DTS-PROA\")\n\
|> range(start: -1m)\n\
|> filter(fn: (r) => r._measurement == \"wifi_status\" and r._field == \"rssi\")";
// Print composed query
Serial.println("Querying for RSSI values written to the \"DTS-PROA\" bucket in the last 1 min... ");
Serial.println(query);
// Send query to the server and get result
FluxQueryResult result = client.query(query);
Serial.println("Results : ");
// Iterate over rows.
while (result.next()) {
// Get converted value for flux result column 'SSID'
String ssid = result.getValueByName("SSID").getString();
Serial.print("SSID '");
Serial.print(ssid);
Serial.print("' with RSSI ");
// Get value of column named '_value'
long value = result.getValueByName("_value").getLong();
Serial.print(value);
// Get value for the _time column
FluxDateTime time = result.getValueByName("_time").getDateTime();
String timeStr = time.format("%F %T");
Serial.print(" at ");
Serial.print(timeStr);
Serial.println();
}
// Report any error
if (result.getError() != "") {
Serial.print("Query result error: ");
Serial.println(result.getError());
}
// Close the result
result.close();
Serial.println("==========");
delay(5000);
// Clear fields for reusing the point. Tags will remain the same as set above.
sensor.clearFields();
// Store measured value into point
// Report RSSI of currently connected network
sensor.addField("rssi", WiFi.RSSI());
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(sensor.toLineProtocol());
// Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Waiting 1 second");
delay(1000);
}