IoTool v1 Get Last Time
Introduction
The /android/iotool_v1_get_last_time
flow is designed to fetch the timestamp of the most recent
sensor reading from a database. It showcases the integration of custom
functions within Node-RED, querying a PostgreSQL database, and basic
error handling and HTTP response mechanisms.
Flow Overview
This documentation outlines the key components and functionalities within the IoTool v1 get last time flow, focusing on:
-
Structuring user data and sending it to Node-RED via post request.
-
Dynamically constructing query to retreive timestamp of last sensor reading.
-
Responding to the HTTP POST request with the retrieved timestamp.
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. For this example only these two pieces of data are sufficient. 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_v1_get_last_time",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "un",
"value": "username",
"type": "text"
},
{
"key": "up",
"value": "password",
"type": "text"
}
]
},
"url": {
"raw": "http://dev.sandbox.engineering:1880/android/iotool_v1_get_last_time",
"protocol": "http",
"host": [
"test.sandbox.engineering"
],
"port": "1880",
"path": [
"android",
"iotool_v1_get_last_time"
]
}
},
"response": []
}
]
}
Dynamic Query Configuration
The Function
node in Node-RED allows for the incorporation of custom
JavaScript code directly within your flows, significantly enhancing
Node-RED’s capabilities. In the example provided, an SQL query is
constructed dynamically. Values sent via the POST request are accessed
within the Function
node via the msg
object, allowing for flexible
query customization based on incoming data.
Error Handling
A Catch
node is strategically placed within the flow to monitor for
any errors that might occur during database interactions. If an error is
detected, it’s captured by the Catch
node and relayed back to the
originator of the POST request. This feedback mechanism ensures that the
sender is informed about the nature of the error, facilitating
troubleshooting.
Overview
The example presents a straightforward Node-RED application that leverages JavaScript functions and PostgreSQL queries. While this flow is designed to retrieve timestamps from IoTool, its structure and logic can be adapted to various other data retrieval and processing tasks, demonstrating the versatility of Node-RED in handling diverse types of data.