You are on page 1of 6

Control an LED over the Internet using the Arduino Ethernet Shield

16 Apr. 2013
How to control an LED over the Internet using an Arduino and an ethernet shield
SOURCE: http://www.instructables.co/i!/Control"an"#E$"o%er"the"internet"usin&"the"Ar!uino/
Original CODE: http://pastebin.com/C!"!#$"
'AR(S :
1) Ar!uino *+ a usin& the $ueilano%e,
1) Ar!uino Ethernet shiel! *with a -.100 chip,
1) noral #E$
%he circuit:
LED anode &'( to Arduino Ethernet shield pin !) LED cathode &*( to +,D
AA 1 16/0//13
%HE CODE &-ODI.ICA%IO,/ b0 AA1 2! A34IL #526 (
// .ILE,A-E 7 LED#.ino
// /O84CE7 http://www.instructables.com/id/Control9an9LED9over9the9internet9using9the9Arduino/
// Control an LED over the internet using the Arduino Ethernet /hield and %ransistor
// http://www.instructables.com/id/Control9an9LED9over9the9internet9using9the9Arduino/
// code /O84CE7 http://pastebin.com/C!"!#$"

:include ;/3I.h<
:include ;Ethernet.h<

:include ;/ervo.h<
/ervo m0servo) // create servo ob=ect to control a servo

b0te mac>? 7 @ 5ADE1 5AAD1 5ABE1 5AE.1 5A.E1 5AED C) //ph0sical mac address
b0te ip>? 7 @ 2D#1 2!E1 21 2FF C) // GiAed I3 addr in LA,
b0te gatewa0>? 7 @ 2D#1 2!E1 21 2 C) // internet access via router
b0te subnet>? 7 @ #HH1 #HH1 #HH1 5 C) //subnet masI
Ethernet/erver server&E5() //server port

/tring read/tring)
//////////////////////
void setup&(@
pin-ode&!1 O8%38%() //pin selected to control LED
//start Ethernet
Ethernet.begin&mac1 ip1 gatewa01 subnet()
server.begin&()
//the pin Gor the servo co
//enable serial data print
/erial.begin&D!55()
/erial.println&Jserver LED test 2.5 9999 .ILE,A-E 7 LED#.ino J() // so I can Ieep tracI
C

void loop&(@
// Create a client connection
EthernetClient client 7 server.available&()
iG &client( @
while &client.connected&(( @
iG &client.available&(( @
char c 7 client.read&()

//read char b0 char H%%3 re$uest
iG &read/tring.length&( ; 255( @

//store characters to string
read/tring '7 c)
///erial.print&c()
C

//iG H%%3 re$uest has ended
iG &c 77 KLnK( @

///////////////
/erial.println&read/tring() //print to serial monitor Gor debuging
client.println&JH%%3/2.2 #55 OMJ() //send new page
client.println&JContent9%0pe: teAt/htmlJ()
client.println&()
AA 2 16/0//13
client.println&J;H%-L<J()
client.println&J;HEAD<J()
client.println&J;%I%LE<Home Automation;/%I%LE<J()
client.println&J;/HEAD<J()
client.println&J;BODN bgcolor7Ka$uaK<J()
client.println&J;H2<Home Automation;/H2<J()
client.println&J;hr /<J()
client.println&J;br /<J()

client.println&J;a hreG7LJ/OlightonLJLJ<%urn On Light;/a<J()
client.println&J;a hreG7LJ/OlightoGGLJLJ<%urn OGG Light;/a<;br /<J()

client.println&J;/BODN<J()
client.println&J;/H%-L<J()

dela0&2()
//stopping client
client.stop&()

///////////////////// control arduino pin
iG&read/tring.indeAOG&JOlightonJ( <5(//checIs Gor on
@
digitalPrite&!1 HI+H() // set pin ! high
/erial.println&JLED OnJ()
C
else@
iG&read/tring.indeAOG&JOlightoGGJ( <5(//checIs Gor oGG
@
digitalPrite&!1 LOP() // set pin ! low
/erial.println&JLED OGGJ()
C
C
read/tring7JJ) //clearing string Gor neAt read

C
C
C
C
C
AA 3 16/0//13
,eAt1 go to the I3 Address: 2D#.2!E.2.2FF and turn on and oGG the LEDQ
Open http://102.161.1.122/ in browser:
/E4IAL -O,I%O4 /HOP/:
DEMO W/O ROUTER
2/ 8/E C4O//9ORE4 E%HE4,E% CABLE %O CO,,EC% 3C * A4D8I,O E%HE4,E% /HIELD
#/ /E% -A,8ALLN 3C I3 ADD4
In LinuA1 edit connections:
/elect wired connection S clicI on EDI4 >TUVWVXYZ[\Z? :
AA / 16/0//13
6/ -anuall0 insert an I3 address oG the same subnet 2D#.2!E.2/#] &ie1 2D#.2!E.2.AAA(1 such as
2D#.2!E.2.25) insert subnet masI #HH.#HH.#HH.5.
]/ %hen checI 0our I3 addr with iGconGig.
H/ %hen ping 2D#.2!E.2.2FF >A4D8I,O E%HE4,E% /HIELD?:
AA . 16/0//13
!/ Open http://2D#.2!E.2.2FF/ in browser and switch LED on/oGG.
Downloaded Grom http://t9h.wiIispaces.com/arduino
AA 6 16/0//13