How I built a Smart Home Automation System with Angular and Node.js on a Raspberry Pi without relying on any external cloud services in just one week
Intro
In the last days I spent some nights designing and developing a home automation system based exclusively on JavaScript with the use of Angular and Node.js. And like with any other projectâââthe planning involved some deep research on the internet and it turned out that there are many fishes in the sea âplenty of solutions on how to implement a home automation system, some offer paid services in âthe cloudâ and others explain how to build your own using a technology called MQTT.None of the solutions made any sense to meâââall options were either expensive or some have inconvenient implementations or even security flaws.
But before we go any further letâs explain what MQTT is:MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging âmachine-to-machineâ (M2M) or âInternet of Thingsâ world of connected devices, and for mobile applications where bandwidth and battery power are at a premium. âMQTT.org
MQTT Publish/Subscribe Architecture
Why wasnât I convinced for using MQTT or by any of the solutions I found on the internet? Two reasons:
- While the MQTT technology seems very convenient for IoT devices, I still thought it was unnecessaryâââsimply because the system which I will be showing in the following tutorial operates in the same medium where the IoT devices live. Which means that all benefits that MQTT has for being âfastâ, âlow-bandwidthâ become irrelevantâââplus all the hassle that is involved in its implementation and all the extra over-head with the additional npm packages that are required for it to work in JavaScript environment. Instead, I will be using generic JavaScript and Node.js libraries only , nothing more!
- What about the security part? Well Iâm not a big fan of â the cloudâ or cloud computing in general, in some cases it can be very beneficial but in most cases itâs just unnecessary. Think about it: why would you have a service that is required for controlling your home appliances to be hosted somewhere else in âthe cloudâ and not in your own network?!
Comic by Geek and Poke
One might think that âthe cloudâ provides the ability to access your home appliances from anywhere in the world via the internet. But think about this: When your home network doesnât have internet connection âthe cloudâ becomes redundant, but more importantly: You can still make your home automation system accessible from the internet using using port-forwarding even if itâs hosted on your local network. This is when it âclickedâ for me and thought about hosting the whole system on a Raspberry Pi and keeping it in my local network.
A Raspberry Pi Model 3
The Technology
- Software: The reason why I chose Angular and Node.js is because itâs based on JavaScript and Iâm already familiar with it. After all, I wanted to design and develop a progressive web app that communicates with my IoT devices via HTTPâââand JavaScript offered all the functionality that IÂ needed.
- Hardware: While the system works with microcontrollers like the Arduino Uno/Mega/Du/MKR1000, Adafruit HUZZAH CC3000 or any other microcontroller with WiFi connection. I am using the ESP8266 as a base component for my home automation system. Itâs a low-cost WiFi microchip with microcontroller capability. It has everything I need and for a cheap price! Lastly, we need to host the system somewhere on our local networkâââso whatâs better than the Raspberry Pi?
This wonât be a coding tutorial where I dive deep into coding since this project is open-source and I will be publishing everything on GitHub. I will only demonstrate how to implement your own home automation system and will be going through each step. If youâre a developer please fork the repository and get involved in improving it.
The Setup
I estimate that it will take about 40 minutes to finish this whole setup plus minus any time spent online searching for solutions for installation errors.
**What is needed?**For this to work a Raspberry Pi si required. In my example Iâm using a Raspberry Pi 3, but it should work with most versions.
- Raspberry Pi board
- MicroSD card (A class 10 with 16 GB or higher is recommended)
- A USB MicroSD card reader or SD card adapter
- HDMI monitor and a USB keyboard (only required temporarily for the first boot of the Raspberry Pi)
- Ethernet cable (Not needed for Raspberry Pi 3 as it has built in WiFi)
Installing Raspbian OS on the Raspberry Pi
Raspbian is a free operating system based on Debian Linux, and it is optimized for Raspberry Pi.
I recommend the headless âLITEâ version. It has no desktop environment or any graphical user interface and itâs remotely accessible from a computer or device on the same network via SSH. Keeping things simple since this is the only way we are going to access the Raspberry Pi and the LITE version has all the functionality weâre looking for.
- Download the latest Raspbian image from the official Raspberry Pi website.
- Flash the Raspbian OS image to the SD card with Etcher or any other OS image burning software of your choice.
Setting up The Raspberry Pi
To get the Raspberry Pi ready to boot we need to:
- Insert the MicroSD card into the Raspberry Pi
- Connect the USB keyboard and the HDMIÂ cable
- Connect the Ethernet cable or if you have a Raspberry Pi 3 and want to use WiFi you should set up the network in the next section
When the Raspberry Pi has finished booting up, log in using username: pi and password: raspberry
Enabling WiFi and Connecting to the Network
Skip this step if you chose to connect with an Ethernet cable.
-
Open the wpa-supplicant configuration file
$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
2. add the following at the bottom of the file while adding your wifi name and password:
network={
ssid="your_networks_name"
psk="your_networks_password"
}
3. Press Ctrl+X to save the code. Confirm with Ythen Enter
4. Reboot the Raspberry Pi with the following command:
$ sudo reboot
Enabling SSH and Changing the Username and Password
Now that the Raspberry Pi is connected to the internet, itâs recommended to change the default password.
-
Open the Raspberry Pi configuration tool and click the second option âChange User Passwordâ and follow the instructions
$ sudo raspi-config
2. Select option 5 âInterfacing Optionsâ then activate SSH
3. Reboot the Raspberry Pi. When itâs up, you have SSH enabled and itâs ready to be accessed remotely from your desktop computer
$ sudo reboot
Configuring remote access to the Raspberry Pi
Now finally the part when we install the required software on the Raspberry Pi. This part can be executed directly on the Pi through the terminal using a HDMI monitor and a USB keyboard, but for convenienceâââand since we enable remote SSH connection, weâre going to connect from another desktop environment. This is the best and easiest way to remotely access and control the Pi whenever changes and configurations are needed.
So basically this is how you can access the command line interface of a Raspberry Pi remotely from another computer or any device on the same network using SSH. This can be done in two ways:
-
Using the Command Prompt or PowerShell (Iâm using Windows on a Desktop computer) replace with your username and IPÂ address
$ ssh username@ipaddress
If you do not know the IP address, type âhostname -I" in the Raspberry Pi command line.
2. The second method by using a client program like PuTTY or any other functioning client SSH software. Hereâs a guide on how to do it
Installing the required Software on the Raspberry Pi
Before installing anything, itâs recommended to update the Raspberry Piâs operating system and packages. Doing this regularly will keep it up to date.
-
Update the system packages list using the following command
$ sudo apt-get update
2. Upgrade all your installed packages to their latest version
$ sudo apt-get dist-upgrade
3. Download and install the latest version of Node.js
// To download
$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
// To install
$ sudo apt-get install -y nodejs
// Check if the installation was successful:
$ node -v
4. Install the Git version-control system
$ sudo apt-get install git
5. Check the current work directory using this command
$ pwd
/* It will probably print "/home/pi"
"pi" is the user directory */
Itâs recommended to clone the projectâs repository under the user directory but you can navigate somewhere else if youâre sure.
6. Clone the repository from
$ git clone https://github.com/ameer157/smarthaus.git
Make sure to navigate inside the directory using
$ cd smarthaus
Now before installing any npm packages using ânpm installâ please visit the following link and change npmâs default directory. This is very important since it will prevent any npm permission errors and allows you to install packages globally without using sudo. Using sudo with npm is not recommended and should be avoided.
7. Install all the required packages for the project
$ npm install
Itâs recommended to install the Angular CLI globally using:
$ npm install -g @angular/cli
Starting the Node.js Server
Before starting the server we need to build the project using the Angular CLI tool. And lastly, we configure the Raspberry Pi so that it runs the server whenever it boots up.
-
Build the project using
$ ng build --prod
2. Edit the rc.local file using nano
$ sudo nano /etc/rc.local
3. Add the following on the line before âexit oâ then exit and save the file
$ su pi -c 'node /home/pi/smarthaus/server.js < /dev/null &'
The Node.js server is now ready. It will run on every system boot up. Letâs run it now and see if it works using this command:
$ sudo node server
The system in now accessible from any device on your network via the Raspberry Piâs IP address. You can make it accessible from the internet using port forwarding, but itâs not recommended to implement this feature now since the system is still in development and doesnât have a user login system.
Please go ahead and fork this project and get involved in developing the missing parts đ
The End
We got ourselves a working home automation system running safely on a Raspberry Pi in our local network without the use of âthe cloudâ or somebody elseâsâ server.
Real-time device status synchronization
Adding a new device synchronising data on-demand
Fun Facts
My Raspberry Pi sitting next to my Fingbox and router in the living room đ
Rick and Morty providing tech support đ¸đ˝
I hope you enjoyed reading,Please follow and share for more tech stuff đ¤đ