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 the 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.
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 glows. When the relay is switched OFF, the circuit opens and the return path breaks and the light bulb switches off.
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 two 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 WiFi connection to the MQTT broker. MQTT broker is installed on a laptop. The default port which MQTT broker uses is 1883. For the setup to function, the MQTT publisher (Android Phone), MQTT broker (The laptop) 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. Also, 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 identifies 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 ESP8266 sketch is given below. A brief description of each method of the code is 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. 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 and initialize the timer to the number of milliseconds since the board started running the program. We connect the Wifi and wait until 500msec.
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!































