This post is a practical demo on controlling an LED from a browser.

Arduino ETHERNET SHIELD is used here. The Browser acts as a CLIENT & the SHIELD acts as a SERVER listening on HTTP PORT 80.

This demo is done on a Local Area Network .This means that the PC from where Browser is operated & the ETHERNET shield both are connected to the same NETWORK. You may have a Router in top of your Network.The Ethernet shield is connected to one of the RJ45 connector of the Router using a STRAIGHT Ethernet cable.The PC from where you start your browser is also connected to the same Router , to another RJ45 connecter or through WIFI , if your Router is WIFI enabled.

Once you connect the RJ45 cable to Ethernet shield, the left LED on RJ45 socket glows Green.This indicates a successful link made to Router.

 

IMG_20161030_153619

 

 

Image 5

Digital pin 2 is used to connect to an LED through a resistor.

Digital pin 13 is occupied by Ethernet shield , so the built in LED on pin13 cannot be used.

When the Ethernet shield is connected to the Router an IP address is allotted by DHCP of the Router.As it is Dynamic & keeps changing on every boot up , we assign the IP using the code :

byte ip[] = { 192, 168, 0, 150 };

Note that , this IP should be in the same range of your Network.

To know the range , Open Network sharing center

Click on your connection & then the DETAILS tab

Here you can see the Gateway IP & IPV4 of your PC.You should assign an IP in the same range

In my case the gateway IP is 192.168.0.1

So I’m assigning the IP of Ethernet shield by changing the last byte of the address , say, 192.168.0.150

Image 1

 

Here is the sketch used in this demo :

————————————————–

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 150 }; // IP address in LAN – need to change according to your Network address
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString;
int ledPin = 2;

void setup(){

    pinMode(ledPin, OUTPUT); //pin selected to control
    // Test LED
    digitalWrite(ledPin, HIGH); // set pin high
    delay(500);
    digitalWrite(ledPin, LOW); // set pin low
   
    //start Ethernet
    Ethernet.begin(mac, ip, gateway, subnet);
    server.begin();
}

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;
                }

                //if HTTP request has ended– 0x0D is Carriage Return \n ASCII
                if (c == 0x0D) {
                    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> ARDUINO ETHERNET SHIELD</TITLE>");
                    client.println("</HEAD>");
                    client.println("<BODY>");
                    client.println("<hr>");
                    client.println("<hr>");
                    client.println("<br>");
                    client.println("<H1 style=\"color:green;\">ARDUINO ETHERNET SHIELD — LED ON/OFF FROM WEBPAGE</H1>");
                    client.println("<hr>");
                    client.println("<br>");

                    client.println("<H2><a href=\"/?LEDON\"\">Turn On LED</a><br></H2>");
                    client.println("<H2><a href=\"/?LEDOFF\"\">Turn Off LED</a><br></H2>");

                    client.println("</BODY>");
                    client.println("</HTML>");

                    delay(10);
                    //stopping client
                    client.stop();

                    // control arduino pin
                    if(readString.indexOf("?LEDON") > -1) //checks for LEDON
                    {
                        digitalWrite(ledPin, HIGH); // set pin high
                    }
                    else{
                        if(readString.indexOf("?LEDOFF") > -1) //checks for LEDOFF
                        {
                            digitalWrite(ledPin, LOW); // set pin low
                        }
                    }
                    //clearing string for next read
                    readString="";

                }
            }
        }
    }
}

——————————————————————————

  The Arduino talks to Ethernet shield over SPI Serial Peripheral Interface. So to work with the shield you need to include both the Ethernet & SPI libraries in the top of the sketch.

#include <SPI.h>

#include <Ethernet.h>

 

The next piece of information is the MAC address of the shield , usually expressed as 6 bytes.

If your Shield has the address printed , use that MAC address.Otherwise you can settle for a random 6 bytes , as it is unlikely that another device of same address exists in the same network.

The MAC address is typically included in the Arduino sketch as global array of bytes.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

