INSTAR Deutschland GmbHINSTAR Deutschland GmbH

Node-RED and MQTT Introduction



Creating your first Flows

You can find the exported source code for this example at the end of the article - The JSON code contains a line "msg.payload = 'http://192.168.1.116/param.cgi?cmd=pushhostalarm&-usr=admin&-pwd=instar'; you can already exchange the IP address and camera login with the information used on your personal camera. Also you can see that the CGI command "name":"/param.cgi?cmd=pushhostalarm" is used in the example. This command will trigger an alert on your INSTAR Full HD Camera - it will not work with HD or VGA cameras! But you can use any other CGI command from our documentation for Full HD, HD and VGA camera models.

[
  {
    "id": "b1aa3b11.229a98",
    "type": "function",
    "z": "13bcbd83.ab1fe2",
    "name": "Alarm",
    "func": "msg.payload = 'http://192.168.1.116/param.cgi?cmd=pushhostalarm&-usr=admin&-pwd=instar';\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "x": 310,
    "y": 160,
    "wires": [
      [
        "a5ae1b98.91c1a8"
      ]
    ]
  }
]

Open the import dialogue, paste the code in and click import to submit:


Node-RED


MQTT

You should be able to see the following flow now:


Node-RED


Let's start reading the flow to understand what is happing here. In the top left you see a node with an icon of a hand pointing up and the title Alarm. This is a UI node that belongs to our dashboard. If you scroll down the list of available nodes on the left, you will also find it in the dashboard section - it is a simple button. It is connected to an MQTT output node with the title Push Alarm. When you double-click the button node, you will see that it carries a payload of { "val" : "alarm"}. Double-clicking the MQTT node shows you that this payload is published to localhost:1880 with a topic of ipcam/alarm/pushalarm.

For information on how to install a MQTT Broker in Windows - please check out our OpenHAB2 Introduction

Unlike the HTTP REST API used to communicate with your camera, MQTT communication is based on a publish and subscription model. Node-RED works as a MQTT broker that receives JSON payloads that are published and makes sure that they are delivered to every client that has a subscription for it. When you double-click the MQTT output node again, you will see a QoS field that is set to the value of 1. QoS stands for Quality of Service. If set to 0 the published message is send once to available clients that are currently reachable. The value 1 signifies that the message is retained by the MQTT broker and whenever a client comes online, that did not receive it yet, it will be resend - making sure that every client is notified about the change. The value 2 would guaranty that each message is only send once to each subscribed client - which is useful for time series data, but not necessary for our use case.


Node-RED


  1. The client that is subscribed to our button is the flow below. You can see that it starts with a MQTT input node that is subscribed to ipcam/alarm/pushalarm. Whenever you click the button in the dashboard the value { "val" : "alarm"} is published and pushed to that flow.
  2. The next node is a function node that extract the value of val - in our case it is always alarm. But if you have picked another CGI command before, you might want to add several buttons that publish different values and that trigger different commands.
  3. In the next node - titled Push - those different values are sorted out. In our case we just have to listen to alarm and send it to exit 1.
  4. Connected to this output is another function node that is triggered and adds the camera IP, login and the CGI command to the payload
  5. Which is then set from msg.payload to msg.url
  6. To be consumed by a HTTP node that sends out the CGI command and receives the camera response.
  7. We have to turn the response into valid JSON - either { "alarm": "triggered" } or { "alarm": "Error" }
  8. And read it into a JavaScript object
  9. To be consumed by another UI node. This UI element is a text field configured to display {{msg.payload.alarm}} - which is either triggered or Error if something went wrong along the way.

Deploy your Flows

Click on the big Red Deploy button in the top right and open http://localhost:1880/dashboard (the URL was set in an earlier step) and you will see the following interface:


Node-RED


Click on the Alarm Button and if everything went right, you should see a triggered next to Alarm Push. You can also check the System Log of your camera to verify that the Alarm was actually triggered (the alarm shows up as Audio Alarm):


Node-RED


Exported Sourcecode

