Skip to content

Commit 023a18a

Browse files
committed
added a basicwrite example like a PlatformIO project for test with CI
1 parent 54582f0 commit 023a18a

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp32dev]
12+
platform = espressif32
13+
board = esp32dev
14+
framework = arduino
15+
lib_deps =
16+
tobiasschuerg/ESP8266 Influxdb @ 3.9.0
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Basic Write Example code for InfluxDBClient library for Arduino
3+
* Data can be immediately seen in a InfluxDB UI: wifi_status measurement
4+
* Enter WiFi and InfluxDB parameters below
5+
*
6+
* Measures signal level of the actually connected WiFi network
7+
* This example supports only InfluxDB running from unsecure (http://...)
8+
* For secure (https://...) or Influx Cloud 2 use SecureWrite example
9+
**/
10+
11+
#if defined(ESP32)
12+
#include <WiFiMulti.h>
13+
WiFiMulti wifiMulti;
14+
#define DEVICE "ESP32"
15+
#elif defined(ESP8266)
16+
#include <ESP8266WiFiMulti.h>
17+
ESP8266WiFiMulti wifiMulti;
18+
#define DEVICE "ESP8266"
19+
#endif
20+
21+
#include <InfluxDbClient.h>
22+
23+
// WiFi AP SSID
24+
#define WIFI_SSID "ssid"
25+
// WiFi password
26+
#define WIFI_PASSWORD "password"
27+
// InfluxDB server url. Don't use localhost, always server name or ip address.
28+
// E.g. http://192.168.1.48:8086 (In InfluxDB 2 UI -> Load Data -> Client Libraries),
29+
#define INFLUXDB_URL "influxdb-url"
30+
// InfluxDB 2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
31+
#define INFLUXDB_TOKEN "toked-id"
32+
// InfluxDB 2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
33+
#define INFLUXDB_ORG "org"
34+
// InfluxDB 2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
35+
#define INFLUXDB_BUCKET "bucket"
36+
// InfluxDB v1 database name
37+
//#define INFLUXDB_DB_NAME "database"
38+
39+
// InfluxDB client instance
40+
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
41+
// InfluxDB client instance for InfluxDB 1
42+
//InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
43+
44+
// Data point
45+
Point sensor("wifi_status");
46+
47+
void setup() {
48+
Serial.begin(115200);
49+
50+
// Connect WiFi
51+
Serial.println("Connecting to WiFi");
52+
WiFi.mode(WIFI_STA);
53+
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
54+
while (wifiMulti.run() != WL_CONNECTED) {
55+
Serial.print(".");
56+
delay(500);
57+
}
58+
Serial.println();
59+
60+
// Set InfluxDB 1 authentication params
61+
//client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);
62+
63+
// Add constant tags - only once
64+
sensor.addTag("device", DEVICE);
65+
sensor.addTag("SSID", WiFi.SSID());
66+
67+
// Check server connection
68+
if (client.validateConnection()) {
69+
Serial.print("Connected to InfluxDB: ");
70+
Serial.println(client.getServerUrl());
71+
} else {
72+
Serial.print("InfluxDB connection failed: ");
73+
Serial.println(client.getLastErrorMessage());
74+
}
75+
}
76+
77+
void loop() {
78+
// Store measured value into point
79+
sensor.clearFields();
80+
// Report RSSI of currently connected network
81+
sensor.addField("rssi", WiFi.RSSI());
82+
// Print what are we exactly writing
83+
Serial.print("Writing: ");
84+
Serial.println(client.pointToLineProtocol(sensor));
85+
// If no Wifi signal, try to reconnect it
86+
if (wifiMulti.run() != WL_CONNECTED) {
87+
Serial.println("Wifi connection lost");
88+
}
89+
// Write point
90+
if (!client.writePoint(sensor)) {
91+
Serial.print("InfluxDB write failed: ");
92+
Serial.println(client.getLastErrorMessage());
93+
}
94+
95+
//Wait 10s
96+
Serial.println("Wait 10s");
97+
delay(10000);
98+
}

0 commit comments

Comments
 (0)