The server is assigned on  HTTP port 80

EthernetServer server(80);

PIN 2 is assigned to LED & a string variable readString is assigned to store the request.

Under void SETUP

The method begin() in the Ethernet class attempts to connect to the Network using the details passed into its arguments.

Here we pass an IP address & the Ethernet shield makes a Network connection using a STATIC IP address.

Ethernet.begin(mac, ip, gateway, subnet);

server.begin()

starts listening on the HTTP port 80

To check the IP connection press WIN Logo key +R & then type in CMD

Ping the IP address of the Shield.

If you get back result , then you’re ready to connect the client.

Image 6

On computer Networks the machines or devices play 2 different roles : CLIENT & SERVER.

For e.g a web browser is a CLIENT that connects to other machines to request web pages or files.

The device or machine that serves the information is the SERVER.

SERVERs wait until a CLIENT connects & starts conversation with them.

PORTS allow different types of messages to be received by different pieces of software running on same server.For e.g Server software that accepts connection over FTP will usually run on port 21.WEB server software usually accepts connection over HTTP on port 80. In this demo we use he WEB SERVER.

Now open up the browser on a PC which is in the same network as Shield.

Type in the address 192.168.0.150

The following CLIENT REQUEST & SERVER RESPONSE happens in the background.

Client Request :

When you surf to the IP address of the Arduino server, the web browser (client) will send a request, such as the one shown below, to the server.

GET / HTTP/1.1\r\n

Host: 10.0.0.20\r\n

The information in the request will differ, depending on the browser and operating system that the request is sent from.

The \r\n characters that you see at the end of every line of text in the request are non-visible characters (non-printable characters). \r is the carriage return character and \n is the linefeed character (or newline character).

The last line of the request is simply \r\n without and preceding text. This is the blank line that the Arduino sketch checks for before sending a response to the client web browser.

In other words, the sketch reads every character from the above request and knows when the end of the request has been reached because it finds the blank line.

Server Response :

After receiving the request for a web page from the client, the server first sends a standard HTTP response and then the web page itself.

The response sent from the Arduino is as follows:

HTTP/1.1 200 OK\r\n

Content-Type: text/html\r\n

Connection: close\r\n

\r\n

Again the non-visible characters \r\n are shown in the above response.

The println() function in the the sketch automatically adds the \r\n characters to the end of each line. The empty println() function at the end of the HTTP response simply sends the \r\n with no text in front of it.

The above request and response are part of HTTP

Web Page

After the server has sent the HTTP response, it sends the actual web page which is then displayed in the browser.

The web page consists of text with HTML tags. You do not see the tags in the browser as these tags are interpreted by the browser.

The actual HTML markup tags are shown below.

————————————————-

<html><head>

<title> ARDUINO ETHERNET SHIELD</title>

</head>

<body>

<hr>

<br>

<h1 style="color:green;">ARDUINO ETHERNET SHIELD — LED ON/OFF FROM WEBPAGE</h1>

<hr>

<br>

<h2><a href="/?LEDON" "="">Turn On LED</a><br></h2>

<h2><a href="/?LEDOFF" "="">Turn Off LED</a><br></h2>

</body></html>

—————————————————–

In the Arduino sketch you can see these HTML tags sent using

client.println command

Now the browser displays the WEB PAGE sent as response from the Server.

Click on

Turn On LED   -  to see the LED going ON

Turn Off LED   – to make the LED OFF

indexOf String function is used to search for the string LEDON or LEDOFF.

The indexOf function returns a –1 if it doesn’t find the wanted string.This function is used to make the LED ON or OFF accordingly.

0Image 2

 

Image 3

 

Image 4

 

Instead of LED you can connect a Relay board & then control any home device through the contacts of the Relay.The above setup is the basic to start with Home Automation.

This demo is done on Local Area Network .It can be extended to Internet of Things through Port Forwarding & DDNS set up which is explained in another post.

Advertisements