Friday, 9 October 2026

Smart Home - Operate Light Bulbs Using Android Phone

Smart Home:

The idea for this project came from the source KROW - One Platform for Workforce Management. On this site, the projects are published to bid and then work with the concerned group. However, instead of bidding to work on this project, we decided to work on this project independently at home, as a home improvement project. So, this project involves no client OR in other words, people at our home are the clients. This project was about operating the light bulbs remotely, using WiFi over Android phone. If you see the video below, we have placed the mobile in close proximity of the switch board. This was solely for the purpose of capturing video. Ideally, we can place the mobile in a remote place. Either in a different home, or we can operate from somewhere in the garden, or we can operate even when we go for a walk outside.     





Smart Home - Circuit Diagram: Please check the circuit diagram below which shows the electrical as well as electronics connections required for the Smart Home project. These electrical and electronics components are placed inside the switch board. Since the switchboard is neatly arranged and closed in the above video, components appear "hidden" from the outside view. The components of the circuit diagram are also listed below. 

  • Power Supply 
  • ESP8266
  • Four Channel Relays 
  • Switch Board with Three Sockets and Single Phase Supply and a Switch



The description of each of the component is given below:

Power Supply: The power supply module takes in a 6V DC supply (four 1.5V batteries) as an input and gives a 3.3V and 5V DC supply as an output. A picture of our power supply module is given below. Please remember to switch the jumpers appropriately to get a constant 3.3V and 5V supply. 



ESP8266: We used ESP8266 as a controller to operate the relays which in turn operate the light bulbs. We chose ESP8266 over Arduino, because ESP8266 has a built-in WiFi capability which Arduino did not have when this project was built (around 2019 timeframe). We need to supply 3V input voltage for ESP8266 to function. This supply comes from the power supply module above, which supplies 3.3V. (No resistor arrangement is necessary. A direct connection of 3.3 V from power supply to ESP8266 "Vin" pin is sufficient for ESP8266 to operate.) Also a GROUND of power supply goes to the GROUND of ESP8266. A picture of ESP8266 which we used is shown below. 


Relays: We used Relays as Switches which can be dynamically turned ON or OFF by the ESP8266 controller. We purchased a 4-channel isolated relay module opto-coupler, as shown in the below picture. The Vcc pin of relays is connected to the 3V output of the ESP8266 controller. The jumper placed on the JD-Vcc and Vcc is removed. And the JD-Vcc pin is connected to the 5V power supply above. The GND pin of this module is connected to the GND pin of the power supply. The digital pins D0, D1, D2 and D3 of ESP8266 are connected to the IN1, IN2, IN3 and IN4 of the Relays module. 


The Electrical Connections: The setup shown in the Circuit Diagram Section, works on a single phase supply. The single phase supply has Red, Green and Black incoming wires. The Red wire representing the Phase, Green representing Ground and Black representing Neutral. In the three holes of the socket, the top hole represents Ground, the right hole represents Phase and the left hole represents Neutral. The circuit diagram shown above, is the inside picture of the Switch Board. So, you can imagine the Left Hole of the socket will fall on right hand side and Right Hole of the socket will fall on left hand side. The view turns reverse. So we made connections from Phase-Phase, Ground-Ground and Neutral-Neutral accordingly. When the relay is switched ON, the circuit closes and the current finds a return path and the light bulb turns ON. When the relay is switched OFF, the circuit opens and the return path breaks and the light bulb switches OFF.    

Android App: Below are the snapshots of our program in MIT APP INVENTOR. As you can see the Designer screen has a mobile snapshot with four buttons. They correspond to four controls of the four light bulbs (As I already mentioned, only IN2 and IN4 are operational yet). The corresponding Blocks screen is also created to operate four light bulbs. The blocks screen initializes the UrsAI2MQTT1 broker with the Broker IP, Port and Client ID, required for a connection to the MQTT broker. MQTT broker is installed on a computer. The default port which MQTT broker uses is 1883. For the setup to function, the MQTT publisher (Android Phone), MQTT broker (The computer) and the MQTT subscriber (The ESP8266), should be on the same WiFi network. Also, there are a couple of configurations to be done on the windows machine, for the Firewall to allow these connections. And there are few configurations to be done in the broker configuration files. I have not listed those configurations here, because they change, based on the operating system type and the broker type. However they can be easily found on the internet. Each of the four buttons on the android phone toggles the state of the bulb. They switch the bulb OFF, if it is previously ON. And switches ON, if it is previously OFF. They achieve this by connecting to the MQTT broker and publishing string messages led_on and led_off to the broker on the topic NodeMCU/inTopic. The subscriber client ESP8266, subscribes to the same topic, and receives the messages. The ESP8266 processes the messages to check whether they belong to port 0, 4, 5 AND 16 (RELAY-1, RELAY-2, RELAY-3 AND RELAY-4 respectively to toggle the four light bulbs).

 







