A Beginner's Guide to Home Automation with the Internet of Things (IoT)

Written by rakeshch | Published 2020/12/24
Tech Story Tags: iot | automated-home | home-automation | home-assistant | node-red | ifttt | wemos | raspberry-pi

TLDR A Beginner's Guide to Home Automation with the Internet of Things (IoT) Rakesh Ch Software Developer and IoT Hobbyist explains how to use IoT/Smart devices. IoT is a network of devices, devices that are embedded with sensors, software, and other technologies with the sole purpose of connecting and exchanging data with other devices over the internet. The user sends a signal to a smart device to turn on or turn on a light, which in turn sends that signal directly to the cloud. Home Assistant is one software that provides a platform/Gateway/Hub functionality.via the TL;DR App

Home Automation does what it says - It enables various smart devices around the home to work together in an automated way based on instructions/configurations.
Home automation can be achieved using IoT/Smart devices. IoT is a network of devices, devices that are embedded with sensors, software, and other technologies with the sole purpose of connecting and exchanging data with other devices over the internet. IoT enables devices to interact, collaborate, and learn from each other's experiences just like humans do. Some of the examples of IoT devices are below.
  • Connected appliances.
  • Smart home security systems.
  • Smart Switches.
  • Smart Locks.
  • Smart Energy Meters.
Below are a few day-to-day activities which I automated
  1. Turn on the water motor when the water supply starts.
  2. Lights Automation and Home Intrusion Detection.
  3. Using an Automatic shut-off valve directing water flow.
  4. Turn on/off lights and fans in the living room

In this blog post, I will explain all software, tools, devices, services that were used by me for achieving the top two automation flows.
Home Automation has three components.
  1. Hardware.
  2. Software/Apps.
  3. Communication Protocols.

Hardware

1) Sensors like (Water flow sensor, Pressure sensor, Temperature sensor, Door sensor, PIR sensor, Water level sensor).
 I used a Water flow sensor, Door sensor, and PIR sensor.
2) ESP8266 boards like (Arduino UNO, Wemos D1 R2, RaspberryPi Zero). We can connect the mentioned sensors to these boards and send the key data to the hub.
I used Wemos D1 R2 which contains a Wi-Fi chip. Flashed this device with Tasmota and connected it to the local hub.
3) Sonoff devices. Sonoff is a brand of low-cost (5$) Wi-Fi switches that can be used for controlling main-powerline connected appliances like lights, pumps, heaters, etc. They consist of the ESP8266 Wi-Fi Chip plus additional circuitry. and are made by ITEAD Studio, which also makes a collection of home automation products.
We can operate these devices using the mobile app. The user sends a signal to a smart device to turn on or off a light, which in turn sends that signal directly to the cloud. Cloud servers understand, process the data, and take necessary action for the targeted home appliance. For Example, consider the Philips Hue bulb. When we turn on the bulb from the mobile app. It sends the request to the Philips cloud server and the cloud server sends the request to the edge device (bulb) to turn on.
Alternatively, we can flash these devices with open-source firmware like Tasmota and configure the devices for connecting to our local gateway or hub. Home Assistant is one such software that provides a platform/Gateway/Hub functionality. which can act as a cloud server.
I am using 10 Sonoff devices. Few of those are running with default firmware connected to manufacturer cloud and few devices are flashed with Tasmota firmware connected to the local hub.
4) TpLink smart plug.
5) Sonoff 433RF Bridge- 433Mhz RF receiver module is low power consumption, high sensitivity radio receiver module. It uses internal software decoding and data decoding circuits. Few examples are - Door sensor, PIR sensor transmits an RF message. The bridge captures the RF message and sends it to the cloud server for further processing.
For my use-case, I have flashed the firmware of the device with OpenMQTTGateway (OMG) and configured it to send data to the local hub for further processing of the data.

Softwares

1) Embedded C programs – Wrote an embedded C program and installed it onto Wemos D1 R2 board. The code measures the water flow rate and publishes the flow rate as an MQTT message on a specific topic to the MQTT broker which is running on Hub.
I replaced this implementation with Tasmota firmware
2) OpenMQTTGateway (OMG) - OMG supports basic 433mhz/315mhz protocols & infrared (IR) with which we can turn old dumb devices - "smart".  and has the lower cost advantage when compared with Zwave or any other sophisticated protocols. OMG is evolving and its recent addition is Bluetooth Low Energy (BLE) and LORA.
In my use case, it enables to:
  • Receive MQTT data from a topic and send RF 433Mhz signal
  • Publish MQTT data to a topic related to the source of the 433Mhz signal. 