Copy the complete, following source code and import it to your Node-RED Flow:

[{"id":"cdec87d0.5ff618","type":"mqtt in","z":"13bcbd83.ab1fe2","name":"Push Alarm","topic":"ipcam/alarm/pushalarm","qos":"1","broker":"47feb3e4.56f11c","x":80,"y":220,"wires":[["8838d57.9ce2228"]]},{"id":"8838d57.9ce2228","type":"function","z":"13bcbd83.ab1fe2","name":"process value","func":"msg.payload = JSON.parse(msg.payload).val;\nreturn msg;","outputs":1,"noerr":0,"x":130,"y":160,"wires":[["3d3ea3f8.d96a9c"]]},{"id":"3d3ea3f8.d96a9c","type":"switch","z":"13bcbd83.ab1fe2","name":"Push","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"alarm","vt":"str"}],"checkall":"false","repair":false,"outputs":1,"x":250,"y":220,"wires":[["b1aa3b11.229a98"]]},{"id":"b1aa3b11.229a98","type":"function","z":"13bcbd83.ab1fe2","name":"Alarm","func":"msg.payload = 'http://192.168.1.116/param.cgi?cmd=pushhostalarm&-usr=admin&-pwd=instar';\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":160,"wires":[["a5ae1b98.91c1a8"]]},{"id":"994abd71.13054","type":"string","z":"13bcbd83.ab1fe2","name":"toJSON","methods":[{"name":"replaceAll","params":[{"type":"str","value":"[Succeed]set ok."},{"type":"str","value":"{ \"alarm\": \"triggered\" }"}]},{"name":"replaceAll","params":[{"type":"str","value":"[Error]Param error."},{"type":"str","value":"{ \"alarm\": \"Error\" }"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":570,"y":220,"wires":[["d3e3b6d2.67bf78"]]},{"id":"d3e3b6d2.67bf78","type":"json","z":"13bcbd83.ab1fe2","name":"","property":"payload","action":"","pretty":false,"x":660,"y":160,"wires":[["c8b27f25.d85df"]]},{"id":"c8b27f25.d85df","type":"ui_text","z":"13bcbd83.ab1fe2","group":"c0689660.4e98c8","order":2,"width":0,"height":0,"name":"UI Alarm","label":"Alarm Push","format":"{{msg.payload.alarm}}","layout":"row-spread","x":660,"y":100,"wires":[]},{"id":"a5ae1b98.91c1a8","type":"change","z":"13bcbd83.ab1fe2","name":"Set URL","rules":[{"t":"set","p":"url","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":220,"wires":[["ea5d1e02.b9b4"]]},{"id":"ea5d1e02.b9b4","type":"http request","z":"13bcbd83.ab1fe2","name":"pushhostalarm","method":"GET","ret":"txt","url":"","tls":"","x":485,"y":160,"wires":[["994abd71.13054"]]},{"id":"2dceef9b.7eebe","type":"mqtt out","z":"13bcbd83.ab1fe2","name":"Push Alarm","topic":"ipcam/alarm/pushalarm","qos":"1","retain":"","broker":"47feb3e4.56f11c","x":200,"y":80,"wires":[]},{"id":"4d2f09f7.2491b8","type":"ui_button","z":"13bcbd83.ab1fe2","name":"Alarm","group":"c0689660.4e98c8","order":0,"width":0,"height":0,"passthru":false,"label":"Trigger Alarm!","color":"#ffffff","bgcolor":"#f3100b","icon":"fa-bell","payload":"{ \"val\" : \"alarm\"}","payloadType":"json","topic":"pushalarm","x":70,"y":80,"wires":[["2dceef9b.7eebe"]]},{"id":"47feb3e4.56f11c","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"c0689660.4e98c8","type":"ui_group","z":"","name":"Wiki Tutorial","tab":"7a3391b6.03a81","disp":true,"width":"6","collapse":false},{"id":"7a3391b6.03a81","type":"ui_tab","z":"","name":"Wiki Tutorial","icon":"dashboard"}]