The below diagram shows the flow of control to operate the light bulbs. The connection initiates, when the user presses a button on the Android phone. There are four buttons on the mobile screen. Out of which button 2 and button 4 are functional. We decided to use MQTT protocol for connection, because MQTT is widely used for IoT operations as embedded protocol. When button 2 is pressed a string message is published on a MQTT Broker topic. The ESP8266 subscribes to this topic and receives the messages led2_on, led2_off, led4_on and led4_off based on the button pressed and it's state. ESP8266 decodes the messages and sends a signal HIGH or LOW to a port D1, D3. A relay is connected to these ports, which gets switched on and off with these signals. This switches the bulbs on and off. In the present scenario, the MQTT Broker is installed on a computer at home. The Android phone, computer and ESP8266 are all on the same WiFi network. So this Smart Home concept can only be applied at home. However, if we install MQTT Broker on cloud, this functionality can be accessed from anywhere outside home. For Example, if we forget to switch off the lights and went off for a walk, we can turn off the lights using mobile when on road. However, when the request is sent over cloud, care should be taken to implement security protocol rightly. Otherwise, there are chances that the request will be hacked.  





The ESP8266 sketch is given below. A brief description of each method of the code is also given below. In the beginning of the code, we initialize the location of MQTT_SERVER, the WiFi Login ID and Password. The MQTT_SERVER can be either on a home private network or on cloud. In any case, we need to specify its Domain Name or IP Address and port in the sketch. Additionally, if the MQTT_SERVER is on cloud, we have to configure additional security parameters, which are not configured here yet. Next, we declare the GPIO Pin Numbers over ESP8266 microcontroller. We have named them r1, r2, r3 and r4, because we have connected relays to them. However on the microcontroller, these pins are marked as D0, D1, D2 and D3 respectively.  Next we declare the eight functions ledXTurnOn() and ledXTurnOff().

callback() method: Since our ESP8266 is going to subscribe to MQTT messages, it needs to implement a callback function. This method takes in the incoming topic name, the payload and the length of the payload as its parameters. This method processes the payload and checks if it is any of the strings led1_on, led1_off, led2_on, led2_off, led3_on, led3_off, led4_on, led4_off. Each of these strings invoke the relevant methods. This method processes exactly one message at a time.  

led1TurnOn(),

led1TurnOff(),

led2TurnOn(),

led2TurnOff(),

led3TurnOn(),

led3TurnOff(),

led4TurnOn(),

led4TurnOff()    

In each of the above function definition, the function pulls one of the pins D0, D1, D2 and D3 to LOW or HIGH. When pulled LOW, the light bulb switches ON and when pulled HIGH, the light blub switches OFF. 

WifiClient Object: This is a transport layer object which must be declared to communicate with MQTT Server.   

PubSubClient Object: We need to declare a PubSubClient object in order to establish a connection to MQTT Server. While initializing the object, it takes in four parameters, MQTT_SERVER Address, MQTT SERVER Port, callback function and WifiClient Object. The PubSubClient object, operates on top of the WifiClient which is a transport layer object. 

setup() function: In this function, we first disconnect the Wifi and bring up the board in ready state. Our ESP8266 board is an active-low board. So we set all the GPIO pins (0, 4, 5, 16) to OUTPUT-HIGH, to keep the bulbs in an OFF state when powered on. We also set the built-in LED in an OUTPUT-HIGH state. We set the baud rate to 9600. We connect the Wifi and wait until 500msec, till the Wifi connection gets established. 

loop() function: This function checks if Wifi is connected. If Wifi is not connected, this function connects to Wifi. Once Wifi is connected, the loop() function tries to connect to the MQTT server. If the MQTT server is connected, the loop() function tries to subscribe to the topic NodeMCU/inTopic every two seconds just in case the connection is dropped. Once subscribed, the loop() function calls client.loop() function to keep checking if there are any messages delivered to the topic. If the messages are delivered, the client.loop() function, triggers a call to the callback() function to process this message.  

reconnect(): This function connects to the MQTT Broker, given the IP Address, Port of the Broker, client ID, MQTT Broker Login ID and MQTT Broker Password. The client ID uniquely identifies the client for the server. 

wifi_connect(): This function connects to Wifi given ssid and password for Wifi. If ESP is not able to connect to the wifi even after 150th attempt, the ESP is restarted and an attempt is made to connect to the Wifi again. Till the time Wifi gets connected, the ESP remains in this loop itself! 

 



  


No comments:

Post a Comment