|
| 1 | +/** |
| 2 | + * QueryParams Example code for InfluxDBClient library for Arduino. |
| 3 | + * |
| 4 | + * This example demonstrates querying using parameters inserted into the Flux query. We select WiFi signal level values bellow a certain threshold. |
| 5 | + * WiFi signal is measured and stored in BasicWrite and SecureWrite examples. |
| 6 | + * |
| 7 | + * Demonstrates connection to any InfluxDB instance accesible via: |
| 8 | + * - InfluxDB 2 Cloud at https://cloud2.influxdata.com/ (certificate is preconfigured) |
| 9 | + * |
| 10 | + * Enter WiFi and InfluxDB parameters below |
| 11 | + **/ |
| 12 | + |
| 13 | +#if defined(ESP32) |
| 14 | +#include <WiFiMulti.h> |
| 15 | +WiFiMulti wifiMulti; |
| 16 | +#define DEVICE "ESP32" |
| 17 | +#elif defined(ESP8266) |
| 18 | +#include <ESP8266WiFiMulti.h> |
| 19 | +ESP8266WiFiMulti wifiMulti; |
| 20 | +#define DEVICE "ESP8266" |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <InfluxDbClient.h> |
| 24 | +#include <InfluxDbCloud.h> |
| 25 | + |
| 26 | +// WiFi AP SSID |
| 27 | +#define WIFI_SSID "SSID" |
| 28 | +// WiFi password |
| 29 | +#define WIFI_PASSWORD "PASSWORD" |
| 30 | +// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries) |
| 31 | +// InfluxDB 1.8+ (v2 compatibility API) server url, e.g. http://192.168.1.48:8086 |
| 32 | +#define INFLUXDB_URL "server-url" |
| 33 | +// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>) |
| 34 | +// InfluxDB 1.8+ (v2 compatibility API) use form user:password, eg. admin:adminpass |
| 35 | +#define INFLUXDB_TOKEN "server token" |
| 36 | +// InfluxDB v2 organization name or id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> ) |
| 37 | +// InfluxDB 1.8+ (v2 compatibility API) use any non empty string |
| 38 | +#define INFLUXDB_ORG "org name/id" |
| 39 | +// InfluxDB v2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets) |
| 40 | +// InfluxDB 1.8+ (v2 compatibility API) use database name |
| 41 | +#define INFLUXDB_BUCKET "bucket name" |
| 42 | + |
| 43 | +// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html |
| 44 | +// Examples: |
| 45 | +// Pacific Time: "PST8PDT" |
| 46 | +// Eastern: "EST5EDT" |
| 47 | +// Japanesse: "JST-9" |
| 48 | +// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3" |
| 49 | +#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3" |
| 50 | + |
| 51 | +// InfluxDB client instance with preconfigured InfluxCloud certificate |
| 52 | +InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert); |
| 53 | + |
| 54 | +void setup() { |
| 55 | + Serial.begin(115200); |
| 56 | + |
| 57 | + // Setup wifi |
| 58 | + WiFi.mode(WIFI_STA); |
| 59 | + wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD); |
| 60 | + |
| 61 | + Serial.print("Connecting to wifi"); |
| 62 | + while (wifiMulti.run() != WL_CONNECTED) { |
| 63 | + Serial.print("."); |
| 64 | + delay(500); |
| 65 | + } |
| 66 | + Serial.println(); |
| 67 | + |
| 68 | + |
| 69 | + // Accurate time is necessary for certificate validation |
| 70 | + // For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/ |
| 71 | + // Syncing progress and the time will be printed to Serial |
| 72 | + timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov"); |
| 73 | + |
| 74 | + // Check server connection |
| 75 | + if (client.validateConnection()) { |
| 76 | + Serial.print("Connected to InfluxDB: "); |
| 77 | + Serial.println(client.getServerUrl()); |
| 78 | + } else { |
| 79 | + Serial.print("InfluxDB connection failed: "); |
| 80 | + Serial.println(client.getLastErrorMessage()); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +// Queries WiFi signal level values bellow a certain threshold using parameters inserted into the Flux query |
| 86 | +// Prints composed query and the result values. |
| 87 | +void loop() { |
| 88 | + // Prepare query parameters |
| 89 | + QueryParams params; |
| 90 | + params.add("bucket", INFLUXDB_BUCKET); |
| 91 | + params.add("since", "-5m"); |
| 92 | + params.add("device", DEVICE); |
| 93 | + params.add("rssiTreshold", -50); |
| 94 | + |
| 95 | + // Construct a Flux query using parameters |
| 96 | + // Parameters are accessed via the 'params' Flux object |
| 97 | + // Flux only supports only string, float and int as parameters. Duration can be converted from string. |
| 98 | + // Query will find RSSI less than defined treshold |
| 99 | + String query = "from(bucket: params.bucket) |> range(start: duration(v: params.since)) \ |
| 100 | + |> filter(fn: (r) => r._measurement == \"wifi_status\") \ |
| 101 | + |> filter(fn: (r) => r._field == \"rssi\") \ |
| 102 | + |> filter(fn: (r) => r.device == params.device) \ |
| 103 | + |> filter(fn: (r) => r._value < params.rssiTreshold)"; |
| 104 | + |
| 105 | + // Print ouput header |
| 106 | + // Print composed query |
| 107 | + Serial.print("Querying with: "); |
| 108 | + Serial.println(query); |
| 109 | + |
| 110 | + // Send query to the server and get result |
| 111 | + FluxQueryResult result = client.query(query, params); |
| 112 | + |
| 113 | + //Print header |
| 114 | + Serial.printf("%10s %20s %5s\n","Time","SSID","RSSI"); |
| 115 | + |
| 116 | + for(int i=0;i<37;i++) { |
| 117 | + Serial.print('-'); |
| 118 | + } |
| 119 | + Serial.println(); |
| 120 | + |
| 121 | + // Iterate over rows. Even there is just one row, next() must be called at least once. |
| 122 | + int c = 0; |
| 123 | + while (result.next()) { |
| 124 | + // Get converted value for flux result column 'SSID' |
| 125 | + String ssid = result.getValueByName("SSID").getString(); |
| 126 | + |
| 127 | + // Get converted value for flux result column '_value' where there is RSSI value |
| 128 | + long rssi = result.getValueByName("_value").getLong(); |
| 129 | + |
| 130 | + // Get converted value for the _time column |
| 131 | + FluxDateTime time = result.getValueByName("_time").getDateTime(); |
| 132 | + |
| 133 | + // Format date-time for printing |
| 134 | + // Format string according to http://www.cplusplus.com/reference/ctime/strftime/ |
| 135 | + String timeStr = time.format("%F %T"); |
| 136 | + // Print formatted row |
| 137 | + Serial.printf("%20s %10s %5d\n", timeStr.c_str(), ssid.c_str() ,rssi); |
| 138 | + c++; |
| 139 | + } |
| 140 | + if(!c) { |
| 141 | + Serial.println(" No data found"); |
| 142 | + } |
| 143 | + |
| 144 | + // Check if there was an error |
| 145 | + if(result.getError() != "") { |
| 146 | + Serial.print("Query result error: "); |
| 147 | + Serial.println(result.getError()); |
| 148 | + } |
| 149 | + |
| 150 | + // Close the result |
| 151 | + result.close(); |
| 152 | + // Wait 15s |
| 153 | + delay(15000); |
| 154 | +} |
0 commit comments