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.
Oops!, No Image to display.

List of Time Series Database features implemented in Shunya stack#

With Shunya stack you can make IoT device

  1. Insert data into Time Series Database.
  2. 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#

  1. Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
  2. InfluxDB installed on either Shunya OS or on external server.

Steps to use Time Series Database in Shunya stack#

  1. Set Time Series Database settings in Shunya.
  2. Insert data into Time Series Database.
  3. 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.

ConfigDescriptions
influxdbUrlURL to the Influx DB instance.
influxdbDBNameName of the database in which the data gets stored.

Example configuration:#

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 :

  1. 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-database
  2. Open the store-influxdb.c in an text editor and modify as per your use case.

  3. For our example, we need to first get the temperature data, add the code in your main() function

    float temperature = 76.98; /* For now lets assume that the temperature is 76.89 Celsius*/
  4. 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.

  5. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  6. Run the code

    sudo ./store-influxdb
  7. More examples Inserting temperature data to database,

    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);

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 :

  1. 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-database
  2. Open the read-influxdb.c in an text editor and modify as per your use case.

  3. 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", "*"};
  4. 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");
  5. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  6. Run the code

    sudo ./read-influxdb
  7. More examples on Querying Time series database,

    For Querying "temperature" value from table "warehouse", change code in step 3. with the example given below.

    influxQuery query = {"warehouse", "temperature", "", "", 0, 0};

Facing errors with the component?#