2020年12月4日 星期五

Node-Red liteDB 的使用範例

 Node-Red liteDB 的使用範例

liteDB 適用於 https://fred.sensetecnic.com/ 

UI -->  https://alex9ufo.fred.sensetecnic.com/api/ui


The Front End for Node-RED (FRED) manages instances of Node-RED for multiple users in the cloud. 

Create a FRED Account

To get started, create your own Node-RED instance in the cloud. Register for a free account at http://fred.sensetecnic.com.

After registering, make sure to activate your account via your email. You will not be able to login until you validate your account.

Installing the litedb node

First, you will need to ensure that you have installed the litedb node since it is not installed by default in FRED. After you register for FRED, or after you login, you will be presented with the FRED dashboard.

In the management panel, click “Add or Remove Nodes“, and find the litedb node.






https://{REPLACE-WITH-YOUR-USERNAME}.fred.sensetecnic.com/api/ui


1.新增 資料庫 (僅能執行一次)

msg.topic = 'CREATE TABLE  random_num ( Time_stamp INTEGER NOT NULL, Value INTEGER NOT NULL, Bool INTEGER NOT NULL, PRIMARY KEY(Time_stamp))';

return msg;

2.Insert (新增紀錄)

msg.topic = "INSERT INTO random_num (Time_stamp ,Value, Bool) VALUES (?,?,?)";

var time_stamp= Date.now() ;

var randomNum = Math.round(Math.random()*100);

var largeBool = (randomNum > 50)?1:0;

msg.payload = [time_stamp ,randomNum,largeBool];

return msg;

3.選擇紀錄

msg.topic = 'SELECT * FROM random_num ORDER BY Time_stamp DESC LIMIT 100';

return msg;

4.刪除所有紀錄

msg.topic = "DELETE FROM random_num  WHERE Time_stamp <= strftime('%s','now', '-1 hours')*1000";

return msg;

5.顯示紀錄筆數

msg.topic = 'SELECT COUNT(*) FROM random_num';

return msg;

6.檢視資料庫

msg.topic ="SELECT * FROM random_num ORDER BY Time_stamp DESC LIMIT 100";
return msg;

========================Node-Red 程式==============

