Instructables

Step 4: Server

Picture of Server
5B.jpg
5C.jpg
5D.jpg
You can use the Arduino Ethernet shield as a web server to load an HTML page or function as a chat server. You can also parse requests sent by a client, such as a web browser. The following two examples show how to use it to serve HTML pages, and parse URL strings.

One important thing to keep in mind is that you will have to enter your Arduino's IP address in both of the examples below in order for them to work.

The following code changes the web page served based on a button press:
To make this example code work, simply attach a button between pin D2 and 5V,  a 10K resistor between pin D2 and ground, and then load the IP address of your Arduino into your web browser. The page should load with a black background. Press and hold the button, and then refresh the browser page. The site should now load with a white background.


The following code lights up an LED depending on the URL that is sent to the Arduino:

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm resistor to ground.

To turn on the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$1

To turn off the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$2

Note: You should obviously replace [YOUR IP ADDRESS HERE] with your IP address.
 
Remove these adsRemove these ads by Signing Up
Dylon1241 year ago
I know this sounds dumb but where how did you find your ip?
randofo (author)  Dylon1241 year ago
See the previous step (Step 3)
ursul1 year ago
You have to set your router to make a port forwarding from your "public" IP address to the arduino IP address, as you would do for whatever device.
Go to your router setting panel, find your arduino in the connected devices list, assign a http service (port 80) to it. You can find an example of how to do it here (find on the website the router you use) : http://portforward.com/english/routers/port_forwarding/Thomson/DWG855/HTTP.htm

Then, if your ISP provide you a static IP you can now "talk" to the arduino using an URL like: http://[yourStaticIp]:80/$1 http://[yourStaticIp]:80/$2
If your IP is dynamic, you can obtain a static IP on http://dyn.com. Good luck!
hi, i have successfully sent commands from a browser to ethernet shield in same network. but, can u please tell me how to do same if i have to do it from a separate network? actually i want to make an android app to operate ethernet shield, so i need it necessarily.
sshapiro21 year ago
SOLUTION:

sorry i had the wrong resistors in the circuit, was using those baby blue ones, I find them hard to read. make sure its the right resistor!
sshapiro21 year ago
Thanks!

Any advice: i have confirmed ip address with addressprinter.

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x56, 0xC6 };
IPAddress ip(10,0,0,10); //<<< ENTER YOUR IP ADDRESS HERE!!!

The light goes on when i uplaod the script but wont respond to the ip $1 or $2, the page just sits and doesnt load. i even tried the script below to return a black screen and nothing loads on that ip.

Thanks!
i have a similar problem with the Switch activated code. It loads a white page with the words DARk but nothing happens when the switch is pressed, tried refreshing etc
clsorens1 year ago
I know it's kinda old-ish post, but just in case anyone else is still having this problem like I was too here's a solution.

It should still turn the light on, but the reason it's not displaying a web page is because the code doesn't return a web page.

change the "
if(c == '\n')
{
currentLineIsBlank = true;
} " to "

if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("");
client.println("");
client.println("");
client.println("");
currentLineIsBlank = true;
break;
}

"

This code will return a web page with the background set to black (#000000) after light turns on.
lamarts2 years ago
Great tutorial.

ethernet board is connected to router,
laptop is connected to router

192.168.1.9/$1 and $2 turns LED on and off.

issue is: web page is not being displayed
"waiting for response from 192.168.1.9" is displayed in the status bar.& "processing" circle continues to wait for response.

any ideas? sounds like something real simple...
I am getting the exact same thing.

The DhcpAddressPrinter gave me 192.168.0.13

so I put this in the sketch
IPAddress ip(192,168,0,13);

In both sketches I have kept the mac address
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

When I load the page with 192.168.0.13/$1
I the page just says connecting and waiting for 192.168.0.13...

The light does not go on?