Influxdb
warning
The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.
What is Time Series Database?
- A database used to store data with time information
- For example: Temperature sensor at time 11:50 pm located at factory 1 recorded 31 deg Celsius.
List of Time Series Database features implemented in Shunya stack
With Shunya stack you can make IoT device
- Insert data into Time Series Database.
- Query data from Time Series Database.
note
Shunya stack uses InfluxDB database program to store and query the timeseries data.
Using Time Series Database in Shunya stack
Requirements to use Time Series Database in Shunya stack
- Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
- InfluxDB installed on either Shunya OS or on external server.
Steps to use Time Series Database in Shunya stack
- Set Time Series Database settings in Shunya.
- Insert data into Time Series Database.
- Query data from Time Series Database.
note
Run the steps given below inside Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices) terminals.
Step 1: Set Time Series Database settings in Shunya.
Set PLC Modbus details, by editing the config file /etc/shunya/config.json
inside Shunya OS.
A simple configuration should contain these parameters.
Config | Descriptions |
---|---|
influxdbUrl | URL to the Influx DB instance. |
influxdbDBName | Name of the database in which the data gets stored. |
Example configuration:
- InflusDB on Server
- InfluxDB on Shunya
Writing the below json in /etc/shunya/config.json
file, will tell the Shunya stack to
connect to InfluxDB installed on Shunya OS.
"database" : {
"influxdbUrl": "localhost:8086/",
"influxdbDBName": "shunya_test"
}
Writing the below json in /etc/shunya/config.json
file, will tell the Shunya stack to
connect to InfluxDB installed on server 146.253.91.253
with port 8086
.
"database" : {
"influxdbUrl": "http://releases.shunyaos.org:8086",
"influxdbDBName": "shunya_test"
}
Step 2: Insert data into Time Series Database.
Let's take an example use case: Say
- We have connected an IoT device in a Wahrehouse
- It is recording temperature data and storing it in Time series database.
- Read data from the temperature sensor connected to Shunya and,
- Insert the data to database
shunya_test
.
Steps are :
Start with an ready to use template for sending messages via MQTT
git clone https://gitlab.iotiot.in/repo-public/examples.git
cd examples/simple-examples/store-to-influxdb-databaseOpen the
store-influxdb.c
in an text editor and modify as per your use case.For our example, we need to first get the temperature data, add the code in your
main()
functionfloat temperature = 76.98; /* For now lets assume that the temperature is 76.89 Celsius*/
Inserting temperature data to database 'test-db', modify the code
float temperature = 76.98; /* For now lets assume that the temperature is 76.89 Celsius*/
/* Create an InfluxDB Object */
influxdbObj testdb = newInfluxdb("database"); /* Argument = JSON Title, Load settings from JSON file */
/* Insert data into InfluxDB database */
writeDbInflux (testdb, "warehouse temperature=%f", temperature);This will store data in Table warehouse and column temperature.
Once you are done editing, save and compile the code , by running
mkdir build && cd build
cmake ../
makeRun the code
sudo ./store-influxdb
More examples Inserting temperature data to database,
- Insert Integer
- Insert string
- Insert Multiple values
For Inserting Integer value, change code in step 4. with the example given below.
int humidity = 98;
/* Insert data into InfluxDB database */
writeDbInflux (testdb, "warehouse humidity=%d", humidity);For Inserting String value, change code in step 4. with the example given below.
char id[1024] = "ion23p98984h342edr23dw3";
/* Insert data into InfluxDB database */
writeDbInflux (testdb, "warehouse uniqueID=\"%s\"", id);For inserting multiple variables, change code in step 4. with the example given below.
float temperature = 76.89;
int humidity = 98;
char id[1024] = "ion23p98984h342edr23dw3";
/* Insert data into InfluxDB database */
writeDbInflux (testdb, "warehouse temperature=%f,humidity=%d,uniqueID=\"%s\"",
temperature, humidity, id);
Step 3: Query data from Time Series Database.
Let's take an example use case: Say we need to
- Query all warehouse data from Time series database and dump all the data into CSV file.
Steps are :
Start with an ready to use template for sending messages via MQTT
git clone https://gitlab.iotiot.in/repo-public/examples.git
cd examples/simple-examples/read-influxdb-databaseOpen the
read-influxdb.c
in an text editor and modify as per your use case.For our example, we need to first write a query for dumping all the warehouse data, add the code in your
main()
function/* Create and InfluxDB query*/
/* The query will get all the in the table "warehouse"*/
influxQuery query = {"warehouse", "*"};Dumping all the data to CSV file "warehouse-data", modify the code
influxQuery query = {"warehouse", "*", "", "", 0, 0};
/* Create an InfluxDB Object */
influxdbObj testdb = newInfluxdb("database"); /* Argument = JSON Title, Load settings from JSON file */
/*Read from influxDB and store it into CSV file*/
readInfluxDbToCsv(testdb, query, "warehouse-data.csv");Once you are done editing, save and compile the code , by running
mkdir build && cd build
cmake ../
makeRun the code
sudo ./read-influxdb
More examples on Querying Time series database,
- Query one Value
For Querying "temperature" value from table "warehouse", change code in step 3. with the example given below.
influxQuery query = {"warehouse", "temperature", "", "", 0, 0};