Arduino WebServer Controlled LED

I wanted to investigate controlling the digital outputs on a Arduino from a webpage so I decided to build a simple setup to Turn a LED on an
ORIGINAL POST
By jamesrww
components
Hardware Components
X 1
Software Apps and online services
details

arudino web server.jpg

Configure Web Server

First off we need to configure the web server  this is done by calling the Ethernet libraries, setting the Mac Address, IP Address and Server Port. then in the Void Setup you start the server and define the pin you want to plug the LED into.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);

EthernetServer server(80);

void setup()
{
    Serial.begin(9600);
    pinMode(8, OUTPUT);
    Ethernet.begin(mac, ip);
    server.begin();
}

Create HTML Form

In the Void Loop we define client then check that the web server is connected and available the get it to display some HTML. The first lot checks the status of pin 8 and prints HTML to tell us if the LED is currently turned on or off. Then we use a HTML form to make some radio buttons and a submit button to select the status on or off.

main {
if (digitalRead(8)){
    client.print(” LED is <font color=’green’>ON</font>”);
}else{
    client.print(” LED is <font color=’red’>OFF</font>”);
}
client.println(“<br />”);

client.print(“<FORM action=\”http://192.168.1.177/\” >”);
client.print(“<P> <INPUT type=\”radio\” name=\”status\” value=\”1\”>ON”);
client.print(“<P> <INPUT type=\”radio\” name=\”status\” value=\”0\”>OFF”);
client.print(“<P> <INPUT type=\”submit\” value=\”Submit\”> </FORM>”);

break;
}

Read LED Status and Turn on or off

Now all that is left is to read the input from the HTML Form and turn the led on or off. When you select one of the radio buttons and click submit the form adds a status=1 or status=0 to the end of the URL. We can now use the GET function to read the value and run through am IF statement to set the digital write on pin 8 to either High or Low (On or Off).

if (c == ‘\n’) {
    currentLineIsBlank = true;
    buffer="";
} else if (c == ‘\r’) {
    if(buffer.indexOf(“GET /?status=1″)>=0)
    digitalWrite(8,HIGH);

    if(buffer.indexOf(“GET /?status=0″)>=0)
    digitalWrite(8,LOW);
}else {

arudino web server.jpg

Configure Web Server

First off we need to configure the web server  this is done by calling the Ethernet libraries, setting the Mac Address, IP Address and Server Port. then in the Void Setup you start the server and define the pin you want to plug the LED into.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);

EthernetServer server(80);

void setup()
{
    Serial.begin(9600);
    pinMode(8, OUTPUT);
    Ethernet.begin(mac, ip);
    server.begin();
}

Create HTML Form

In the Void Loop we define client then check that the web server is connected and available the get it to display some HTML. The first lot checks the status of pin 8 and prints HTML to tell us if the LED is currently turned on or off. Then we use a HTML form to make some radio buttons and a submit button to select the status on or off.

main {
if (digitalRead(8)){
    client.print(” LED is <font color=’green’>ON</font>”);
}else{
    client.print(” LED is <font color=’red’>OFF</font>”);
}
client.println(“<br />”);

client.print(“<FORM action=\”http://192.168.1.177/\” >”);
client.print(“<P> <INPUT type=\”radio\” name=\”status\” value=\”1\”>ON”);
client.print(“<P> <INPUT type=\”radio\” name=\”status\” value=\”0\”>OFF”);
client.print(“<P> <INPUT type=\”submit\” value=\”Submit\”> </FORM>”);

break;
}

Read LED Status and Turn on or off

Now all that is left is to read the input from the HTML Form and turn the led on or off. When you select one of the radio buttons and click submit the form adds a status=1 or status=0 to the end of the URL. We can now use the GET function to read the value and run through am IF statement to set the digital write on pin 8 to either High or Low (On or Off).

if (c == ‘\n’) {
    currentLineIsBlank = true;
    buffer="";
} else if (c == ‘\r’) {
    if(buffer.indexOf(“GET /?status=1″)>=0)
    digitalWrite(8,HIGH);

    if(buffer.indexOf(“GET /?status=0″)>=0)
    digitalWrite(8,LOW);
}else {
documents
Code
Schematics
Others

COMMENTS

Please Login to comment
  Subscribe  
Notify of