You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Build a starter Python3 IoT app with the InfluxDB API and client libraries
2
-
3
-
## Introduction
1
+
## Build a starter Python IoT app with the InfluxDB API and client libraries
4
2
5
3
This guide is for python developers who want to build Internet-of-Things (IoT) applications using the InfluxDB API and client libraries.
6
4
InfluxDB API client libraries are maintained by InfluxData and the user community. As a developer, client libraries enable you to take advantage of:
@@ -13,30 +11,19 @@ deconstruct the flow of events and data between the app, devices, and InfluxDB.
13
11
You'll see code samples that use InfluxDB API python client libraries to
14
12
manage IoT devices, write data to InfluxDB, query data from InfluxDB, create visualizations, and monitor the health of devices and the application itself.
15
13
16
-
From start to finish, you will:
17
-
- create a UI dashboard using Jinja
18
-
- install the influxdb-client package
19
-
-[create a configuration file for authentication with InfluxDB](#create-config.ini)
20
-
-[create a virtual IoT device](#create-iot-virtual-device)
21
-
-[store virtual IoT device to InfluxDB using the API](#store-virtual-iot-device-to-influxdb)
22
-
-[query bucket for IoT device using the API](#query-bucket-for-device)
23
-
-[write telemetry data to InfluxDB using the API](#write-telemetry-data)
24
-
-[query telemetry data in InfluxDB using the API](#query-telemetry-data)
25
14
26
15
## Contents
27
-
1.[Setup InfluxDB](#setup-influxdb)
28
-
1.[InfluxDB API basics](#influxdb-api-basics)
29
-
1.[Authorization and authentication in InfluxDB](#authorization-and-authentication-in-influxdb)
30
-
1. Start with an API client library
31
-
1. Create a bucket
32
-
1. Measurements, time series
33
-
2. Measurement schemas (aka bucket schemas)
34
-
1.[Write data to InfluxDB](#write-data-to-influxdb)
35
-
1. Write line protocol
36
-
1.[Query InfluxDB](#query-influxdb)
37
-
1. Send a Flux query
38
-
1. Aggregate and downsample your data
39
-
1. Create data visualizations
16
+
From start to finish, you will:
17
+
-[Create a UI dashboard using Jinja](#create-iot-center-dashboard)
2. From your `iot-center-v2` directory, launch the `node` REPL.
713
-
714
-
```sh
715
-
716
-
node
717
-
718
-
```
719
-
720
-
3. At the prompt, paste the example code (from _Step 1_) and press `Enter`.
721
-
722
-
{{% /code-tab-content %}}
723
-
{{< /code-tabs-wrapper >}}
724
-
725
-
726
-
727
-
728
-
## Introducing IoT Center
729
-
730
-
The IoT Center architecture has four layers:
731
-
732
-
- **InfluxDB API**: InfluxDB v2 API.
733
-
- **IoT device**: Virtual or physical devices write IoT data to the InfluxDB API.
734
-
- **IoT Center UI**: User interface sends requests to IoT Center server and renders views for the browser.
735
-
- **IoT Center server**: Server and API receives requests from the UI, sends requests to InfluxDB,
736
-
and processes responses from InfluxDB.
737
-
738
-
### IoT Center: register and list IoT devices
681
+
## Connecting the UI to the API
682
+
Now that the core functionality has been implemented, we can now create a UI to perform these requests.
683
+
Your IoT Dashboard will have four main pages.
684
+
* Query Devices
685
+
* Create Devices
686
+
* Write Data
687
+
* Query Data
739
688
740
-
The IoT Center **Device Registrations** page lists registered IoT devices
741
-
and lets you register new devices.
742
-
In IoT Center, a **registered device** is a virtual or physical IoT device with a unique InfluxDB authorization.
743
-
For each registered device, you can do the following:
744
-
- view a dashboard with data visualizations
745
-
- view configuration
746
-
- remove the device
689
+
Within your templates directory, create the following files
690
+
* ```create.html```(link outs to the files. UI code explanation out of scope) (Page to create virutal IoT device)
691
+
* ```data.html``` (Page to query for telemetry data)
692
+
* ```devices.html``` (Page to query for device data)
693
+
* ```write.html``` (Page to write telemetry data)
747
694
748
-
### IoT Center: register a device
695
+
Once those files have been created, update ```base.html``` and ```app.py``` to connect the routes.
696
+
Without going into details regarding the UI, ```app.py``` serves our routes and runs our core logic.
749
697
750
-
When you click the "Register" button, IoT Center UI sends a request to the `/api/devices/DEVICE_ID` IoT Center server endpoint. In response, IoT Center server queries InfluxDB to find a point with device ID in `INFLUX_BUCKET_AUTH`.
751
-
If no point exists (i.e., the device is not registered), IoT Center server uses the InfluxDB client library to send the following API requests:
752
-
753
-
2. `POST` request to `/api/v2/write` writes a `deviceauth` point with device ID to `INFLUX_BUCKET_AUTH` bucket.
754
-
3. `POST` request to `/api/v2/authorizations` creates an authorization with the description **IoT Center: `DEVICE_ID`** and write permission to the `INFLUX_BUCKET` bucket.
755
-
4. `POST` request to `/api/v2/write` writes a `deviceauth` point with device ID, API token, and authorization ID to `INFLUX_BUCKET_AUTH`.
756
-
757
-
758
-
### IoT Center: list registered devices
759
-
760
-
To list registered devices, the IoT Center UI [DevicesPage](https://github.com/bonitoo-io/iot-center-v2/blob/3118c6576ad7bccf0b84b63f95350bdaa159324e/app/ui/src/pages/DevicesPage.tsx) component sends a request to the `/api/devices` IoT Center server endpoint.
When IoT Center server receives the request, the server calls the [`getDevices()`](ttps://github.com/bonitoo-io/iot-center-v2/blob/f5b4a0b663e2e14bcd4f6fddb35cab2de216e6b6/app/server/influxdb/devices.js#L16) function.
764
-
`getDevices()` does the following:
765
-
766
-
1. Instantiates a query client from the InfluxDB client library.
767
-
2. Builds a Flux query to retrieve all points with the `deviceauth` measurement from
768
-
the `INFLUX_BUCKET_AUTH` bucket.
769
-
3. Returns a `Promise` that sends the query to the `/api/v2/query` InfluxDB API endpoint and iterates over devices in the response.
770
-
771
-
#### Example
772
-
773
-
{{< code-tabs-wrapper >}}
774
-
{{% code-tabs %}}
775
-
[Node.js](#nodejs)
776
-
{{% /code-tabs %}}
777
-
{{% code-tab-content %}}
778
-
779
-
Flux query for devices in InfluxDB.
780
-
The query doesn't return device API tokens unless a device ID is specified.
0 commit comments