Anda di halaman 1dari 9

Home

Forum
Wiki
Code
RSS
Twitter
Facebook
Search Wiki Search Forum Search Blog
Login / Register
About
Pin Control Over the Internet Arduino + Ethernet
Thursday, June 9
th
, 2011
In a previous article we showed you how to control digital pins over over serial, and showed how such a simple thing can be so powerful. One
major downfall with that is you need to be nearby to send commands So today we are going to look at doing the same thing, but this time we will
be doing it over the internet using the Arduino Ethernet Shield.
As per our usual style, I am going to make this as simple as possible so it is easier to extend. So if you you are looking for something with an
HTML interface, this article will not be covering that.
The Arduino Ethernet Shield is capable of being both a client (like a web browser), and a server, and with the onboard SD card-slot can be quite
powerful by hosting up full websites, but for this article we are just looking at using the Arduino as a server and you will control it simply by going
to a specific URL.
Note that the Ethernet Shield uses digital pins, 10, 11, 12, and 13 for itself, so it is best to leave these alone and not try to use them for anything
else.
Getting It On The Net
Getting the ethernet shield on the internet is going to differ depending on your network, but no matter what, you need to plug it into an ethernet port
connected to the internet So make sure you do that.
Without an extra library, the ethernet code does not support DHCP and therefore requires that we hardcode the IP Address,gateway address, and
subnet mask for your network. This isnt really that hard, it is more of a pain when especially if you want to plug it into a different network as the
same settings may not work.
As of Arduino 1.0, the library does support DHCP So you should be able to just plug it into your network and have it work. It will report the
ipAddress to the serial monitor
We also left the code in there to do a manual setup if you prefer doing it that way. (I know I do) If you are familiar with your network and how to
do this, awesome, just make sure you change the setting at the top of the code to fit your network, and un comment out the parts related to it. If you
are not familiar with it, we can help you in the forum, and have posted some general help in there as well.
search blog...
bildr Pin Control Over the Internet Arduino +Ethernet http://bildr.org/2011/06/arduino-ethernet-pin-control/
1 of 6 9/20/2014 10:07 PM
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
Hooking It Up
Aside from plugging the Ethernet Shield into you Arduino, there isnt really anything you HAVE to have. I have LEDs on pins 2-9 on this example
so we can see it being controlled. If you do not intend to blink LEDs or test the code with LEDs, you can by all means leave these off.
Code
Assuming the the ip address of the arduino is 192.168.1.167 (check the serial monitor to see what it is), this code will allow you to send a sequence
to the arduino via your web browser like so: http://192.168.1.167/?23456789
The way the code is currently setup is that the pins will go high one at a time, in sequence, for 25ms then move on to the next in line. So this
example would blink pin 2,3,4,5,6,7,8 then 9.
As it stands, the user will not see the webpage return until the arduino is finished processing the request. You could change the code to return right
away and then process the request afterwards if needed, but I liked seeing the return after it was completed like a receipt, instead of an order.
NOW 1.0 COMPATIBLE
Copy Code
/ / ARDUI NO 1. 0+ ONLY
/ / ARDUI NO 1. 0+ ONLY
#i ncl ude <Et her net . h>
#i ncl ude <SPI . h>
bool ean r eadi ng = f al se;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / CONFI GURE
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / byt e i p[ ] = { 192, 168, 0, 199 }; / / Manual set up onl y
/ / byt e gat eway[ ] = { 192, 168, 0, 1 }; / / Manual set up onl y
/ / byt e subnet [ ] = { 255, 255, 255, 0 }; / / Manual set up onl y
bildr Pin Control Over the Internet Arduino +Ethernet http://bildr.org/2011/06/arduino-ethernet-pin-control/
2 of 6 9/20/2014 10:07 PM
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
/ / i f need t o change t he MAC addr ess ( Ver y Rar e)
byt e mac[ ] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Et her net Ser ver ser ver = Et her net Ser ver ( 80) ; / / por t 80
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
voi d set up( ) {
Ser i al . begi n( 9600) ;
/ / Pi ns 10, 11, 12 & 13 ar e used by t he et her net shi el d
pi nMode( 2, OUTPUT) ;
pi nMode( 3, OUTPUT) ;
pi nMode( 4, OUTPUT) ;
pi nMode( 5, OUTPUT) ;
pi nMode( 6, OUTPUT) ;
pi nMode( 7, OUTPUT) ;
pi nMode( 8, OUTPUT) ;
pi nMode( 9, OUTPUT) ;
Et her net . begi n( mac) ;
/ / Et her net . begi n( mac, i p, gat eway, subnet ) ; / / f or manual set up
ser ver . begi n( ) ;
Ser i al . pr i nt l n( Et her net . l ocal I P( ) ) ;
}
voi d l oop( ) {
/ / l i st en f or i ncomi ng cl i ent s, and pr ocess qequest .
checkFor Cl i ent ( ) ;
}
voi d checkFor Cl i ent ( ) {
Et her net Cl i ent cl i ent = ser ver . avai l abl e( ) ;
i f ( cl i ent ) {
/ / an ht t p r equest ends wi t h a bl ank l i ne
bool ean cur r ent Li neI sBl ank = t r ue;
bool ean sent Header = f al se;
whi l e ( cl i ent . connect ed( ) ) {
i f ( cl i ent . avai l abl e( ) ) {
i f ( ! sent Header ) {
/ / send a st andar d ht t p r esponse header
cl i ent . pr i nt l n( " HTTP/ 1. 1 200 OK" ) ;
cl i ent . pr i nt l n( " Cont ent - Type: t ext / ht ml " ) ;
cl i ent . pr i nt l n( ) ;
sent Header = t r ue;
}
char c = cl i ent . r ead( ) ;
i f ( r eadi ng && c == ' ' ) r eadi ng = f al se;
i f ( c == ' ?' ) r eadi ng = t r ue; / / f ound t he ?, begi n r eadi ng t he i nf o
i f ( r eadi ng) {
Ser i al . pr i nt ( c) ;
swi t ch ( c) {
case ' 2' :
/ / add code her e t o t r i gger on 2
t r i gger Pi n( 2, cl i ent ) ;
br eak;
case ' 3' :
/ / add code her e t o t r i gger on 3
t r i gger Pi n( 3, cl i ent ) ;
br eak;
case ' 4' :
/ / add code her e t o t r i gger on 4
t r i gger Pi n( 4, cl i ent ) ;
br eak;
case ' 5' :
/ / add code her e t o t r i gger on 5
t r i gger Pi n( 5, cl i ent ) ;
br eak;
case ' 6' :
bildr Pin Control Over the Internet Arduino +Ethernet http://bildr.org/2011/06/arduino-ethernet-pin-control/
3 of 6 9/20/2014 10:07 PM
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
/ / add code her e t o t r i gger on 6
t r i gger Pi n( 6, cl i ent ) ;
br eak;
case ' 7' :
/ / add code her e t o t r i gger on 7
t r i gger Pi n( 7, cl i ent ) ;
br eak;
case ' 8' :
/ / add code her e t o t r i gger on 8
t r i gger Pi n( 8, cl i ent ) ;
br eak;
case ' 9' :
/ / add code her e t o t r i gger on 9
t r i gger Pi n( 9, cl i ent ) ;
br eak;
}
}
i f ( c == ' \ n' && cur r ent Li neI sBl ank) br eak;
i f ( c == ' \ n' ) {
cur r ent Li neI sBl ank = t r ue;
}el se i f ( c ! = ' \ r ' ) {
cur r ent Li neI sBl ank = f al se;
}
}
}
del ay( 1) ; / / gi ve t he web br owser t i me t o r ecei ve t he dat a
cl i ent . st op( ) ; / / cl ose t he connect i on:
}
}
voi d t r i gger Pi n( i nt pi n, Et her net Cl i ent cl i ent ) {
/ / bl i nk a pi n - Cl i ent needed j ust f or HTML out put pur poses.
cl i ent . pr i nt ( " Tur ni ng on pi n " ) ;
cl i ent . pr i nt l n( pi n) ;
cl i ent . pr i nt ( " <br >" ) ;
di gi t al Wr i t e( pi n, HI GH) ;
del ay( 25) ;
di gi t al Wr i t e( pi n, LOW) ;
del ay( 25) ;
}
Unless otherwise stated, this code is released under the MIT License Please use, change and share it.
Video
bildr Pin Control Over the Internet Arduino +Ethernet http://bildr.org/2011/06/arduino-ethernet-pin-control/
4 of 6 9/20/2014 10:07 PM
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
Extending This
By adding to, or changing, the switch statement in the code, you could easily add support for letters as well allowing you to call even more
functions over the web. You can also change the code that is run if needed. So instead of blinking pins, maybe you control motor directions or
timing. This could be easily be used for automation or art installations.
If I can take the non ethernet version of this article and make web controllable bells, just imagine what you can do with this.
Tweet 7