3) 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. The core of Node-RED is Node.js.
I created a couple of data-flows using this software
4) IFTTT- IFTTT derives its name from the programming conditional statement “if this, then that.” It provides a software platform that connects apps, devices, and services from different developers in order to trigger one or more automation involving those apps, devices, and services. There are numerous ways you can connect all your services and the resulting combinations are called “Applets”. Applets connect two or more services together and help you do something that you couldn’t do with just one service alone.
Here are just two “if this then that” automation you can run with IFTTT:
  • If you make a call on your Android phone, then a log of that call is added to a Google Spreadsheet.
  • If you add a new task to your Amazon Alexa to-dos, then it will be added to your iOS Remainders app.
I implemented few applets in IFTTT, one of them sends a mobile notification with the message “Water supply started” and another applet to turn on "Water motor".
5) Tasmota - Tasmota is custom firmware that is available for ESP8266 based devices allowing for web, timer, OTA firmware updates, and sensor support. Tasmota allows ESP8266 based devices to be controlled via HTTP, MQTT, and KNX allowing for any home automation integration.
6) Home Assistant – Home Assistant is an open-source home automation system. It supports RaspberryPi, uses Python with Hassbian OS. It has simplified automation rules that developers can use to build their home automation product saving them thousands of lines of code. Home Assistant acts as a central smart home controller hub that features all common conditional statements, creator functions to control any modern home automation platform. It also includes gateway and bridge (Protocol converters) for devices and services using different IoT technologies.
If you want complete control of your devices then the use of a home assistant is recommended and it also has great community support and robust integrations solutions for multiple smart devices. Some of the integrations are mentioned below.
Alternatives of Home Assistant are OpenHAB (written in java), Calaos, FHEM, Domoticz, EEDOM, etc.,
With references from the Home assistant community portal. I could run Home Assistant on a RaspberryPI and configured a few integrations like MQTT broker, NodeRed, IFTTT, PushOver (for sending push notifications to mobile), Tasmo-Admin, motionEye.

Communication Protocols.

  1. Zigbee- Zigbee is a wireless protocol that operates in a mesh network. It uses a device to relay a signal to other devices, helps in strengthening and expanding the network. Zigbee can be built-in dimmers, door locks, thermostats, and more. It’s used on WeMo and Philips Hue.
  2. Z-Wave- Zwave is similar to Zigbee, Z-Wave is an open-source mesh network protocol. Technically speaking, the main difference between the two is the data throughput – Z-wave is slower than Zigbee, however, it requires less energy to cover the same range as Zigbee. SmartThings and Lowes Iris use Z-Wave.
  3. Infrared- Infrared is one of the simplest and most reliable protocols, normally offering one-way communication. It’s the number one choice for remote controls, like the one for your TV.
  4. Bluetooth- Bluetooth is a short-range (around 10m) wireless protocol often used on phones, headphones, and speakers. Its adaptive frequency hopping system detects existing signals, such as Wi-Fi, and negotiates a channel map for the Bluetooth devices in order to minimize interference.
  5. Wi-Fi- fast and reliable wireless communication, with a range of around 25m.
  6. Radio Frequency (RF)- RF is very common. Instead of sending out light signals, an RF device transmits radio waves that correspond to the binary command for the button you're pushing. A radio receiver on the controlled device receives the signal and decodes it. Sonoff RF 433Mhz bridge is a receiver.

Automation 1: Turn on the water-motor when the water supply starts.

Once the water supply starts my automation-goal is to perform the below actions.
  • Send a push notification to my mobile saying the water supply started.
  • Turn on one of the water motors.
  • Activate the loud voice-alarm device at home.
  • Activate a shut-off valve device, which directs water to the tank.
Flow Sensor and Wemos
  • The flow sensor is connected to wemos(with Tasmota firmware).
  • The flow sensor is connected to GPIO14 pin, power pin, and ground pin of the wemos device.
  • On the same wemos device I configured MQTT, Wi-Fi, which will publish a telemetry message every 30 seconds. The payload of the message is going to be a timestamp and the pulse counter value. When water flows through the sensor, it increments the counter and publishes the message to the hub.