[

    {

        "id": "f7cc0ce5.16ec3",

        "type": "inject",

        "z": "6868818c.c3848",

        "name": "新增 資料庫 (僅能執行一次)",

        "topic": "",

        "payload": "",

        "payloadType": "date",

        "repeat": "",

        "crontab": "",

        "once": false,

        "onceDelay": 0.1,

        "x": 230,

        "y": 30,

        "wires": [

            [

                "69706324.17d51c"

            ]

        ]

    },

    {

        "id": "2922f868.71b138",

        "type": "litedb",

        "z": "6868818c.c3848",

        "name": "liteDB_EX1.db",

        "x": 620,

        "y": 220,

        "wires": [

            [

                "e4ea68bf.93d988"

            ]

        ]

    },

    {

        "id": "e4ea68bf.93d988",

        "type": "debug",

        "z": "6868818c.c3848",

        "name": "",

        "active": true,

        "tosidebar": true,

        "console": false,

        "tostatus": false,

        "complete": "false",

        "x": 790,

        "y": 220,

        "wires": []

    },

    {

        "id": "dc129cba.90505",

        "type": "inject",

        "z": "6868818c.c3848",

        "name": "Insert (新增紀錄)",

        "topic": "",

        "payload": "",

        "payloadType": "date",

        "repeat": "",

        "crontab": "",

        "once": false,

        "onceDelay": 0.1,

        "x": 190,

        "y": 120,

        "wires": [

            [

                "9e03c46b.a7fe88"

            ]

        ]

    },

    {

        "id": "9e03c46b.a7fe88",

        "type": "function",

        "z": "6868818c.c3848",

        "name": "write query",

        "func": "msg.topic = \"INSERT INTO random_num (Time_stamp ,Value, Bool) VALUES (?,?,?)\";\nvar time_stamp= Date.now() ;\nvar randomNum = Math.round(Math.random()*100);\nvar largeBool = (randomNum > 50)?1:0;\nmsg.payload = [time_stamp ,randomNum,largeBool];\nreturn msg;\n",

        "outputs": 1,

        "noerr": 0,

        "x": 370,

        "y": 150,

        "wires": [

            [

                "2922f868.71b138",

                "25e3da8b.356f16"

            ]

        ]

    },

    {

        "id": "8e85c6f2.ff8108",

        "type": "inject",

        "z": "6868818c.c3848",

        "name": "Select",

        "topic": "",

        "payload": "",

        "payloadType": "date",

        "repeat": "",

        "crontab": "",

        "once": false,

        "onceDelay": 0.1,

        "x": 160,

        "y": 210,

        "wires": [

            [

                "ff5dfb3e.455708"

            ]

        ]

    },

    {

        "id": "81d29b53.d21b58",

        "type": "inject",

        "z": "6868818c.c3848",

        "name": "Delete Records",

        "topic": "",

        "payload": "",

        "payloadType": "date",

        "repeat": "180",

        "crontab": "",

        "once": false,

        "onceDelay": 0.1,

        "x": 200,

        "y": 290,

        "wires": [

            [

                "4f6a2ea5.618"

            ]

        ]

    },

    {

        "id": "bb889245.b0d45",

        "type": "inject",

        "z": "6868818c.c3848",

        "name": "Show Count",

        "topic": "",

        "payload": "",

        "payloadType": "date",

        "repeat": "",

        "crontab": "",

        "once": false,

        "onceDelay": 0.1,

        "x": 180,

        "y": 380,

        "wires": [

            [

                "63c9f0b0.9ee4c"

            ]

        ]

    },

    {

        "id": "cf8e3ed5.8aaf7",

        "type": "inject",

        "z": "6868818c.c3848",

        "name": "View Recoder",

        "topic": "",

        "payload": "",

        "payloadType": "date",

        "repeat": "",

        "crontab": "",

        "once": false,

        "onceDelay": 0.1,

        "x": 180,

        "y": 480,

        "wires": [

            [

                "1a10de02.648302"

            ]

        ]

    },

    {

        "id": "8b67a8dc.cdba98",

        "type": "litedb",

        "z": "6868818c.c3848",

        "name": "liteDB_EX1.db",

        "x": 520,

        "y": 480,

        "wires": [

            [

                "9416c5d9.a5a3d8"

            ]

        ]

    },

    {

        "id": "9416c5d9.a5a3d8",

        "type": "ui_template",

        "z": "6868818c.c3848",

        "group": "a504ec7f.10f84",

        "name": "UI Table",

        "order": 0,

        "width": "15",

        "height": "12",

        "format": "<table style=\"width:100%\">\n  <tr>\n    <th>Index</th> \n    <th>Timestamp</th>\n    <th>Value</th> \n    <th>Bool</th>\n  </tr>\n  <tr ng-repeat=\"x in msg.payload | limitTo:20\">\n    <td>{{$index}}</td>\n    <td>{{msg.payload[$index].Time_stamp}}</td>\n    <td>{{msg.payload[$index].Value}}</td> \n    <td>{{msg.payload[$index].Bool}}</td>\n  </tr>\n</table>",

        "storeOutMessages": true,

        "fwdInMessages": true,

        "resendOnRefresh": true,

        "templateScope": "local",

        "x": 690,

        "y": 470,

        "wires": [

            []

        ]

    },

    {

        "id": "69706324.17d51c",

        "type": "function",

        "z": "6868818c.c3848",

        "name": "",

        "func": "msg.topic = 'CREATE TABLE  random_num (\tTime_stamp\tINTEGER NOT NULL,\tValue INTEGER NOT NULL,\tBool\tINTEGER NOT NULL,\tPRIMARY KEY(Time_stamp))';\nreturn msg;",

        "outputs": 1,

        "noerr": 0,

        "x": 430,

        "y": 50,

        "wires": [

            [

                "2922f868.71b138"

            ]

        ]

    },

    {

        "id": "ff5dfb3e.455708",

        "type": "function",

        "z": "6868818c.c3848",

        "name": "",

        "func": "msg.topic = 'SELECT * FROM random_num ORDER BY Time_stamp DESC LIMIT 100';\nreturn msg;",

        "outputs": 1,

        "noerr": 0,

        "x": 320,

        "y": 230,

        "wires": [

            [

                "2922f868.71b138"

            ]

        ]

    },

    {

        "id": "63c9f0b0.9ee4c",

        "type": "function",

        "z": "6868818c.c3848",

        "name": "",

        "func": "msg.topic = 'SELECT COUNT(*) FROM random_num';\nreturn msg;",

        "outputs": 1,

        "noerr": 0,

        "x": 370,

        "y": 420,

        "wires": [

            [

                "2922f868.71b138"

            ]

        ]

    },

    {

        "id": "4f6a2ea5.618",

        "type": "function",

        "z": "6868818c.c3848",

        "name": "",

        "func": "msg.topic = \"DELETE FROM random_num  WHERE Time_stamp <= strftime('%s','now', '-1 hours')*1000\";\nreturn msg;",

        "outputs": 1,

        "noerr": 0,

        "x": 370,

        "y": 330,

        "wires": [

            [

                "2922f868.71b138"

            ]

        ]

    },

    {

        "id": "25e3da8b.356f16",

        "type": "debug",

        "z": "6868818c.c3848",

        "name": "",

        "active": true,

        "tosidebar": true,

        "console": false,

        "tostatus": false,

        "complete": "false",

        "x": 620,

        "y": 60,

        "wires": []

    },

    {

        "id": "1a10de02.648302",

        "type": "function",

        "z": "6868818c.c3848",

        "name": "",

        "func": "msg.topic =\"SELECT * FROM random_num ORDER BY Time_stamp DESC LIMIT 100\";\nreturn msg;",

        "outputs": 1,

        "noerr": 0,

        "x": 350,

        "y": 480,

        "wires": [

            [

                "8b67a8dc.cdba98"

            ]

        ]

    },

    {

        "id": "1abcb0ee.469dff",

        "type": "ui_button",

        "z": "6868818c.c3848",

        "name": "",

        "group": "a504ec7f.10f84",

        "order": 1,

        "width": "5",

        "height": "2",

        "passthru": false,

        "label": "檢視資料庫",

        "tooltip": "",

        "color": "",

        "bgcolor": "",

        "icon": "",

        "payload": "",

        "payloadType": "str",

        "topic": "",

        "x": 160,

        "y": 520,

        "wires": [

            [

                "1a10de02.648302"

            ]

        ]

    },

    {

        "id": "24fdd87.ddaf128",

        "type": "ui_button",

        "z": "6868818c.c3848",

        "name": "",

        "group": "a504ec7f.10f84",

        "order": 2,

        "width": "5",

        "height": "2",

        "passthru": false,

        "label": "顯示紀錄筆數",

        "tooltip": "",

        "color": "",

        "bgcolor": "",

        "icon": "",

        "payload": "",

        "payloadType": "str",

        "topic": "",

        "x": 170,

        "y": 420,

        "wires": [

            [

                "63c9f0b0.9ee4c"

            ]

        ]

    },

    {

        "id": "9bbcc23.879e94",

        "type": "ui_button",

        "z": "6868818c.c3848",

        "name": "",

        "group": "a504ec7f.10f84",

        "order": 3,

        "width": "5",

        "height": "2",

        "passthru": false,

        "label": "刪除所有紀錄",

        "tooltip": "",

        "color": "",

        "bgcolor": "",

        "icon": "",

        "payload": "",

        "payloadType": "str",

        "topic": "",

        "x": 170,

        "y": 330,

        "wires": [

            [

                "4f6a2ea5.618"

            ]

        ]

    },

    {

        "id": "45e5a89c.dd93d8",

        "type": "ui_button",

        "z": "6868818c.c3848",

        "name": "",

        "group": "a504ec7f.10f84",

        "order": 5,

        "width": "5",

        "height": "2",

        "passthru": false,

        "label": "選擇紀錄",

        "tooltip": "",

        "color": "",

        "bgcolor": "",

        "icon": "",

        "payload": "",

        "payloadType": "str",

        "topic": "",

        "x": 150,

        "y": 250,

        "wires": [

            [

                "ff5dfb3e.455708"

            ]

        ]

    },

    {

        "id": "60aa865a.88df28",

        "type": "ui_button",

        "z": "6868818c.c3848",

        "name": "",

        "group": "a504ec7f.10f84",

        "order": 6,

        "width": "5",

        "height": "2",

        "passthru": false,

        "label": "新增紀錄",

        "tooltip": "",

        "color": "",

        "bgcolor": "",

        "icon": "",

        "payload": "",

        "payloadType": "str",

        "topic": "",

        "x": 150,

        "y": 160,

        "wires": [

            [

                "9e03c46b.a7fe88"

            ]

        ]

    },

    {

        "id": "3ea64a38.944646",

        "type": "ui_button",

        "z": "6868818c.c3848",

        "name": "",

        "group": "a504ec7f.10f84",

        "order": 4,

        "width": "5",

        "height": "2",

        "passthru": false,

        "label": "新增 資料庫 (僅能執行一次)",

        "tooltip": "",

        "color": "",

        "bgcolor": "",

        "icon": "",

        "payload": "",

        "payloadType": "str",

        "topic": "",

        "x": 200,

        "y": 80,

        "wires": [

            [

                "69706324.17d51c"

            ]

        ]

    },

    {

        "id": "a504ec7f.10f84",

        "type": "ui_group",

        "z": "",

        "name": "libedb",

        "tab": "1ca4065.bbd39ba",

        "order": 1,

        "disp": true,

        "width": "15",

        "collapse": false

    },

    {

        "id": "1ca4065.bbd39ba",

        "type": "ui_tab",

        "z": "",

        "name": "Home",

        "icon": "dashboard"

    }

]


參考來源 http://noderedguide.com/tutorial-sqlite-and-node-red/

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...