90 Like Like
66 Comments
Archives
October 2013
November 2012
August 2012
April 2012
March 2012
February 2012
J anuary 2012
October 2011
September 2011
August 2011
J uly 2011
J une 2011
May 2011
April 2011
March 2011
February 2011
J anuary 2011
December 2010
November 2010
October 2010
September 2010
Categories
bildr Pin Control Over the Internet Arduino +Ethernet http://bildr.org/2011/06/arduino-ethernet-pin-control/
5 of 6 9/20/2014 10:07 PM
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
funded projects
Promotions
Review
Site
Tutorials
User Contributed Articles
User Project
about
Content available under:
Attribution-Share Alike 3.0 Unported
Proudly powered by WordPress, Free WordPress Themes, and Search Marketing
bildr Pin Control Over the Internet Arduino +Ethernet http://bildr.org/2011/06/arduino-ethernet-pin-control/
6 of 6 9/20/2014 10:07 PM
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
New Text Document ( 2) . t xt
/ / ARDUI NO 1. 0+ ONLY
/ / ARDUI NO 1. 0+ ONLY
#i ncl ude <Et her net . h>
#i ncl ude <SPI . h>
bool ean r eadi ng = f al se;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / CONFI GURE
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / byt e i p[ ] = { 192, 168, 0, 199 }; / / Manual set up onl y
/ / byt e gat eway[ ] = { 192, 168, 0, 1 }; / / Manual set up onl y
/ / byt e subnet [ ] = { 255, 255, 255, 0 }; / / Manual set up onl y
/ / i f need t o change t he MAC addr ess ( Ver y Rar e)
byt e mac[ ] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Et her net Ser ver ser ver = Et her net Ser ver ( 80) ; / / por t 80
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
voi d set up( ) {
Ser i al . begi n( 9600) ;
/ / Pi ns 10, 11, 12 & 13 ar e used by t he et her net shi el d
pi nMode( 2, OUTPUT) ;
pi nMode( 3, OUTPUT) ;
pi nMode( 4, OUTPUT) ;
pi nMode( 5, OUTPUT) ;
pi nMode( 6, OUTPUT) ;
pi nMode( 7, OUTPUT) ;
pi nMode( 8, OUTPUT) ;
pi nMode( 9, OUTPUT) ;
Et her net . begi n( mac) ;
/ / Et her net . begi n( mac, i p, gat eway, subnet ) ; / / f or manual set up
ser ver . begi n( ) ;
Ser i al . pr i nt l n( Et her net . l ocal I P( ) ) ;
}
voi d l oop( ) {
/ / l i st en f or i ncomi ng cl i ent s, and pr ocess qequest .
checkFor Cl i ent ( ) ;
}
Page 1
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
New Text Document ( 2) . t xt
voi d checkFor Cl i ent ( ) {
Et her net Cl i ent cl i ent = ser ver . avai l abl e( ) ;
i f ( cl i ent ) {
/ / an ht t p r equest ends wi t h a bl ank l i ne
bool ean cur r ent Li neI sBl ank = t r ue;
bool ean sent Header = f al se;
whi l e ( cl i ent . connect ed( ) ) {
i f ( cl i ent . avai l abl e( ) ) {
i f ( ! sent Header ) {
/ / send a st andar d ht t p r esponse header
cl i ent . pr i nt l n( "HTTP/ 1. 1 200 OK") ;
cl i ent . pr i nt l n( "Cont ent - Type: t ext / ht ml ") ;
cl i ent . pr i nt l n( ) ;
sent Header = t r ue;
}
char c = cl i ent . r ead( ) ;
i f ( r eadi ng && c == ' ' ) r eadi ng = f al se;
i f ( c == ' ?' ) r eadi ng = t r ue; / / f ound t he ?, begi n r eadi ng t he i nf o
i f ( r eadi ng) {
Ser i al . pr i nt ( c) ;
swi t ch ( c) {
case ' 2' :
/ / add code her e t o t r i gger on 2
t r i gger Pi n( 2, cl i ent ) ;
br eak;
case ' 3' :
/ / add code her e t o t r i gger on 3
t r i gger Pi n( 3, cl i ent ) ;
br eak;
case ' 4' :
/ / add code her e t o t r i gger on 4
t r i gger Pi n( 4, cl i ent ) ;
br eak;
case ' 5' :
/ / add code her e t o t r i gger on 5
t r i gger Pi n( 5, cl i ent ) ;
br eak;
case ' 6' :
/ / add code her e t o t r i gger on 6
Page 2
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)
New Text Document ( 2) . t xt
t r i gger Pi n( 6, cl i ent ) ;
br eak;
case ' 7' :
/ / add code her e t o t r i gger on 7
t r i gger Pi n( 7, cl i ent ) ;
br eak;
case ' 8' :
/ / add code her e t o t r i gger on 8
t r i gger Pi n( 8, cl i ent ) ;
br eak;
case ' 9' :
/ / add code her e t o t r i gger on 9
t r i gger Pi n( 9, cl i ent ) ;
br eak;
}
}
i f ( c == ' \ n' && cur r ent Li neI sBl ank) br eak;
i f ( c == ' \ n' ) {
cur r ent Li neI sBl ank = t r ue;
}el se i f ( c ! = ' \ r ' ) {
cur r ent Li neI sBl ank = f al se;
}
}
}
del ay( 1) ; / / gi ve t he web br owser t i me t o r ecei ve t he dat a
cl i ent . st op( ) ; / / cl ose t he connect i on:
}
}
voi d t r i gger Pi n( i nt pi n, Et her net Cl i ent cl i ent ) {
/ / bl i nk a pi n - Cl i ent needed j ust f or HTML out put pur poses.
cl i ent . pr i nt ( "Tur ni ng on pi n ") ;
cl i ent . pr i nt l n( pi n) ;
cl i ent . pr i nt ( "<br >") ;
di gi t al Wr i t e( pi n, HI GH) ;
del ay( 25) ;
di gi t al Wr i t e( pi n, LOW) ;
del ay( 25) ;
}
Page 3
Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)

Anda mungkin juga menyukai