Hello everyone.
In this project, I will show you how to start designing and coding for home automation using Node-RED on the Raspberry Pi. I will be using a Pi Zero W, and an Arduino Uno.
Okay, let's get started!
First off, you need to set up your Raspberry Pi and make sure it is running. There are lots of tutorials online on how to do that so I won't explain that as it is quite straightforward.
Once you are in the Raspian OS, you need to click the Node-RED app, which will take you to a terminal window.
If you want to set up some security for your code, check this project out!
Now, with everything installed, it's time to start doing actual work. Get to a computer and find out your Raspberry Pi IP address (your Pi should be connected to internet) and the address should look something like this: 192.168.0.105. After finding it out, type it in a web browser, adding ":1880" at the end. A Node-RED page should appear.
The EnvironmentThe picture above explains the main regions of the environment. The debug tab is used to read the messages transmitted through the flow and the deploy button uploads the flow to the raspberry pi to be executed.
Now, to add new libraries you'll have to go to the side menu, and click manage palette like in the picture below:
From there, you ll be greeted with another tab in which you can search for new nodes, such as "string" and install them.
Adding the string node (node-red-contrib-string) is highly recommended
Starting to codeFirst off, you need to code the email protocol: reading emails on an account, looking for keywords in the incoming emails and reacting to them. For this, we will have to use the email node and the switch node.
- Begin with a flow similar to the one below.
- Double click every node and you will be able to configure them
- Keep experimenting with more and more nodes until you reach your goals
The switch node(the yellow one) should be configured something like this:
Finally, the home automation flow will look like this:
There are some things to note here:
- There are 2 different "flows" as part of the same program. This is because the top line deals with a sensor value transmitted through a serial connection between the Pi Zero and the Arduino (more on that later)
- There has to be a function to deal with every output of the switch node
- There has to be only one send email node and different strings will be inputted to be sent as different emails.
The switch function (in my case) is configured using a keyword (IOTGOD) and then a command such as REPORT, TEMPERATURE, ENABLE, etc.
The output of the switch node will be exactly the keyword and command so those should serve further as triggers. Every function block has the following code template:
if(msg.payload.includes("IOTGOD ENABLE1"))
{
msg.payload="1";
}
return msg;
or for disabling the GPIO pins of the Pi Zero:
if(msg.payload.includes("IOTGOD ENABLE1"))
{
msg.payload="0";
}
return msg;
Then, you can start doing different things with it to automate your house. Likewise, you can control relays. I used a relay board and connected it to the GPIO pins of the Pi Zero and enabled or disabled them through the switch node in order to turn on appliances
ArduinoCoding
Because the Pi Zero does not have an ADC (and I couldn't find a reasonably priced one), I decided to use the usb port of the Pi and connect an arduino to it. The Arduino would then send sensor values over serial and the flow will interpret and store them.
To do this, we have to use the global object and to create a *secondary* flow as shown below:
There are 5 nodes and only 3 are essential:
- The serial node (named "Temperature"). This connects to the serial port the Arduino is connected to. To find out the port, just open up the Arduino IDE inside the Pi OS and see the name of the port.
- Then I added a delay node that just delays the data input from the Arduino
- Then I added the average node so I get the average of 10 readings as an output every time (not essential, can be skipped)
- Then I added the string node with the following configuration:
This makes sure that msg.payload is a string that contains "temperature: <value> deg C" so it will be easier to understand in email format. This step is really important for larger networks of sensors. You want to be able to distinguish which is which.
- Lastly, there is a function node, which contains the following code:
global.set("temp",msg);
And that's all. Now we have a global variable named "temp" that stored the contents of msg. If you want to email it straight away, you just need to add:
return msg;
Emailtopicsandsendingcontent
You can of course change the text/content of your email, but that will not change the subject of your email. And you need to keep you smart home organized. So, to add a subject to a message, just add the following line to the function nodes that deal with the email content:
msg.topic="email subject"
Testing
Now, to test the algorithm, you can do 2 things. You can either email every command once and look at the output on email. This way you will test your algorithm in A LONG TIME.
Or you can be smart and make use of the inject and debug nodes.
The blue nodes can simulate any message or event. In this case, you can make them send a string containing your keywords and see the messages pass through the flow.
If you add a debug node to the output of any node, you will be able to see the exact payload that specific node is carrying further in the flow and solve your issues faster.
Furtherexploration
This tutorial serves as a basis of how to create a smart home infrastructure. You can add different sensors and make use of different hardware. I will explore in depth the security concerns this specific method poses in a future tutorial.
Till then, be imaginative and create something awesome!
Comments