TESTING CONTROLED LED AS SOLENOID IN ARDUINO WEB SERVER
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 and off from a webpage. For this project
I used the Arduino Uno R3 and Arduino Ethernet Shield.
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,0, 2);
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
pinMode(8, OUTPUT);
Ethernet.begin(mac, ip);
server.begin();
}
|
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.
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;
}
|
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 {
|
![]() |
After create HTML form, the web server will look like this |
![]() |
Before click the on LED |
![]() |
After click the ON the LED in red led |
No comments:
Post a Comment