Background:
The use of Internet-of-Thing (IoT) brings about myriad benefits and is increasingly transforming the way we live, making our life easier. Traditionally, the control of our home devices such as turning on or off the lights, boiling water in a kettle or cooling the house with an air conditioner requires manual human intervention. However, with the use of IoT, we can remotely control or even automate these operations in our home.
Imagine your alarm clock ringing in the morning, your room light brightness gets brighter every 2 minutes, your room’s blind is slowly letting sun in, your water heater gets switched on, hot water starts boiling in your kitchen (and that list goes on….), how amazing will that be?
In this article, I will touch on a software tool titled Node-Red and share two flows on how you can control your smart devices and even automating them.
Set up:
For this article, the following hardware were used:
- Raspberry Pi
- CC 2531 (Coordinator)
- Xiaomi Aqara smart LED bulb
- Xiaomi Aqara wireless switch
The following software were installed on the raspberry pi:
- Mosquitto — The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.
- ZigBee2Mqtt — Allows you to use your ZigBee devices without the vendors bridge or gateway. It bridges events and allows you to control your ZigBee devices via MQTT. In this way you can integrate your ZigBee devices with whatever smart home infrastructure you are using.
- Node-Red — Node-Red is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click. Node-Red allows the creation of a user interactive dashboard to remotely control or monitor your IoT devices.
*I will like to stress on the importance of having a secure set up for all the software tools required for installation. Ensure that you secure your Smart Home set up by having authentication for Mosquitto and Node-Red, SSL for transport, replace default network keys etc. In a separate post, i shared on the potential danger associated to ZigBee IoT devices.
Architecture
About Node-Red Flows:
Every flow in Node-Red requires the use of an input and output node. In between the input and output node, you could have functions that checks the data and react differently depending on the input. For example, if the wireless switch is clicked once, the smart light bulb will turn on and if the wireless switch is clicked twice, the smart light bulb will turn off.
JSON (JavaScript Object Notation) is a syntax for exchanging data. In the ZigBee2MQTT set up, JSON is used to control the smart devices. For example, to turn on the smart light bulb, set the inject node (Figure 3: On Smart Light) properties to send the following JSON data {“State”:”ON”}. The MQTT publisher node (Figure 3: Light Toggle) will publish the JSON data to the MQTT broker which will be forwarded to the smart light bulb, turning on the light.
When building your smart home with Node-Red, there may be times where error is encountered. To aid with resolving errors, subscribe to all topics using the # sign and output them to a debug message node.
Now you should have a better idea on how Node-Red works, and with that knowledge, we will move on to 2 simple flows.
Flow 1: Using wireless switch to control your smart light
In this flow, we will learn how to control the smart light bulb using a portable wireless switch. After fiddling around with the wireless switch, from the ZigBee2MQTT logs, the JSON data contained a key titled ‘click’, with values being either ‘single’, ‘double’ or ‘triple’. This means that we can program up to a maximum of three different outcome of our choice.
To begin, insert a MQTT input node. Under the properties, subscribe to the portable wireless switch device ID. This helps filter other ZigBee IoT device messages and execute the flow only when the wireless switch is clicked.
Place a JSON message converter. I’ve noted that in the absence of this JSON converter node, the subsequent nodes in the flow will not be able to interpret my message.payload data.
Thirdly, add a switch node that reads the value of the ‘click’ property. The switch node have two branches that will flow accordingly depending on the value ‘click’ property.
If the consumer performs a single click on the wireless switch, the smart light bulb will turn on. To do so, send the {“State”:”ON”} JSON data to the light bulb, and for double click, we will send a {“State”:”OFF”} to turn the light bulb off.
Lastly, we will want to publish the JSON data to control the Smart light bulb.
Flow 2: Wake up lights
In this wake up lights flow, we will learn how can one automate smart devices.
To begin, we will start off with the creation of an inject node, setting its properties to turn on the light and repeat at a specific time interval each day. You can also replace the input node using a dashboard input field, allowing end users to fill up a form that contains the desired time, which days of the week and any other options for the your home automation. For the dashboard, you have to install an optional module titled node-red-dashboard.
Subsequently, I used a function node to declare a new flow variable, setting the initial light bulb brightness.
In my case, I created a loop that executes subsequent nodes in the flow for 5 consecutive times with a 5 second interval.
The first function it will perform is to increase the brightness of the light bulb. Using the flow variable we created earlier, we will continuously increase its brightness by 35. We will then publish the JSON data to ensure the light state is on and updating the bulb brightness.
The second function it will perform is playing an audio. In this flow, I used the UI audio output which required a string as an input. You can send string messages by installing an optional module titled node-red-contrib-string.
Summary
As you have seen from this article, setting up your smart home is affordable, easy to do so using Node-Red and brings convenience to end users. Node-Red offers a lot of other optional modules created by the community that can help with the design and creation of your smart home flows. I hope this article found you well and do share any interesting flows!