This a simple tutorial on how to setup your Arduino board with Ethernet shield to control your home appliances like Lights or Fans using any device (Mobile/Tablet/Laptop/Desktop) connected to your WiFi network. It is assumed that the reader has gone through the project how to get started with the arduino.
Components Required
Block Diagram
Fig. 1: Block Diagram of Arduino based Wi-Fi Controlled Home Automations System
HTML (Hyper Text Markup Language)
Getting Started
– Go to File menu and click Save As. Below the file name you would see save as type:
Fig. 2: Screenshot of saving webpage on Windows used for controlling home appliances
– Now close the notepad and open the file again using a browser (Mozilla Firefox/ Google Chrome/ IE). You should see something like this.
Fig. 3: Screenshot of a simple HTML webpage
What we wrote above is the basic code.
HTML Tags Explanation
This is a bold text
Some Frequently Used Tags
Tags |
Function |
<title></title> |
Title of the page which would be shown as the name of the browser window and bookmarks |
<body></body> |
Whatever you want to display on your webpage would be included in this tag |
<b></b> |
Bold |
<u></u> |
Underline |
<i></i> |
Italics |
<strike></strike> |
Strike through the text |
<center></center> |
For displaying text on center of the page |
Empty Tags
<br/> and <hr/> are called empty tags since they don’t have corresponding closing tag. <br/> is used for giving line break (equivalent to hitting enter in a word document) and <hr/> is horizontal rule (shows a visible line when used).
Linking to Other pages
Will give you this:
underlined
Well nothing is good without a good example so let us implement everything we just learned into an example code. Copy paste the following into a notepad, save it as a HTML file and see for yourself in the browser.
This is how the page would look in the browser:
Fig. 4: Screenshot of a simple HTML webpage showing simple text
Now that we have learnt the web interface part, let us move on to the next section.
Hardware Setup -Step by Step
Hardware Setup
1) Gather all the parts shown in the image.
Fig. 5: Image showing components used in design of Wi-Fi controlled Home Automation System
Note 1: The one I used is an Indian made Arduino clone. Depending on from which manufacturer you bought, it would look different.
Fig. 6: Image of Ethernet Shield for Arduino Uno
Fig. 7: Image showing Ethernet Cable connected to PC
Fig. 8: Image showing Ethernet Cable connected to Wi-Fi Modem
(Skip this step if the Router has already been configured)
5) Now take another Ethernet cable and connect one end to another LAN port of the Wi-Fi router and other end of the same cable to the Ethernet port of the Ethernet shield.
Fig. 9: Image showing LAN cable connected to Ethernet Shield
Fig. 10: Image showing LAN Cable connected to Wi-Fi Modem
Fig. 11: Screenshot of Arduino Serial Port showing Local IP Address
7) Switch ON WiFi on your Phone/Tablet/Laptop and locate the name of the Router (which you assigned earlier) and enter the password if necessary to connect to the network.
Note: My WiFi SSID is “Robots Reloaded”
Fig. 12: Screenshot of Android’s Settings for connecting to Wi-Fi Router
8) Open your browser and go to the address “http://192.168.0.108” (which was assigned in the sketch uploaded into it)
You should see this page in few seconds
Fig. 13: Screenshot of webpage loaded from Local Server
When opened from desktop/Laptop:
Fig. 14: Screenshot of Webpage on a desktop computer
9) Connect the rest of the circuit as per the circuit diagram.
Arduino Code Explanation & Troubleshooting
Arduino Code Explanation
Troubleshooting
– Problem: Nothing is shown on the serial monitor after implementing step 6
Possible cause:
-Problem: Web page not loading after implementing step 8
Possible cause:
a. You connected to the wrong WiFi network
b. The IP Address you dialed doesn’t match the one in the serial monitor
Make sure you are connected to the same WiFi network with correct password and that you have entered the correct IP Address.
– Problem: Web page not loading after implementing step 8
While you click the links, check the serial monitor output. If you see something like this:
Fig. 15: Screenshot of Arduino Serial Port sending commands to control relays
Improvements
Although only single appliance control is shown here, you can create code based on this model to control any number of appliances through WiFi.
Project Source Code
###
#include <SPI.h> #include <Ethernet.h> int relay = 3; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address byte ip[] = { 192, 168, 0, 108 }; // ip in lan (that's what you need to use in your browser. ("192.168.0.108") byte gateway[] = { 192, 168, 1, 1 }; // internet access via router byte subnet[] = { 255, 255, 255, 0 }; //subnet mask EthernetServer server(80); //server port String readString; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } pinMode(relay, OUTPUT); // start the Ethernet connection and the server: Ethernet.begin(mac, ip, gateway, subnet); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // Create a client connection EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read(); //read char by char HTTP request if (readString.length() < 100) { //store characters to string readString += c; //Serial.print(c); } //if HTTP request has ended if (c == 'n') { Serial.println(readString); //print to serial monitor for debuging client.println("HTTP/1.1 200 OK"); //send new page client.println("Content-Type: text/html"); client.println(); client.println("<HTML>"); client.println("<HEAD>"); client.println("<TITLE>WEB INTERFACE</TITLE>"); client.println("</HEAD>"); client.println("<BODY>"); client.println("<center>"); client.println("WELCOME TO THE WEB INTERFACE OF APPLIANCE CONTROLLER OVER WIFI"); client.println("<hr />"); client.println("<br />"); client.println("<H2>Arduino with Ethernet Shield</H2>"); client.println("<br />"); client.println("<a href="/?relay1on">Switch ON Relay 1</a>"); client.println("<a href="/?relay1off">Switch OFF Relay 1</a><br />"); client.println("<br />"); client.println("<br />"); client.println("<p>Created by GANESH SELVARAJ for EngineersGarage.com</p>"); client.println("<br />"); client.println("</center>"); client.println("</BODY>"); client.println("</HTML>"); delay(1); //stopping client client.stop(); //controls the Arduino if you press the buttons if (readString.indexOf("?relay1on") >0){ digitalWrite(relay, HIGH); } if (readString.indexOf("?relay1off") >0){ digitalWrite(relay, LOW); } //clearing string for next read readString=""; } } } } }###
Circuit Diagrams
Project Video
Filed Under: Electronic Projects
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.