Hub with Home Assistant on Raspberry Pi

  1. Using Node-Red integrations on Home Assistant, I created two data-flows on Node-Red. 
  2. In the first flow, a subscription to the MQTT topic using the MQTT node is configured, which receives the telemetry message every 30 seconds. The message is fed to a function node which checks the counter value. If the value is greater than 20 it allows the message to pass through other nodes in the flow.
  3. After successful validation, a push notification is sent and the water motor is turned-on, Below data-flow explains its working:
    • Node-red IFTTT triggers call the webhooks associated with an applet. 
    • IFTTT sends requests to the Pushover server which in turn sends a push notification to mobile. IFTTT also sends a trigger to the Sonoff cloud-server to activate the sonoff connected water motor.
    • In my IFTTT account, I configured both sonoff and pushover server account details.
    Flow1
    In the node-red flow 2. Every day at 6 am below actions are performed-
    • The hub resets the flow sensor valve to 0 by restarting the wemos device.
    • Turn off the shut-off valve.
    Flow 2
    Automation1 Images
    Installed wemos device in an IP65 rating case.
    Flow sensor connected to the wemos device.
    Configured flow sensor in wemos device. Module. Tasmota considers it as a sonoff basic device. There are similar screens for configuring Wi-Fi and MQTT broker details on the device.
    Sonoff Device

    Automation 2: Lights Automation and Home Intrusion Detection

    In this automation, all devices communicate with the local hub and external pushover server. The door sensor is fixed to the main entrance door and the PIR sensor is fixed near the stairs.
    Goals that I want to achieve with this automation.
    • Turn on the staircase and balcony lights at sunset. 
    • Turn off lights at 21:30 pm.
    • Turn on/off lights for only 5mins between 09:30 PM to 06:00 AM using PIR sensor/Door Sensor
    • Intrusion alert via push notification from 09:30 to 06:00 AM via PIR sensor if stairs are used
    The Home Assistant sun integration will use my current location to track if the sun is above or below the horizon. Based on this data it will turn on lights at sunset and turn off those lights at 09:30 PM every day.
    Sensors
    The Door sensor and PIR sensors transmit RF signals. Door sensors transmit when the door is opened and the PIR sensor transmits an RF signal when it detects any object within its range. Both these sensors work under the 433Mhz band. 
    Sonoff RF bridge
    When these sensors transmit RF signals it gets captured by the Sonoff RF bridge. The purpose of the bridge is to consume RF signals and convert them to a unique code that gets published using MQTT protocol to MQTT broker over a topic. 
    Home Assistant on Raspberry Pi
    • MQTT broker is running in the hub. For the bridge to transmit data to the local hub instead of its cloud server. I used OpenMQTTGateway (OMG) firmware and it is connected to wifi. 
    • In the hub, few automation flows are configured. These flows get activated based on the current time and if they receive the MQTT message containing that unique code transmitted by the bridge. This confirms that either of the sensors got activated so the hub executes the configured actions in the flow.
    IFTTT Connection
    • Using IFTTT extension on Home-Assistant, a trigger is sent to the webhook which in turn activates an applet
    • The applet sends a push notification to mobile with the message “Intrusion detected”.
    Sonoff dual-channel device
    Both the lights are connected to a Sonoff dual-channel device running Tasmota firmware and this device is also configured as a switch in the hub.

    Automation 2 References

    Below are sample automation scripts are written for Automation 2.
    Sample configuration written in Home Assistant. There are similar configurations for other integrations like IFTTT, Pushover, TasmoAdmin, Tuya, Motion-Eye, and Custom Sensors.
    TasmoAdmin showing all Tasmota running devices.
    Home page of a home assistant after user login.
    Additional Implementations
    • All the devices can also be activated using Google Home or Amazon Echo using voice commands. 
    • As I have a public IP and my edge router is configured with all required Port forwarding/NAT configurations to access this Home Assistant web UI from outside my home. 
    • All my IoT edge devices are connected to a different subnet which provides some kind of security as well. 
    • Home Assistant web application is Progressive Web App (PWA) which means it can be accessed as a mobile app once we open it in a browser.

    Closing Thoughts

    In this blog, we have seen a couple of IoT automation flows that are actively used, observe how a device communicates with other devices and how it reacts without user intervention. 
    These smart solutions can be easily implemented for any household. I will be happy to share my knowledge and experience. and do share your thoughts/opinions/suggestions. I am available in messenger.
    Needless to say, I learned many concepts by doing this project, some of them are mentioned below: 
    • IFTTT.
    • Webhooks.
    • MQTT.
    • Node-Red.
    • Home Assistant.
    • Arduino programming.
    • Different sensors and their uses.
    • How to flash firmware into ESP8266 microcontroller boards.
    • OpenMQTTGateway.
    • Tasmota.
    • Working on Solenoid valve.
    • Progressive web Application
    • RTSP protocol. Used this protocol to configure my IP cam in Home Assistant.
    • Types of IoT communication protocols.
    The other plus side of this implementation is power and cost savings and it helped my parents to manage daily chores with less/no strain. I installed these applications in their mobiles and taught them how to manage the IoT devices.
    Also published on: https://medium.com/@rakesh362/home-automation-using-the-internet-of-things-iot-59f11e16c283

    Written by rakeshch | Software Developer and IoT Hobbyist
    Published by HackerNoon on 2020/12/24