Skip to content

IoT alarm system with MQTT and Node-Red with UI control.

License

Notifications You must be signed in to change notification settings

SDsupun/Home-alarm-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Home-alarm-system

IoT alarm system with MQTT and Node-Red with UI control.

Reference

The login page and the login session management were taken from phyunsj/node-red-dashboard-login. The Home-alarm-system project carries an enhancement to this reference project with added MQTT and HTTP sources, SQLite database and rules for alarms.

Control UI

The Dashboard page has SensorData, CurrentStatus and Threshold columns. The SensorData column is similar to what is described in the reference. The CurrentStatus table dynamically grows when a new sensor is added. From the Threshold column, you can set the thresholds for temperature and humidity. The alarms are saved into a SQLite database.

dashboard_3

The temperature and humidity history has moved to a new page.

history_2

Node-Red Flow

node-red-r4 node-red-r2 node-red-r3
[{"id": "fecda7240c448018",         "type": "tab",         "label": "Flow 1",         "disabled": false,         "info": "",         "env": []     },     {         "id": "bd456e4e.359d8",         "type": "ui_form",         "z": "fecda7240c448018",         "name": "Dashboard Sign-In",         "label": "",         "group": "b82da486.9fc8d8",         "order": 0,         "width": 0,         "height": 0,         "options": [             {                 "label": "username",                 "value": "username",                 "type": "text",                 "required": true,                 "rows": null             },             {                 "label": "password",                 "value": "password",                 "type": "password",                 "required": true,                 "rows": null             }         ],         "formValue": {             "username": "",             "password": ""         },         "payload": "",         "submit": "submit",         "cancel": "cancel",         "topic": "",         "topicType": "str",         "splitLayout": false,         "className": "",         "x": 150,         "y": 280,         "wires": [             [                 "ea809461.f36198",                 "a926c1ff.ede37",                 "6cabb33.95cd54c"             ]         ]     },     {         "id": "6603f3ab.d89abc",         "type": "debug",         "z": "fecda7240c448018",         "name": "Debug Account Log ",         "active": false,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "true",         "x": 718.0000915527344,         "y": 241.00000762939453,         "wires": []     },     {         "id": "bc6dfbc9.d9d138",         "type": "comment",         "z": "fecda7240c448018",         "name": "Sign-In Section",         "info": "Ref. https://github.com/phyunsj/node-red-dashboard-login",         "x": 150.00000762939453,         "y": 222.99999618530273,         "wires": []     },     {         "id": "ea809461.f36198",         "type": "function",         "z": "fecda7240c448018",         "name": "Account Log",         "func": "var accountlog =  flow.get(\"accountlog\") || [] ; \n\naccountlog.push({ accessAt : new Date(), username : msg.payload.username})\n\nflow.set(\"accountlog\", accountlog);\n\nmsg.payload = accountlog;\nreturn msg;",         "outputs": 1,         "noerr": 0,         "x": 349.00000762939453,         "y": 249.99999618530273,         "wires": [             [                 "6603f3ab.d89abc"             ]         ]     },     {         "id": "a926c1ff.ede37",         "type": "function",         "z": "fecda7240c448018",         "name": "Account Verification",         "func": "var accounts =  flow.get(\"accounts\") || [ { username : \"admin\", password : \"admin\"},{ username : \"guest\", password : \"guest\"}] ; \n\nvar username = msg.payload.username ;\nvar password = msg.payload.password ; \n\nmsg.payload = 1;\n\naccounts.forEach(function ( account ){\n    if ( account.username == username ) {\n       msg.payload = 2;\n       if ( account.password == password ) {\n           msg.payload = 0;\n       } \n    }\n});\n\nif ( msg.payload == 0 ) {\n  var currentsocketid = flow.get(\"clientid\") || undefined;\n  if ( currentsocketid !== undefined && currentsocketid !== msg.socketid ) msg.payload = 3;\n}\n// keep the original socketid from msg.socketid;\nreturn msg;",         "outputs": 1,         "noerr": 0,         "x": 362.0000457763672,         "y": 295.0000057220459,         "wires": [             [                 "88afc196.8abc2"             ]         ]     },     {         "id": "88afc196.8abc2",         "type": "switch",         "z": "fecda7240c448018",         "name": "",         "property": "payload",         "propertyType": "msg",         "rules": [             {                 "t": "eq",                 "v": "0",                 "vt": "str"             },             {                 "t": "eq",                 "v": "1",                 "vt": "str"             },             {                 "t": "eq",                 "v": "2",                 "vt": "str"             },             {                 "t": "eq",                 "v": "3",                 "vt": "str"             }         ],         "checkall": "true",         "repair": false,         "outputs": 4,         "x": 531.0000419616699,         "y": 325.9999885559082,         "wires": [             [                 "eefb7b9e.f5daa8"             ],             [                 "2f57eca.40fa214"             ],             [                 "cc34d440.a91948"             ],             [                 "126fd334.ed13ed"             ]         ]     },     {         "id": "eefb7b9e.f5daa8",         "type": "function",         "z": "fecda7240c448018",         "name": "= 0 : Success",         "func": "/* activate session timer */\nvar sessionTimer =  flow.get(\"sessionTimer\") || 0; \nvar currTime = Date.now();\nflow.set(\"sessionTimer\", currTime);\nflow.set(\"clientid\", msg.socketid);\n/* ui-control payload */\nmsg.payload = { group: {\n    show : [\"Dashboard_SensorData\", \"History_History\",\n            \"Dashboard_CurrentStatus\", \"Dashboard_Threshold\"],\n    hide : [\"Dashboard_Signin\"]\n    }\n};\n\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 700.999885559082,         "y": 281.99999809265137,         "wires": [             [                 "5c0b7126.396e8"             ]         ]     },     {         "id": "2f57eca.40fa214",         "type": "function",         "z": "fecda7240c448018",         "name": "= 1 : Unknown User",         "func": "msg.payload = \"Username Doesn't exist!!!\"\nreturn msg;",         "outputs": 1,         "noerr": 0,         "x": 721.9998931884766,         "y": 319,         "wires": [             [                 "61e843e4.4d74cc"             ]         ]     },     {         "id": "cc34d440.a91948",         "type": "function",         "z": "fecda7240c448018",         "name": "= 2 : Wrong Password",         "func": "msg.payload = \"Invalid Password\";\nreturn msg;",         "outputs": 1,         "noerr": 0,         "x": 722.9999618530273,         "y": 356.0000123977661,         "wires": [             [                 "61e843e4.4d74cc"             ]         ]     },     {         "id": "5c0b7126.396e8",         "type": "ui_ui_control",         "z": "fecda7240c448018",         "name": "Go to Sensor/History Group",         "x": 988.0000686645508,         "y": 281.9999942779541,         "wires": [             []         ]     },     {         "id": "61e843e4.4d74cc",         "type": "ui_toast",         "z": "fecda7240c448018",         "position": "dialog",         "displayTime": "3",         "highlight": "",         "outputs": 1,         "ok": "OK",         "cancel": "",         "topic": "Authentication Failed",         "name": "Authentication Failure Dialog",         "x": 987.9999732971191,         "y": 339,         "wires": [             []         ]     },     {         "id": "d60f3dd8.0214b",         "type": "comment",         "z": "fecda7240c448018",         "name": "Session Management - 5 min",         "info": "Ref. \nhttps://github.com/phyunsj/node-red-dashboard-login",         "x": 179.5,         "y": 439.00000762939453,         "wires": []     },     {         "id": "643a01a8.560ea",         "type": "inject",         "z": "fecda7240c448018",         "name": "Timer",         "props": [             {                 "p": "payload"             },             {                 "p": "topic",                 "vt": "str"             }         ],         "repeat": "3",         "crontab": "",         "once": false,         "onceDelay": 0.1,         "topic": "",         "payload": "",         "payloadType": "date",         "x": 138.00000762939453,         "y": 527.9999923706055,         "wires": [             [                 "bc828c1d.b3504",                 "f6900b2.d3e9ef8"             ]         ]     },     {         "id": "bc828c1d.b3504",         "type": "function",         "z": "fecda7240c448018",         "name": "Detect Session Timer",         "func": "var sessionTimer =  flow.get(\"sessionTimer\") || 0; \nvar currTime = Date.now();\nvar SESSION_TIMEOUT = 300000; //5 min\n\nif ( sessionTimer === 0 /* Inactive, No user signed in */ ) {\n    msg.payload = 2;\n} else {\n    if ( currTime - sessionTimer > SESSION_TIMEOUT ) {\n      msg.payload = 0;  \n    } else {\n      msg.payload = 1;        \n    }\n}\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 326.00003814697266,         "y": 494.00000762939453,         "wires": [             [                 "50a18f10.e23c2"             ]         ]     },     {         "id": "50a18f10.e23c2",         "type": "switch",         "z": "fecda7240c448018",         "name": "",         "property": "payload",         "propertyType": "msg",         "rules": [             {                 "t": "eq",                 "v": "0",                 "vt": "str"             },             {                 "t": "eq",                 "v": "1",                 "vt": "str"             },             {                 "t": "eq",                 "v": "2",                 "vt": "str"             }         ],         "checkall": "true",         "repair": false,         "outputs": 3,         "x": 496.00007247924805,         "y": 492.00000762939453,         "wires": [             [                 "5b39c16e.da2f"             ],             [                 "91c7a0ab.ec163"             ],             [                 "71d9aa2e.665fd4"             ]         ]     },     {         "id": "5b39c16e.da2f",         "type": "function",         "z": "fecda7240c448018",         "name": "Time-Out",         "func": "/* activate session timer */\nvar sessionTimer =  flow.get(\"sessionTimer\") || 0; \nflow.set(\"sessionTimer\", 0);\n\nmsg.socketid = flow.get(\"clientid\") || undefined;\nmsg.payload = \"Session Expired! Please singin again!\";\nreturn msg;",         "outputs": 1,         "noerr": 0,         "x": 651.9999847412109,         "y": 465.0000114440918,         "wires": [             [                 "d29000e.556e5",                 "a3d5d44d.a71ce8"             ]         ]     },     {         "id": "d29000e.556e5",         "type": "ui_toast",         "z": "fecda7240c448018",         "position": "dialog",         "displayTime": "3",         "highlight": "",         "outputs": 1,         "ok": "OK",         "cancel": "",         "topic": "Session Timeout",         "name": "Time-Out Dialog",         "x": 839.500114440918,         "y": 431.0000114440918,         "wires": [             []         ]     },     {         "id": "8859efbd.09b1b",         "type": "ui_ui_control",         "z": "fecda7240c448018",         "name": "Go to Signin Group",         "x": 1046.5000324249268,         "y": 466.99999618530273,         "wires": [             []         ]     },     {         "id": "a3d5d44d.a71ce8",         "type": "function",         "z": "fecda7240c448018",         "name": "Sign-In Redirection",         "func": "var msg = {};\n\nmsg.socketid = flow.get(\"clientid\") || undefined;\nmsg.payload = { group: {\n    hide : [\"Dashboard_SensorData\", \n            \"History_History\",\n            \"Dashboard_CurrentStatus\", \"Dashboard_Threshold\"],\n    show : [\"Dashboard_Signin\"]\n}\n};\n\nflow.set(\"clientid\", undefined);\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 850.0000228881836,         "y": 468.00000762939453,         "wires": [             [                 "8859efbd.09b1b"             ]         ]     },     {         "id": "de95afea.546e3",         "type": "ui_toast",         "z": "fecda7240c448018",         "position": "bottom right",         "displayTime": "1",         "highlight": "",         "sendall": true,         "outputs": 0,         "ok": "OK",         "cancel": "",         "raw": false,         "className": "",         "topic": "Timer",         "name": "Display Remaining Time",         "x": 659.7222023010254,         "y": 581.9999923706055,         "wires": []     },     {         "id": "4da58462.7ddd1c",         "type": "comment",         "z": "fecda7240c448018",         "name": "Sensor Data Collection",         "info": "",         "x": 169.00000762939453,         "y": 624.0000019073486,         "wires": []     },     {         "id": "f6900b2.d3e9ef8",         "type": "function",         "z": "fecda7240c448018",         "name": "Calculating Remaining Time (Debug)",         "func": "var sessionTimer =  flow.get(\"sessionTimer\") || 0; \nvar currTime = Date.now();\nvar remainingSecs = Math.floor((300000 - (currTime - sessionTimer))/1000) + 1;\n    \nif ( sessionTimer == 0 /* Inactive, No user signed in */ ) {\n    return null;\n} else {\n    \n    if ( remainingSecs <= 7 ) msg.payload = \"Remaining : \" + remainingSecs + \" secs\";  \n    else return null;\n}\n\nmsg.socketid = flow.get(\"clientid\") || \"B1234\";\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 376.0000305175781,         "y": 582.0000200271606,         "wires": [             [                 "de95afea.546e3"             ]         ]     },     {         "id": "6d52854e.4e8d8c",         "type": "ui_text",         "z": "fecda7240c448018",         "group": "c35e46a8.b04ce8",         "order": 0,         "width": 0,         "height": 0,         "name": "Label Display",         "label": "",         "format": "{{msg.payload}}",         "layout": "row-spread",         "x": 1035.999984741211,         "y": 803.0001449584961,         "wires": []     },     {         "id": "1e3effb1.5ff58",         "type": "function",         "z": "fecda7240c448018",         "name": "Filter : temperature",         "func": "\nmsg.payload = msg.payload.value;\nmsg.topic = \"Temperature\"\nreturn msg;   ",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 470,         "y": 680,         "wires": [             [                 "ef7cf0b4.332f5",                 "38045480.64ec1c",                 "328b09bd.9901c6",                 "4381200b.380d3"             ]         ]     },     {         "id": "ef7cf0b4.332f5",         "type": "ui_gauge",         "z": "fecda7240c448018",         "name": "Gauge : Temperature",         "group": "c35e46a8.b04ce8",         "order": 0,         "width": 0,         "height": 0,         "gtype": "gage",         "title": "Temperature",         "label": "Temperature from All Sensors",         "format": "{{value}}°C",         "min": 0,         "max": "100",         "colors": [             "#66ccff",             "#ff8000",             "#ca3838"         ],         "seg1": "",         "seg2": "",         "diff": false,         "className": "",         "x": 800,         "y": 660,         "wires": []     },     {         "id": "d320c244.f24af",         "type": "function",         "z": "fecda7240c448018",         "name": "Filter : humidity",         "func": "\nmsg.payload = msg.payload.value;\nmsg.topic = \"Humidity\"\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 460,         "y": 740,         "wires": [             [                 "d4eaedd1.98978",                 "38045480.64ec1c",                 "328b09bd.9901c6",                 "4381200b.380d3"             ]         ]     },     {         "id": "d4eaedd1.98978",         "type": "ui_gauge",         "z": "fecda7240c448018",         "name": "Gauge : Humidity",         "group": "c35e46a8.b04ce8",         "order": 0,         "width": "0",         "height": "0",         "gtype": "gage",         "title": "Humidity",         "label": "Humidity from All Sensors",         "format": "{{value}}%",         "min": 0,         "max": "100",         "colors": [             "#4ad4de",             "#e6e600",             "#ca3838"         ],         "seg1": "",         "seg2": "",         "diff": false,         "className": "",         "x": 792.0000648498535,         "y": 697.0000457763672,         "wires": []     },     {         "id": "38045480.64ec1c",         "type": "function",         "z": "fecda7240c448018",         "name": "Attention : temperature & humidity",         "func": "context.data = context.data || new Object();\n\nswitch (msg.topic) {\n    case \"Temperature\":\n        context.data.temperature = msg.payload;\n        msg = null;\n        break;\n    case \"Humidity\":\n        context.data.humidity = msg.payload;\n        msg = null;\n        break;\n    default:\n        msg = null;\n    \tbreak;\n\n}\n\nif(context.data.temperature !== null && context.data.humidity !== null && context.data.temperature !== undefined && context.data.humidity !== undefined) {\n\tmsgOut = \"\"; //new Object();\n    msgOut = \"Temerature : \" \n    msgOut += context.data.temperature.toString(); \n    msgOut+= \"°C - Humidity : \"; \n    msgOut+= context.data.humidity.toString();\n    msgOut+= \"%\";\n    context.data=null;\n    var msg = {};\n    msg.payload = msgOut;\n\treturn msg;\n} \n//else return msg;",         "outputs": 1,         "noerr": 0,         "x": 795.9999847412109,         "y": 804.0001049041748,         "wires": [             [                 "6d52854e.4e8d8c"             ]         ]     },     {         "id": "328b09bd.9901c6",         "type": "ui_chart",         "z": "fecda7240c448018",         "name": "History : Temperature & Humidity",         "group": "4de9506d.e7364",         "order": 1,         "width": "14",         "height": "6",         "label": "Temperature & Humidity",         "chartType": "line",         "legend": "true",         "xformat": "HH:mm",         "interpolate": "linear",         "nodata": "No Data Yet",         "dot": false,         "ymin": "0",         "ymax": "200",         "removeOlder": "2",         "removeOlderPoints": "",         "removeOlderUnit": "3600",         "cutout": "",         "useOneColor": false,         "useUTC": false,         "colors": [             "#1f77b4",             "#aec7e8",             "#ff7f0e",             "#2ca02c",             "#98df8a",             "#d62728",             "#ff9896",             "#9467bd",             "#c5b0d5"         ],         "outputs": 1,         "useDifferentColor": false,         "className": "",         "x": 843.0000228881836,         "y": 735.0001220703125,         "wires": [             []         ]     },     {         "id": "f6f225f9.0408b8",         "type": "ui_ui_control",         "z": "fecda7240c448018",         "name": "Stay in Sensor Group",         "x": 996.5000991821289,         "y": 506.0000057220459,         "wires": [             []         ]     },     {         "id": "91c7a0ab.ec163",         "type": "function",         "z": "fecda7240c448018",         "name": "Sensor/History Goup",         "func": "var msg = {};\nmsg.socketid = flow.get(\"clientid\") || undefined;\nmsg.payload = { group: {\n    show : [\"Dashboard_SensorData\", \"History_History\",\n        \"Dashboard_CurrentStatus\", \"Dashboard_Threshold\"],\n    hide : [\"Dashboard_Signin\"]\n}\n};\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 693.5000305175781,         "y": 506.00000381469727,         "wires": [             [                 "f6f225f9.0408b8"             ]         ]     },     {         "id": "71d9aa2e.665fd4",         "type": "function",         "z": "fecda7240c448018",         "name": "Signin Goup",         "func": "var msg = {};\nmsg.payload = { group: {\n    hide : [\"Dashboard_SensorData\", \"History_History\",\n        \"Dashboard_CurrentStatus\", \"Dashboard_Threshold\"],\n    show : [\"Dashboard_Signin\"]\n}\n};\nreturn msg;",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 664.5000915527344,         "y": 547.0000419616699,         "wires": [             [                 "a2bd5885.a386c8"             ]         ]     },     {         "id": "a2bd5885.a386c8",         "type": "ui_ui_control",         "z": "fecda7240c448018",         "name": "Go to Signin Group",         "x": 984.5002479553223,         "y": 544.0000419616699,         "wires": [             []         ]     },     {         "id": "4381200b.380d3",         "type": "debug",         "z": "fecda7240c448018",         "name": "",         "active": false,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "true",         "statusVal": "",         "statusType": "auto",         "x": 759.5590667724609,         "y": 770.642463684082,         "wires": []     },     {         "id": "6cabb33.95cd54c",         "type": "debug",         "z": "fecda7240c448018",         "name": "",         "active": false,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "true",         "statusVal": "",         "statusType": "auto",         "x": 331.5000419616699,         "y": 360.5347557067871,         "wires": []     },     {         "id": "b960fa16.e7e618",         "type": "ui_toast",         "z": "fecda7240c448018",         "position": "dialog",         "displayTime": "3",         "highlight": "",         "outputs": 1,         "ok": "OK",         "cancel": "",         "topic": "System In Use.",         "name": "System In-Use Dialog",         "x": 971.1111793518066,         "y": 392.1215353012085,         "wires": [             []         ]     },     {         "id": "126fd334.ed13ed",         "type": "function",         "z": "fecda7240c448018",         "name": "= 4 : System In Use",         "func": "msg.payload = \"System is used by other user.\";\nreturn msg;",         "outputs": 1,         "noerr": 0,         "x": 712.4443283081055,         "y": 393.1215286254883,         "wires": [             [                 "b960fa16.e7e618"             ]         ]     },     {         "id": "667ff37adf073b0b",         "type": "mqtt in",         "z": "fecda7240c448018",         "name": "Motion Sensor",         "topic": "ir_sensor",         "qos": "2",         "datatype": "auto-detect",         "broker": "5c2aa58ed4582521",         "nl": false,         "rap": true,         "rh": 0,         "inputs": 0,         "x": 150,         "y": 800,         "wires": [             [                 "9b03c030358426e8",                 "0265bef3ba2a2a3b"             ]         ]     },     {         "id": "4a180788fe03b4ef",         "type": "function",         "z": "fecda7240c448018",         "name": "Motion_rule",         "func": "\nif(msg.topic == \"athome\"){\n    global.ownerathome = msg.payload.athome;\n    msg.payload.globalhome = global.ownerathome;\n    return msg;\n} \n\nif (global.ownerathome){\n    if(global.ownerathome.athome == \"no\"){\n        if (msg.topic == \"ir_sensor\" \n        && msg.payload.ir_sensor.value == \"1\") {\n            return msg;\n        }\n    }\n}\n",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 590,         "y": 940,         "wires": [             [                 "69d2f7cccddbe42f",                 "57a9fa7e80bbad7e",                 "5b6c033df04c2448",                 "6325702431e47a2a"             ]         ]     },     {         "id": "9b03c030358426e8",         "type": "join",         "z": "fecda7240c448018",         "name": "join_m_con",         "mode": "custom",         "build": "object",         "property": "payload",         "propertyType": "msg",         "key": "topic",         "joiner": "\\n",         "joinerType": "str",         "accumulate": false,         "timeout": "5",         "count": "1",         "reduceRight": false,         "reduceExp": "",         "reduceInit": "",         "reduceInitType": "",         "reduceFixup": "",         "x": 430,         "y": 940,         "wires": [             [                 "4a180788fe03b4ef"             ]         ]     },     {         "id": "79227341885442d7",         "type": "sqlite",         "z": "fecda7240c448018",         "mydb": "b609783aa9a91e56",         "sqlquery": "msg.topic",         "sql": "",         "name": "alarm_db",         "x": 1020,         "y": 1000,         "wires": [             []         ]     },     {         "id": "69d2f7cccddbe42f",         "type": "function",         "z": "fecda7240c448018",         "name": "Save Events",         "func": "if(msg.topic == \"ir_sensor\"){\n        msg.topic = `INSERT INTO event_hist(date, \n        sensor_type, sensor_no, sensor_value) \n        VALUES (datetime('now'), $sensor_type, \n        $sensor_no, $sensor_value)`;\n\n        msg.payload = [msg.payload.ir_sensor.sensor_type, \n                        msg.payload.ir_sensor.sensor_node,\n                        1];\n    return msg;\n} \n\nif(msg.topic == \"temp_sensor\"){\n        msg.topic = `INSERT INTO event_hist(date, \n        sensor_type, sensor_no, sensor_value) \n        VALUES (datetime('now'), $sensor_type, \n        $sensor_no, $sensor_value)`;\n\n        msg.payload = [msg.payload.temp_sensor.sensor_type, \n                    msg.payload.temp_sensor.sensor_node,\n                    parseFloat(msg.payload.temp_sensor.value)];\n    return msg;\n} \n\nif(msg.topic == \"humid_sensor\"){\n        msg.topic = `INSERT INTO event_hist(date, \n        sensor_type, sensor_no, sensor_value) \n        VALUES (datetime('now'), $sensor_type, \n        $sensor_no, $sensor_value)`;\n\n        msg.payload = [msg.payload.humid_sensor.sensor_type, \n                    msg.payload.humid_sensor.sensor_node,\n                    parseFloat(msg.payload.humid_sensor.value)];\n    return msg;\n} ",         "outputs": 1,         "timeout": 0,         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 790,         "y": 1000,         "wires": [             [                 "3b48bde75d22b536",                 "79227341885442d7"             ]         ]     },     {         "id": "3b48bde75d22b536",         "type": "debug",         "z": "fecda7240c448018",         "name": "debug_sql_msg",         "active": false,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "true",         "targetType": "full",         "statusVal": "",         "statusType": "auto",         "x": 1040,         "y": 1060,         "wires": []     },     {         "id": "55f9016017b41435",         "type": "ui_table",         "z": "fecda7240c448018",         "group": "fcb4731684a61d84",         "name": "sensor_current_status",         "order": 3,         "width": "7",         "height": "9",         "columns": [             {                 "field": "sensor_type",                 "title": "Sensor Type",                 "width": "",                 "align": "left",                 "formatter": "plaintext",                 "formatterParams": {                     "target": "_blank"                 }             },             {                 "field": "sensor_no",                 "title": "Sensor No",                 "width": "",                 "align": "left",                 "formatter": "plaintext",                 "formatterParams": {                     "target": "_blank"                 }             },             {                 "field": "sensor_value",                 "title": "Sensor Value",                 "width": "",                 "align": "left",                 "formatter": "plaintext",                 "formatterParams": {                     "target": "_blank"                 }             }         ],         "outputs": 0,         "cts": false,         "x": 1060,         "y": 1220,         "wires": []     },     {         "id": "0265bef3ba2a2a3b",         "type": "function",         "z": "fecda7240c448018",         "name": "Show Current Status",         "func": "if(global.sensor_table == null){\n    global.sensor_table = [{\"sensor_type\": \n                            msg.payload.sensor_type, \n                    \"sensor_no\": \n                            msg.payload.sensor_node,\n                    \"sensor_value\": \n                            msg.payload.value}];\n} else {\n    let sens_node_found = false;\n    for (let i=0;i<global.sensor_table.length;i++)\n    {\n        if(global.sensor_table[i].sensor_no \n                == msg.payload.sensor_node){\n                        global.sensor_table[i].sensor_value \n                                = msg.payload.value;\n                sens_node_found = true;\n                break;\n                }\n    }\n    if(sens_node_found == false){\n        global.sensor_table[global.sensor_table.length] \n                = {\"sensor_type\": \n                            msg.payload.sensor_type, \n                    \"sensor_no\": \n                            msg.payload.sensor_node,\n                    \"sensor_value\": \n                            msg.payload.value};\n    }\n}\nmsg.payload = global.sensor_table;\nreturn msg;",         "outputs": 1,         "timeout": 0,         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 460,         "y": 1220,         "wires": [             [                 "f5c8f92d998d74f4",                 "ce91870ddfa27aad",                 "910136c0d14640b1"             ]         ]     },     {         "id": "f5c8f92d998d74f4",         "type": "debug",         "z": "fecda7240c448018",         "name": "debug_show_msg",         "active": false,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "true",         "targetType": "full",         "statusVal": "",         "statusType": "auto",         "x": 810,         "y": 1340,         "wires": []     },     {         "id": "8489d25cc867d0d1",         "type": "comment",         "z": "fecda7240c448018",         "name": "Control inputs",         "info": "",         "x": 150,         "y": 900,         "wires": []     },     {         "id": "2ee0ba511ef6521c",         "type": "mqtt in",         "z": "fecda7240c448018",         "name": "Temp Sensor",         "topic": "temp_sensor",         "qos": "2",         "datatype": "auto-detect",         "broker": "5c2aa58ed4582521",         "nl": false,         "rap": true,         "rh": 0,         "inputs": 0,         "x": 150,         "y": 680,         "wires": [             [                 "1e3effb1.5ff58",                 "0265bef3ba2a2a3b",                 "e74b5ebbf4763cfa"             ]         ]     },     {         "id": "412fd8cae1d38e4b",         "type": "mqtt in",         "z": "fecda7240c448018",         "name": "",         "topic": "humid_sensor",         "qos": "2",         "datatype": "auto-detect",         "broker": "5c2aa58ed4582521",         "nl": false,         "rap": true,         "rh": 0,         "inputs": 0,         "x": 150,         "y": 740,         "wires": [             [                 "d320c244.f24af",                 "0265bef3ba2a2a3b",                 "afde869360f10cde"             ]         ]     },     {         "id": "c44a7dd89150e280",         "type": "ui_slider",         "z": "fecda7240c448018",         "name": "Temp Threshold",         "label": "Tempe. Threshold",         "tooltip": "",         "group": "4583918034e6223c",         "order": 1,         "width": 0,         "height": 0,         "passthru": false,         "outs": "end",         "topic": "temp_thresh",         "topicType": "str",         "min": 0,         "max": "100",         "step": 1,         "className": "",         "x": 160,         "y": 1060,         "wires": [             [                 "e74b5ebbf4763cfa"             ]         ]     },     {         "id": "da72a0fd6aa4d29f",         "type": "ui_slider",         "z": "fecda7240c448018",         "name": "Humid Threshold",         "label": "Humid. Threshold",         "tooltip": "",         "group": "4583918034e6223c",         "order": 1,         "width": 0,         "height": 0,         "passthru": false,         "outs": "end",         "topic": "humid_thresh",         "topicType": "str",         "min": 0,         "max": "100",         "step": 1,         "className": "",         "x": 170,         "y": 1120,         "wires": [             [                 "afde869360f10cde"             ]         ]     },     {         "id": "ce91870ddfa27aad",         "type": "change",         "z": "fecda7240c448018",         "name": "clear out the table",         "rules": [             {                 "t": "set",                 "p": "payload",                 "pt": "msg",                 "to": "[]",                 "tot": "json"             }         ],         "action": "",         "property": "",         "from": "",         "to": "",         "reg": false,         "x": 810,         "y": 1220,         "wires": [             [                 "55f9016017b41435"             ]         ]     },     {         "id": "910136c0d14640b1",         "type": "delay",         "z": "fecda7240c448018",         "name": "",         "pauseType": "delay",         "timeout": "1",         "timeoutUnits": "milliseconds",         "rate": "1",         "nbRateUnits": "1",         "rateUnits": "second",         "randomFirst": "1",         "randomLast": "5",         "randomUnits": "seconds",         "drop": false,         "outputs": 1,         "x": 790,         "y": 1280,         "wires": [             [                 "55f9016017b41435"             ]         ]     },     {         "id": "ab7b124a36ca3398",         "type": "function",         "z": "fecda7240c448018",         "name": "Temp_rule",         "func": "\nif (msg.topic == \"temp_thresh\"){\n    global.temp_thresh = msg.payload.temp_thresh;\n    msg.payload.temp_thresh = global.temp_thresh;\n    return msg;\n} \n\nif (msg.topic == \"temp_sensor\") {\n    if (parseFloat(msg.payload.temp_sensor.value) \n        > global.temp_thresh) {\n        return msg;\n    }\n} \n\n",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 590,         "y": 1000,         "wires": [             [                 "69d2f7cccddbe42f",                 "57a9fa7e80bbad7e",                 "6325702431e47a2a"             ]         ]     },     {         "id": "e74b5ebbf4763cfa",         "type": "join",         "z": "fecda7240c448018",         "name": "join_t_con",         "mode": "custom",         "build": "object",         "property": "payload",         "propertyType": "msg",         "key": "topic",         "joiner": "\\n",         "joinerType": "str",         "accumulate": false,         "timeout": "5",         "count": "1",         "reduceRight": false,         "reduceExp": "",         "reduceInit": "",         "reduceInitType": "",         "reduceFixup": "",         "x": 430,         "y": 1000,         "wires": [             [                 "ab7b124a36ca3398"             ]         ]     },     {         "id": "b56df0452b9e9818",         "type": "function",         "z": "fecda7240c448018",         "name": "Humid_rule",         "func": "\nif (msg.topic == \"humid_thresh\") {\n    global.humid_thresh = msg.payload.humid_thresh;\n    msg.payload.humid_thresh = global.humid_thresh;\n    return msg;\n}\n\nif (msg.topic == \"humid_sensor\") {\n    if (parseFloat(msg.payload.humid_sensor.value)\n        > global.humid_thresh) {\n        return msg;\n    }\n} ",         "outputs": 1,         "timeout": "",         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 590,         "y": 1060,         "wires": [             [                 "57a9fa7e80bbad7e",                 "69d2f7cccddbe42f",                 "6325702431e47a2a"             ]         ]     },     {         "id": "afde869360f10cde",         "type": "join",         "z": "fecda7240c448018",         "name": "join_h_con",         "mode": "custom",         "build": "object",         "property": "payload",         "propertyType": "msg",         "key": "topic",         "joiner": "\\n",         "joinerType": "str",         "accumulate": false,         "timeout": "5",         "count": "1",         "reduceRight": false,         "reduceExp": "",         "reduceInit": "",         "reduceInitType": "",         "reduceFixup": "",         "x": 430,         "y": 1060,         "wires": [             [                 "b56df0452b9e9818"             ]         ]     },     {         "id": "57a9fa7e80bbad7e",         "type": "debug",         "z": "fecda7240c448018",         "name": "Debug rules",         "active": true,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "true",         "targetType": "full",         "statusVal": "",         "statusType": "auto",         "x": 790,         "y": 1060,         "wires": []     },     {         "id": "0af160f6df593af5",         "type": "comment",         "z": "fecda7240c448018",         "name": "Alarm rules",         "info": "",         "x": 430,         "y": 880,         "wires": []     },     {         "id": "67f62db72a13e257",         "type": "comment",         "z": "fecda7240c448018",         "name": "Save Critical Events",         "info": "",         "x": 1030,         "y": 940,         "wires": []     },     {         "id": "47a7be585c0ca3d2",         "type": "comment",         "z": "fecda7240c448018",         "name": "Show Current Status",         "info": "",         "x": 450,         "y": 1160,         "wires": []     },     {         "id": "6da3f86c7e4eb795",         "type": "http in",         "z": "fecda7240c448018",         "name": "Set At Home",         "url": "/set_at_home",         "method": "put",         "upload": false,         "swaggerDoc": "",         "x": 150,         "y": 960,         "wires": [             [                 "209cfd08996aef29"             ]         ]     },     {         "id": "5b6c033df04c2448",         "type": "http response",         "z": "fecda7240c448018",         "name": "Set At Home success",         "statusCode": "0",         "headers": {},         "x": 820,         "y": 940,         "wires": []     },     {         "id": "209cfd08996aef29",         "type": "function",         "z": "fecda7240c448018",         "name": "Set Topic",         "func": "msg.topic = \"athome\";\nreturn msg;",         "outputs": 1,         "timeout": 0,         "noerr": 0,         "initialize": "",         "finalize": "",         "libs": [],         "x": 140,         "y": 1020,         "wires": [             [                 "9b03c030358426e8"             ]         ]     },     {         "id": "6325702431e47a2a",         "type": "http request",         "z": "fecda7240c448018",         "d": true,         "name": "Send Alarms",         "method": "POST",         "ret": "txt",         "paytoqs": "ignore",         "url": "http://localhost:8080",         "tls": "",         "persist": false,         "proxy": "",         "insecureHTTPParser": false,         "authType": "",         "senderr": false,         "headers": [             {                 "keyType": "Content-Type",                 "keyValue": "",                 "valueType": "application/json",                 "valueValue": ""             }         ],         "x": 790,         "y": 1140,         "wires": [             [                 "448ba8b6072e4a61"             ]         ]     },     {         "id": "448ba8b6072e4a61",         "type": "debug",         "z": "fecda7240c448018",         "name": "debug send alarm",         "active": false,         "tosidebar": true,         "console": false,         "tostatus": false,         "complete": "payload",         "targetType": "msg",         "statusVal": "",         "statusType": "auto",         "x": 1030,         "y": 1140,         "wires": []     },     {         "id": "8153c5b2a6bc57e4",         "type": "comment",         "z": "fecda7240c448018",         "name": "Send Alarms from REST API",         "info": "",         "x": 840,         "y": 1100,         "wires": []     },     {         "id": "b82da486.9fc8d8",         "type": "ui_group",         "name": "Signin",         "tab": "d65502b1.ee517",         "order": 1,         "disp": true,         "width": "6",         "collapse": false     },     {         "id": "c35e46a8.b04ce8",         "type": "ui_group",         "name": "SensorData",         "tab": "d65502b1.ee517",         "order": 2,         "disp": true,         "width": "6",         "collapse": false,         "className": ""     },     {         "id": "4de9506d.e7364",         "type": "ui_group",         "name": "History",         "tab": "c6089bd7247a8361",         "order": 3,         "disp": true,         "width": "14",         "collapse": false,         "className": ""     },     {         "id": "5c2aa58ed4582521",         "type": "mqtt-broker",         "name": "safe_home_mqtt",         "broker": "192.168.8.103",         "port": "1883",         "clientid": "",         "autoConnect": true,         "usetls": false,         "protocolVersion": "4",         "keepalive": "60",         "cleansession": true,         "autoUnsubscribe": true,         "birthTopic": "",         "birthQos": "0",         "birthRetain": "false",         "birthPayload": "",         "birthMsg": {},         "closeTopic": "",         "closeQos": "0",         "closeRetain": "false",         "closePayload": "",         "closeMsg": {},         "willTopic": "",         "willQos": "0",         "willRetain": "false",         "willPayload": "",         "willMsg": {},         "userProps": "",         "sessionExpiry": ""     },     {         "id": "b609783aa9a91e56",         "type": "sqlitedb",         "db": "/home/supun/my_sqlite/alarm_app.db",         "mode": "RW"     },     {         "id": "fcb4731684a61d84",         "type": "ui_group",         "name": "CurrentStatus",         "tab": "d65502b1.ee517",         "order": 3,         "disp": true,         "width": "7",         "collapse": false,         "className": ""     },     {         "id": "4583918034e6223c",         "type": "ui_group",         "name": "Threshold",         "tab": "d65502b1.ee517",         "order": 4,         "disp": true,         "width": "6",         "collapse": false,         "className": ""     },     {         "id": "d65502b1.ee517",         "type": "ui_tab",         "name": "Dashboard",         "icon": "dashboard",         "order": 1     },     {         "id": "c6089bd7247a8361",         "type": "ui_tab",         "name": "History",         "icon": "dashboard",         "disabled": false,         "hidden": false     } ]

Future work

  • Need to improve the security.
  • Need to create the end-user.

About

IoT alarm system with MQTT and Node-Red with UI control.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published