here to install latest NodeJS package.
In local terminal window, install Node Red.
npm install node-red --unsafe-perm
Run command node-red
to start Node Red.
If the output shows Server now running at http://127.0.0.1:1880/, navigate to 127.0.0.1:1880 in your web browser. You should be able to see Node Red webview.
Press Ctrl+C
in terminal window to close Node Red.
(Optional) By default, the Node-RED editor is not secured - anyone who can access its IP address can access the editor and deploy changes. This is only suitable if you are running on a trusted network. See this guide to secure your Node-RED application.
In this application, we are going to learn to:
Start Node Red, and browse to http://127.0.0.1:1880
Install node-red-contrib-azure-iot-hub
via Manage Palette. Manage Palette can be found under dropdown menu from menu button at top right corner of browser window.
From left pane of the browser window, scroll and search for Azure IoT Hub Receiver node under Cloud category, then drag it into the center of dashboard.
From the same left pane under output category, drag debug node to the dashboard. (It will be renamed as msg.payload once it is placed at dashboard)
Link it to Azure IoT Hub Receiver node.
Double-click on the Azure IoT Hub Receiver node and enter your Iot Hub connectionString for your Azure IoT Hub and click Done.
Click Deploy.
You should see the below messages on your command line from where you are running NodeRED. The Azure IoT Hub Receiver node should now say 'Connected'.
Once you have messages coming into your Azure IoT Hub, you should see them in the debug pane. The Azure IoT Hub Receiver node should now say 'Received'.
We are going to create 2 widgets which show temperature and humidity of the sensor.
node-red-dashboard
using Manage Palette.Go to Hamburger Menu -> Import -> Clipboard.
Paste the following code into the "Import nodes" dialog. Then click Import.
[{"id":"4748d9e2.be7648","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"706c73f9.d1df8c","type":"azureiothubreceiver","z":"4748d9e2.be7648","name":"Azure IoT Hub Receiver","x":140,"y":60,"wires":[["3ba06501.57624a","4a231742.945ba8","6851aa80.949924"]]},{"id":"3ba06501.57624a","type":"debug","z":"4748d9e2.be7648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":380,"y":60,"wires":[]},{"id":"4a231742.945ba8","type":"ui_gauge","z":"4748d9e2.be7648","name":"Temperature","group":"d4a658b7.4c8a28","order":0,"width":"6","height":"6","gtype":"gage","title":"Temperature","label":"°C","format":"{{payload.temperature}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":429,"y":147,"wires":[]},{"id":"6851aa80.949924","type":"ui_gauge","z":"4748d9e2.be7648","name":"Humidity","group":"d4a658b7.4c8a28","order":1,"width":"6","height":"6","gtype":"gage","title":"Humidity","label":"%","format":"{{payload.humidity}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":358,"y":208,"wires":[]},{"id":"d4a658b7.4c8a28","type":"ui_group","z":"","name":"Current Reading","tab":"b8f8f747.dab748","order":1,"disp":true,"width":"12","collapse":false},{"id":"b8f8f747.dab748","type":"ui_tab","z":"","name":"Overview","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
Important !!
Import flow including Azure IoT Hub Receiver node does not include connectionString by default. You will have to provide connectionString again. Double-click on the Azure IoT Hub Receiver node and enter your Iot Hub connectionString for your Azure IoT Hub and click Done.
Click Deploy.
Browse to http://127.0.0.1:1880/ui. You should be able to see the following screen.
To know more about how to create customized Node Red Dashboard, you can visit https://randomnerdtutorials.com/getting-started-with-node-red-dashboard/ as a starting point. There are also many websites online for you to Google!
Here we will be using SendDrid service to send email for alert notification. You can use other option like Gmail service.
node-red-contrib-sendgrid
using Manage Palette in Node-RED.Create the flow by importing JSON below.
[{"id":"4748d9e2.be7648","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"706c73f9.d1df8c","type":"azureiothubreceiver","z":"4748d9e2.be7648","name":"Azure IoT Hub Receiver","x":140,"y":60,"wires":[["77746ec7.63db9","3ba06501.57624a"]]},{"id":"3ba06501.57624a","type":"debug","z":"4748d9e2.be7648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":380,"y":60,"wires":[]},{"id":"4f0d6c8e.4291e4","type":"sendgrid","z":"4748d9e2.be7648","from":"no-reply@example.com","to":"popfibo@gmail.com","name":"Send email","content":"text","x":962,"y":288,"wires":[]},{"id":"77746ec7.63db9","type":"switch","z":"4748d9e2.be7648","name":"Condition to send email","property":"payload.temperature","propertyType":"msg","rules":[{"t":"gte","v":"30","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":190,"y":291,"wires":[["167c7aa.94e2385"]]},{"id":"27bb65ef.759cba","type":"template","z":"4748d9e2.be7648","name":"Email template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Alert! The temperature from {{ deviceId }} has exceeded 30°C! Current temperature is {{ payload.temperature }}.","output":"str","x":614,"y":290,"wires":[["38930ee.12709f2"]]},{"id":"167c7aa.94e2385","type":"delay","z":"4748d9e2.be7648","name":"Limit 1 email per 5min","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":416,"y":291,"wires":[["27bb65ef.759cba"]]},{"id":"38930ee.12709f2","type":"change","z":"4748d9e2.be7648","name":"Email Settings","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"Temperature Alert","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":792,"y":289,"wires":[["4f0d6c8e.4291e4"]]}]
You will see the screen as below.
Get API key from SendGrid website.
Click Deploy.
Condition to send email
node. When the temperature exceeds 30°C, the email will be sent.Limit 1 email per 5 min
node.Email template
node allows you to decide what content to be sent in email.Email settings
node allows you to add extra parameters like topic, cc, bcc.Backend Application
UI Dashboard
Full working JSON
[{"id":"bc488bc5.7b36c8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"72280523.04ccec","type":"ui_group","z":"","name":"Current Reading","tab":"619a0ac8.53b8a4","order":1,"disp":true,"width":"12","collapse":false},{"id":"619a0ac8.53b8a4","type":"ui_tab","z":"","name":"Overview","icon":"dashboard","order":1},{"id":"74561fa2.60827","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#ffffff","edited":false},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"78f670e6.6d9bc","type":"ui_group","z":"","name":"Current Reading","tab":"78eac48a.20fd8c","order":1,"disp":true,"width":"12","collapse":false},{"id":"78eac48a.20fd8c","type":"ui_tab","z":"","name":"Overview","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"b828193e.d6c448","type":"ui_group","z":"","name":"Current Reading","tab":"91d14d66.2b94","order":1,"disp":true,"width":"12","collapse":false},{"id":"91d14d66.2b94","type":"ui_tab","z":"","name":"Overview","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"8ea2cd4f.2878c","type":"azureiothubreceiver","z":"bc488bc5.7b36c8","name":"Azure IoT Hub Receiver","x":140,"y":60,"wires":[["8f36c520.fb5e58","40b6b683.04c548","89f0aa8d.578d48","96844771.27e478"]]},{"id":"8f36c520.fb5e58","type":"debug","z":"bc488bc5.7b36c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":380,"y":60,"wires":[]},{"id":"40b6b683.04c548","type":"ui_gauge","z":"bc488bc5.7b36c8","name":"Temperature","group":"b828193e.d6c448","order":0,"width":"6","height":"6","gtype":"gage","title":"Temperature","label":"°C","format":"{{payload.temperature}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":429,"y":147,"wires":[]},{"id":"89f0aa8d.578d48","type":"ui_gauge","z":"bc488bc5.7b36c8","name":"Humidity","group":"b828193e.d6c448","order":1,"width":"6","height":"6","gtype":"gage","title":"Humidity","label":"%","format":"{{payload.humidity}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":358,"y":208,"wires":[]},{"id":"a72bcedd.3440d","type":"sendgrid","z":"bc488bc5.7b36c8","from":"","to":"","name":"Send email","content":"text","x":1056,"y":292,"wires":[]},{"id":"96844771.27e478","type":"switch","z":"bc488bc5.7b36c8","name":"Condition to send email","property":"payload.temperature","propertyType":"msg","rules":[{"t":"gte","v":"30","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":260,"y":293,"wires":[["50792deb.086f44"]]},{"id":"33d14c05.c9e964","type":"template","z":"bc488bc5.7b36c8","name":"Email template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Alert! The temperature from {{ deviceId }} has exceeded 30°C! Current temperature is {{ payload.temperature }}°C.","output":"str","x":703,"y":293,"wires":[["c97cd755.df9218"]]},{"id":"50792deb.086f44","type":"delay","z":"bc488bc5.7b36c8","name":"Limit 1 email per 5min","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":492,"y":293,"wires":[["33d14c05.c9e964"]]},{"id":"c97cd755.df9218","type":"change","z":"bc488bc5.7b36c8","name":"Email Settings","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"Temperature Alert","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":884,"y":292,"wires":[["a72bcedd.3440d"]]}]
You can implement other features in this application.
You can look into https://github.com/Azure/node-red-contrib-azure for more libraries and modules for Azure. It is up to your creativity. The other libraries you may find useful for your application:
One thing Node Red is good is that you can export your work to a JSON file. So if next time you are building a new machine and set up fresh Node Red, you can import the same file, do some configuration on nodes and you are ready to go!
Export your application
Download as JSON file
Once we have done developing the application, it is time for us to deploy it to the cloud so we can view it anywhere as long as we have Internet.
Log in to the Azure console
Select Virtual Machines option under Favourite List.
In the list of Virtual Machines, select Ubuntu Server, then click ‘Create’
Give your machine a name, the username you want to use and the authentication details you want to use to access the instance
Choose the Size of your instance. Remember that node.js is single-threaded so there’s no benefit to picking a size with multiple cores for a simple node-red instance. A1 Basic is a good starting point
On the ‘Settings’ step, click on the ‘Network security group’ option. Add a new ‘Inbound rule’ with the options set as:
- Name: node-red-editor - Priority: 1010 - Protocol: TCP - Destination port range: 1880
After a couple of minutes your instance will be running. In the console you can find your instance’s IP address.
The next task is to log into the instance then install node.js and Node-RED.
Log into your instance using the authentication details you specified in the previous stage.
Once logged in you need to install node.js and Node-RED.
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - $ sudo apt-get install -y nodejs build-essential $ sudo npm install -g node-red
At this point you can test your instance by running command below.
node-red
Note
You may get some errors regarding the Serial node - that’s to be expected and can be ignored.
Once started, you can access the editor at http://<your-instance-ip>:1880/
.
To get Node-RED to start automatically whenever your instance is restarted, you can use pm2:
sudo npm install -g pm2 pm2 start `which node-red` -- -v pm2 save pm2 startup
If you have your application exported to JSON file from this step, deployment will be super easy!
Go to Hamburger Menu -> Import -> Clipboard.
Select select a file to import
. Choose your desired JSON file to be import.
Click Import to finish your import.
Click Deploy.
For more information, please visit https://nodered.org/docs/getting-started/azure