IoTool Lite v3 Save Data

Introduction

The `/android/iotool_lite_v3_savedata`flow example is designed for data ingestion and processing. It interacts with PostgreSQL databases to manage sensor data, leveraging HTTP requests, database queries, and custom logic to parse and insert data efficiently.

Flow Overview

This documentation outlines the key components and functionalities within the IoTool lite v3 save data, focusing on:

  1. Structuring sensor data and sending it to Node-RED using HTTP POST request,

  2. Managment of sensors through PostgreSQL database

  3. Table creation and manipulation in PostgreSQL

  4. Parsing and inserting sensor data into database tables

Post Request Handling

Purpose

Initiates the flow by receiving data through a POST request to a specified address. This example shows you how to specify endpoint where data is going to be sent and example configuration/structure of data.

Configuration

Following json file only represents example structure of POST request. Json file contains fields for PostgreSQL username and password, along with some other data which would tipically come from sensor such as sensor name and readings. For this example all provided fields are needed. Username and password in example are random and you should change those to actual credentials for your PostgreSQL user.

You should also modify url and host and set them to your device.

Example of Post request to send:

{
  "info": {
    "_postman_id": "56ff6dea-0cd1-40b3-80ef-8a7426935ede",
    "name": "IoTool",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "_exporter_id": "4611963"
  },
  "item": [
    {
      "name": "iotool_lite_v3_savedata",
      "request": {
        "method": "POST",
        "header": [],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "un",
              "value": "username",
              "type": "text"
            },
            {
              "key": "up",
              "value": "password",
              "type": "text"
            },
            {
              "key": "db",
              "value": "localhost",
              "type": "text"
            },
            {
              "key": "sensorcount",
              "value": "1",
              "type": "text"
            },
            {
              "key": "db0",
              "value": "senzor1",
              "type": "text"
            },
            {
              "key": "data0",
              "value": ",1#,2#,3",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "http://dev.sandbox.engineering:1880/android/iotool_lite_v3_savedata",
          "protocol": "http",
          "host": [
            "dev.sandbox.engineering"
          ],
          "port": "1880",
          "path": [
            "android",
            "iotool_lite_v3_savedata"
          ]
        }
      },
      "response": []
    }
  ]
}

Managment of sensors

Purpose

This section of the flow is used to manage incomming sensor data. Focus is on generating sensor ID which will help you connect readings to specific sensor in database.

Try to add sensor IDs subflow

This subflow enhances the parent flow by focusing on error detection and data structuring. It serves as the precursor to the addNewSensorsIDs subflow, analyzing the output from that subflow to ascertain any process errors. Utilizing the log error subflow in conjunction with a Template node, it formats any encountered errors into HTML. This ensures that the errors are easily interpretable by the requester, such as when viewing the response in an API testing tool like Postman.

Add New Sensor IDs Flow

This flow integrates several key database management and error logging principles. Initially, it ensures the creation of the necessary table before invoking the getSID subflow. The getSID subflow is crucial for injecting the required credentials to access a specific user within PostgreSQL.

The core operation of this flow involves searching the database for the sensor ID specified in the incoming HTTP POST request. If the sensor ID is not found within the table, the flow proceeds to add it to the database. Following the completion of this operation—whether successful or not—the outcome is relayed back to the parent flow. Additionally, in the event of a failure, an error message is generated and dispatched for further handling.

Adding Sensor Data

This segment of the workflow is dedicated to the interpretation and organization of incoming sensor data, followed by the creation of a dynamic SQL query for the insertion of this data into a PostgreSQL table. The function outlined in this section is designed to adapt sensor data for SQL insertion. This adaptation involves specifically transforming special numerical values such as NaN, -Infinity, and Infinity into string formats. Moreover, it structures the data entries for database integration by associating each with a timestamp and sensor ID. An HTTP response is dispatched following a successful operation, signaling the completion of the data insertion process.

Overview

This workflow demonstrates several crucial aspects of data management and transmission. It provides insights on how to handle and parse HTTP POST requests, generate dynamic SQL queries, and craft user-specific JavaScript code segments to augment the functionality within Node-RED. This overview offers a glimpse into the versatility and customization potential of Node-RED for sophisticated data processing tasks.