Home Automation using IOT – Concept and Working Model

This note explains the concept of using IOT for home automation. The below diagram shows how home appliances are controlled using a web based control panel.

Schema diagram for IOT based home automation using nodeMCU-ESP8266

There are three lights(LEDs), one fan and a temperature sensor in the model house. Lights and fan are driven by relay modules.

Relays are driven by NodeMCU (ESP8266 based IOT module). The temperature sensor is connected to the nodeMCU directly.

We have an IOT server, running MQTT broker and also the web based control panel. Using the control panel buttons, the user can turn on/turn off lights and fan. He/she can also check the state of appliances (on/off). There is also a button to fetch the room temperature.

An MQTT broker (Mosquitto) facilitates communication between the webpage and the nodeMCU. MQTT broker facilitates publishing and subscription to topics. The webpage publishes to and subscribes from topics, based on user input. NodeMCU module receives messages published from the webpage and it drives the relay modules for various appliances.

NodeMCU connects to the home WiFi. An old WiFi router is used to create the home network.

For simplicity, the webpage and MQTT broker are hosted on the same server.  Ideally, the server components must be hosted in the internet, for it to be accessible from anywhere. But, for this demo project, I installed them on a raspberry pi and connected the pi to the home network itself.

User can access the control panel web page from his smart phone or computer, using its LAN IP address. By default, the WiFi router will assign the IP address dynamically. Some routers allow us to fix an IP address to a device from the router’s admin panel, so that the IP address does not change when you reboot the network.

NOTE: Because this project was done to demonstrate how it works, I have simplified and reduced some components.

For example, one nodeMCU is required for each appliance. Instead, here I used a single nodeMCU centrally to drive all appliances.

Similarly, it is not ideal to connect the web page directly to the MQTT broker. Instead it should have recorded user input in a database. Then another service must be used to message the IOT module. Also, the web page must be password protected at the very minimum for security and privacy reasons.

Watch this video for a live demo and explanation of the connections

Components Required

Hardware

  • Nodemcu v1.0 (ESP8266 based IOT module)
  • Breadboard power supply module supplying both 3.3 and 5V
  • 4 Channel Relay module
  • 3 LED’s
  • 1 DC Motor with fan
  • Raspberrypi or any computer
  • Home WiFi Router

Software

  • Arduino IDE
  • ESP8266 Core for Arduino IDE
  • Apache web server
  • PHP
  • PHP module for Mosquitto mqtt server

Software Installation

For this project, we are going to program the nodeMCU with the arduino IDE. Steps for installing the arduino IDE is given here :  

The ESP8266 Arduino core is needed for the Arduino IDE  for connecting and programming the NodeMCU. Steps for installing the ESP8266 Arduino core is given in this note

On the IOT server, we need Mosquitto MQTT broker installed.

For the control panel website, we need apache web server and PHP.

For the PHP to connect to MQTT broker, we need the PHP module for Mosquitto MQTT. (More on installation of PHP module for Mosquitto server is here

Logic

When the LIGHT1-ON button is pressed on the web page, the underlying PHP code publishes a “LIGHT1:ON” message to the topic “LIGHTS”, through the PHP MQTT library. Similarly, when the LIGHT1-OFF button is pressed, the web page sends “LIGHT1:OFF” message to the LIGHTS topic. Similarly, for fan, FAN:ON and FAN:OFF messages are published to LIGHTS topic itself.  

For receiving information like status of appliances and room temperature from the IOT module, the web page subscribes to LIGHTSTAT and TEMP topics. The nodeMCU continuously publish on/off state of all appliances to the LIGHTSTAT topic. Similarly, the temperature data read from an LM35 sensor is published to the TEMP topic. When a user presses STATUS button or CURRENT TEMPERATURE button on the control panel, the web page subscribes to these topics and retrieves messages.

On the NodeMCU side, we can configure it to connect to multiple WiFi networks – it will choose the best WiFi based on signal strength. The function connect_wifi() function does this. A new WiFi SSID can be added to the known access point list within prep_aplist() function.

The complete code for the NodeMCU as well as the web based control panel are available for download at the end of this note.

Wiring

Wiring diagram for IOT based home automation concept

You can connect the USB power supply for the nodemcu with the breadboard power supply. Here is a note about how to connect a relay module to NodeMCU

Code

Download code for nodeMCU (compressed .zip file )

Download code for web page (compressed .zip file)

In the nodeMCU code, following function may need change based on the available WiFi networks. You can add any number of WiFi networks.

void prep_aplist()
{
  strcpy(known_aplist[0].ssid, "Your First Network SSID");
  strcpy(known_aplist[0].password, "Your First Network Password");
  known_aplist[0].siglevel = -9999;

  strcpy(known_aplist[1].ssid, "Your Second Network SSID");
  strcpy(known_aplist[1].password, "Your Second Network Password");
  known_aplist[1].siglevel = -9999;

  strcpy(known_aplist[2].ssid, "Your Third Network SSID");
  strcpy(known_aplist[2].password, "Your Third Network Password");
  known_aplist[2].siglevel = -9999;

  known_ap_count = 3;
}

 

Connecting a Relay Module with NodeMCU

Written by Joseph Justin

I am a gadget freak, electronics enthusiast and passionate programmer. My current interests are electronics platforms like Arduino and various IOT boards. Attending school (St. Peter's Senior Secondary School, Kadayiruppu) by day and coding by night is what I do.