Skip to content

Commit 9f4e60d

Browse files
vlastahajekRuss Savage
and
Russ Savage
authored
docs(client-libraries): Arduino client (#18396)
* docs: added Arduino client library overview * fix(logos): cleaning up logos, updating arduino logo Co-authored-by: Russ Savage <[email protected]>
1 parent f79abf2 commit 9f4e60d

File tree

14 files changed

+382
-17
lines changed

14 files changed

+382
-17
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Libraries
2+
import React, {FunctionComponent} from 'react'
3+
import {connect} from 'react-redux'
4+
5+
// Components
6+
import ClientLibraryOverlay from 'src/clientLibraries/components/ClientLibraryOverlay'
7+
import TemplatedCodeSnippet from 'src/shared/components/TemplatedCodeSnippet'
8+
9+
// Constants
10+
import {clientArduinoLibrary} from 'src/clientLibraries/constants'
11+
12+
// Types
13+
import {AppState} from 'src/types'
14+
15+
// Selectors
16+
import {getOrg} from 'src/organizations/selectors'
17+
18+
interface StateProps {
19+
org: string
20+
}
21+
22+
type Props = StateProps
23+
24+
const ClientArduinoOverlay: FunctionComponent<Props> = props => {
25+
const {
26+
name,
27+
url,
28+
installingLibraryManagerCodeSnippet,
29+
installingManualCodeSnippet,
30+
initializeClientCodeSnippet,
31+
writingDataPointCodeSnippet,
32+
executeQueryCodeSnippet,
33+
} = clientArduinoLibrary
34+
const {org} = props
35+
const server = window.location.origin
36+
37+
return (
38+
<ClientLibraryOverlay title={`${name} Client Library`}>
39+
<p>
40+
For more detailed and up to date information check out the{' '}
41+
<a href={url} target="_blank">
42+
GitHub Repository
43+
</a>
44+
</p>
45+
<h5>Install Library</h5>
46+
<p>Library Manager</p>
47+
<TemplatedCodeSnippet
48+
template={installingLibraryManagerCodeSnippet}
49+
label="Guide"
50+
/>
51+
<p>Manual Installation</p>
52+
<TemplatedCodeSnippet
53+
template={installingManualCodeSnippet}
54+
label="Guide"
55+
defaults={{
56+
url: 'url',
57+
}}
58+
values={{
59+
url,
60+
}}
61+
/>
62+
<h5>Initialize the Client</h5>
63+
<TemplatedCodeSnippet
64+
template={initializeClientCodeSnippet}
65+
label="Arduino Code"
66+
defaults={{
67+
server: 'basepath',
68+
token: 'token',
69+
org: 'orgID',
70+
bucket: 'bucketID',
71+
}}
72+
values={{
73+
server,
74+
org,
75+
}}
76+
/>
77+
<h5>Write Data</h5>
78+
<TemplatedCodeSnippet
79+
template={writingDataPointCodeSnippet}
80+
label="Arduino Code"
81+
/>
82+
<h5>Execute a Flux query</h5>
83+
<TemplatedCodeSnippet
84+
template={executeQueryCodeSnippet}
85+
label="Arduino Code"
86+
/>
87+
</ClientLibraryOverlay>
88+
)
89+
}
90+
91+
const mstp = (state: AppState): StateProps => {
92+
return {
93+
org: getOrg(state).id,
94+
}
95+
}
96+
97+
export {ClientArduinoOverlay}
98+
export default connect<StateProps, {}, Props>(
99+
mstp,
100+
null
101+
)(ClientArduinoOverlay)

ui/src/clientLibraries/constants/index.ts

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {SFC} from 'react'
22
import {
3+
ArduinoLogo,
34
CSharpLogo,
45
GoLogo,
56
JavaLogo,
@@ -18,6 +19,172 @@ export interface ClientLibrary {
1819
image: SFC
1920
}
2021

22+
export const clientArduinoLibrary = {
23+
id: 'arduino',
24+
name: 'Arduino',
25+
url: 'https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino',
26+
image: ArduinoLogo,
27+
installingLibraryManagerCodeSnippet: `1. Open the Arduino IDE and click to the "Sketch" menu and then Include Library > Manage Libraries.
28+
2. Type 'influxdb' in the search box
29+
3. Install the 'InfluxDBClient for Arduino' library`,
30+
installingManualCodeSnippet: `1. cd <arduino-sketch-location>/library.
31+
2. git clone <%= url %>
32+
3. Restart the Arduino IDE`,
33+
initializeClientCodeSnippet: `#if defined(ESP32)
34+
#include <WiFiMulti.h>
35+
WiFiMulti wifiMulti;
36+
#define DEVICE "ESP32"
37+
#elif defined(ESP8266)
38+
#include <ESP8266WiFiMulti.h>
39+
ESP8266WiFiMulti wifiMulti;
40+
#define DEVICE "ESP8266"
41+
#endif
42+
43+
#include <InfluxDbClient.h>
44+
#include <InfluxDbCloud.h>
45+
46+
// WiFi AP SSID
47+
#define WIFI_SSID "SSID"
48+
// WiFi password
49+
#define WIFI_PASSWORD "PASSWORD"
50+
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
51+
#define INFLUXDB_URL "<%= server %>"
52+
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
53+
#define INFLUXDB_TOKEN "<%= token %>"
54+
// InfluxDB v2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
55+
#define INFLUXDB_ORG "<%= org %>"
56+
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
57+
#define INFLUXDB_BUCKET "<%= bucket %>"
58+
59+
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
60+
// Examples:
61+
// Pacific Time: "PST8PDT"
62+
// Eastern: "EST5EDT"
63+
// Japanesse: "JST-9"
64+
// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3"
65+
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
66+
67+
// InfluxDB client instance with preconfigured InfluxCloud certificate
68+
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
69+
70+
// Data point
71+
Point sensor("wifi_status");
72+
73+
void setup() {
74+
Serial.begin(115200);
75+
76+
// Setup wifi
77+
WiFi.mode(WIFI_STA);
78+
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
79+
80+
Serial.print("Connecting to wifi");
81+
while (wifiMulti.run() != WL_CONNECTED) {
82+
Serial.print(".");
83+
delay(100);
84+
}
85+
Serial.println();
86+
87+
// Add tags
88+
sensor.addTag("device", DEVICE);
89+
sensor.addTag("SSID", WiFi.SSID());
90+
91+
// Accurate time is necessary for certificate validation and writing in batches
92+
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
93+
// Syncing progress and the time will be printed to Serial.
94+
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
95+
96+
// Check server connection
97+
if (client.validateConnection()) {
98+
Serial.print("Connected to InfluxDB: ");
99+
Serial.println(client.getServerUrl());
100+
} else {
101+
Serial.print("InfluxDB connection failed: ");
102+
Serial.println(client.getLastErrorMessage());
103+
}
104+
}`,
105+
writingDataPointCodeSnippet: `void loop() {
106+
// Clear fields for reusing the point. Tags will remain untouched
107+
sensor.clearFields();
108+
109+
// Store measured value into point
110+
// Report RSSI of currently connected network
111+
sensor.addField("rssi", WiFi.RSSI());
112+
113+
// Print what are we exactly writing
114+
Serial.print("Writing: ");
115+
Serial.println(sensor.toLineProtocol());
116+
117+
// If no Wifi signal, try to reconnect it
118+
if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED)) {
119+
Serial.println("Wifi connection lost");
120+
}
121+
122+
// Write point
123+
if (!client.writePoint(sensor)) {
124+
Serial.print("InfluxDB write failed: ");
125+
Serial.println(client.getLastErrorMessage());
126+
}
127+
128+
//Wait 10s
129+
Serial.println("Wait 10s");
130+
delay(10000);
131+
}`,
132+
executeQueryCodeSnippet: `void loop() {
133+
// Construct a Flux query
134+
// Query will find the worst RSSI for last hour for each connected WiFi network with this device
135+
String query = "from(bucket: \\"" INFLUXDB_BUCKET "\\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \\"wifi_status\\" and r._field == \\"rssi\\"";
136+
query += " and r.device == \\"" DEVICE "\\")";
137+
query += "|> min()";
138+
139+
// Print ouput header
140+
Serial.print("==== ");
141+
Serial.print(selectorFunction);
142+
Serial.println(" ====");
143+
144+
// Print composed query
145+
Serial.print("Querying with: ");
146+
Serial.println(query);
147+
148+
// Send query to the server and get result
149+
FluxQueryResult result = client.query(query);
150+
151+
// Iterate over rows. Even there is just one row, next() must be called at least once.
152+
while (result.next()) {
153+
// Get converted value for flux result column 'SSID'
154+
String ssid = result.getValueByName("SSID").getString();
155+
Serial.print("SSID '");
156+
Serial.print(ssid);
157+
158+
Serial.print("' with RSSI ");
159+
// Get converted value for flux result column '_value' where there is RSSI value
160+
long value = result.getValueByName("_value").getLong();
161+
Serial.print(value);
162+
163+
// Get converted value for the _time column
164+
FluxDateTime time = result.getValueByName("_time").getDateTime();
165+
166+
// Format date-time for printing
167+
// Format string according to http://www.cplusplus.com/reference/ctime/strftime/
168+
String timeStr = time.format("%F %T");
169+
170+
Serial.print(" at ");
171+
Serial.print(timeStr);
172+
173+
Serial.println();
174+
}
175+
176+
// Check if there was an error
177+
if(result.getError() != "") {
178+
Serial.print("Query result error: ");
179+
Serial.println(result.getError());
180+
}
181+
182+
// Close the result
183+
result.close();
184+
}
185+
`,
186+
}
187+
21188
export const clientCSharpLibrary = {
22189
id: 'csharp',
23190
name: 'C#',
@@ -512,6 +679,7 @@ system.terminate()`,
512679
}
513680

514681
export const clientLibraries: ClientLibrary[] = [
682+
clientArduinoLibrary,
515683
clientCSharpLibrary,
516684
clientGoLibrary,
517685
clientJavaLibrary,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Libraries
2+
import React, {SFC} from 'react'
3+
4+
const ArduinoLogo: SFC = () => {
5+
return (
6+
<svg
7+
width="100"
8+
height="100"
9+
preserveAspectRatio="xMidYMid meet"
10+
viewBox="0 0 720 490"
11+
>
12+
<style>
13+
{
14+
'.arduino{fill:none;stroke:#00979c;stroke-width:1}.text{fill:#00979c;stroke-width:1}'
15+
}
16+
</style>
17+
<path
18+
id="infinity"
19+
className="arduino"
20+
style={{
21+
strokeWidth: 59,
22+
}}
23+
d="M174 30C78.937 22.427-.942 134.8 38.361 221.95c37.952 94.4 180.989 120.13 249.159 44.42 65.88-64.55 91.33-163.57 171.52-214.629 82.03-55.96 214.21-3.125 229.75 96.429 15.62 95.33-87.06 188.19-180.27 159.93-68.71-15.53-118.15-74.15-146.28-135.77-34.52-58.97-79.65-121.804-150.05-138.385C199.68 31.108 186.82 29.987 174 30z"
24+
/>
25+
<path
26+
id="minus"
27+
className="arduino"
28+
d="M118 165h120"
29+
style={{
30+
strokeWidth: 25,
31+
}}
32+
/>
33+
<path
34+
id="plus"
35+
className="arduino"
36+
d="M486 165h100m-51-50v100"
37+
style={{
38+
strokeWidth: 32,
39+
}}
40+
/>
41+
<path
42+
id="A"
43+
className="arduino text"
44+
d="M84 485l-6.4-23H37l-6.6 22.963H8.683l34.445-116.329h30.617l34.445 116.33zm-26.434-94.968l-15.31 54.115h30.44z"
45+
/>
46+
<path
47+
id="R"
48+
className="arduino text"
49+
d="M180 485c-6.16-13.64-11.68-27.59-18.29-41.01-3.83-5.76-11.35-8.02-17.98-7.09-1.05 3.95-.25 8.16-.48 12.23V485h-21.9V368.67c16.45.38 33.02-.97 49.38 1.19 10.7 1.73 21.86 7.97 25.01 19.01 3.95 11.8 1.99 27.03-8.6 34.79-4.16 3.15-9.14 5.16-14.28 5.94 6.54 2.6 10.52 8.65 13.33 14.78 6.18 11.9 11.47 24.3 17.49 36.24 2.9 2.89 1.15 5.26-2.34 4.38H180zm-5.42-82.07c.81-7.4-5.22-14.46-12.58-15.15-6.16-1.34-12.5-.73-18.75-.87v32.85c9.21-.1 19.95 1.01 27.15-5.96 2.82-2.84 4.26-6.9 4.18-10.87z"
50+
/>
51+
<path
52+
id="D"
53+
className="arduino text"
54+
d="M307 425c0 16.34-3.65 34.15-16.06 45.74-11.93 11.24-29.05 14.33-44.91 13.9h-26.88V368.31c13.64.1 27.28-.22 40.92.21 13.54.73 27.94 5.34 36.34 16.62 8.57 11.29 10.54 26.07 10.59 39.86zm-23.5 1.43c-.19-11.38-.97-24.42-9.68-32.82-7.29-6.93-17.82-7.17-27.29-6.97h-5.48v79.66c10 0 20.87.79 29.42-5.4 10.97-7.59 12.99-22.14 13.03-34.47z"
55+
/>
56+
<path
57+
id="U"
58+
className="arduino text"
59+
d="M407 442c.32 14.07-4.38 29.63-16.62 37.76-14.07 8.91-32.18 8.82-47.57 3.64-11.57-4.55-19.3-16.37-19.86-28.66-1.53-13.95-.27-28-.68-42v-44.25h21.89c.2 27.67-.43 55.35.39 83 .75 7.75 6.1 15.87 14.37 16.72 8.38 1.89 19.29.1 23.24-8.51 4.66-10.13 2.5-21.52 2.98-32.29v-58.92h21.9c0 24.49.2 49.09 0 73.51z"
60+
/>
61+
<path
62+
id="I"
63+
className="arduino text"
64+
d="M428 386v-17.89h73.518V386h-25.634v80.371h25.634v18.068H428v-18.068h25.633V386z"
65+
/>
66+
<path
67+
id="N"
68+
className="arduino text"
69+
d="M583 485c-12.51-28.13-25.44-56.08-37.56-84.38V485H525.5V368.67h26.88c11.83 26.48 24.41 52.64 35.67 79.36.66 2.94 2.27 4.52 1.72.61v-79.97h19.93V485H583z"
70+
/>
71+
<path
72+
id="O"
73+
className="arduino text"
74+
d="M716 426c.1 17.33-3.54 36.21-16.34 48.83-13 12.7-33.67 14.75-50.21 8.52-13.94-5.33-22.03-19.45-24.87-33.46-3.05-15.52-3.18-31.81.62-47.2 3.61-14.3 12.89-28.04 27.23-33.19 13.4-4.85 29.21-4.31 41.72 2.81 13.59 8.11 19.33 24.2 21.03 39.17.58 4.81.82 9.67.82 14.52zm-23.14 1.07c-.25-11.45-.6-23.99-7.42-33.72-6.14-8.04-18.48-9.19-27.03-4.6-9.49 5.83-11.73 17.93-12.57 28.18-.63 13.14-.81 27.06 4.89 39.23 3.41 7.57 11.95 11.53 20 10.74 8.31 0 15.54-6.02 18.24-13.68 3.27-8.28 3.81-17.35 3.89-26.15z"
75+
/>
76+
<path
77+
id="TM"
78+
className="arduino text"
79+
d="M676.41 13.375v2.344h5.15v13.562h2.66V15.719h5.19v-2.344h-13zm14.78 0v15.906h2.43V15.719l4.29 13.562h2.4c1.37-4.447 2.73-8.896 4.1-13.343v13.343h2.4V13.375H703c-1.3 4.241-2.61 8.478-3.91 12.719-1.33-4.241-2.67-8.478-4-12.719h-3.9z"
80+
/>
81+
</svg>
82+
)
83+
}
84+
/* eslint-enable */
85+
export default ArduinoLogo

ui/src/clientLibraries/graphics/CSharpLogo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const CSharpLogo: SFC = () => {
66
<svg
77
width="100%"
88
height="100%"
9-
xmlns="http://www.w3.org/2000/svg"
9+
preserveAspectRatio="xMidYMid meet"
1010
viewBox="0 0 300 300"
1111
>
1212
<style>

ui/src/clientLibraries/graphics/GoLogo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const GoLogo: SFC = () => {
77
<svg
88
width="100%"
99
height="100%"
10-
xmlns="http://www.w3.org/2000/svg"
10+
preserveAspectRatio="xMidYMid meet"
1111
viewBox="0 0 300 300"
1212
xmlSpace="preserve"
1313
>

0 commit comments

Comments
 (0)