You are on page 1of 3

Web-Controlled Arduino

This second example of using an Ethernet shield allows you to turn the Arduino pins
D3 to D7 on and off using a Web form.
Unlike the simple server example, you are going to have to find a way to pass the
pin settings to the Arduino.
The method for doing this is called posting data and is part of the HTTP
standard. For this method to work, you have to build the posting mechanism into the
HTML so that the Arduino returns HTML to the browser, which renders a form. This
form (shown in Figure 10-5 ) has a selection of On and Off for each pin and an
Update button that will send the pin settings to the Arduino.
Figure 10.5 A Web interface to Arduino pins .

When the Update button is pressed, a second request is sent to the Arduino. This
will be just like the first request, except that the request will contain request
parameters that will contain the values of the pins.
A request parameter is similar in concept to a function parameter. A function
parameter enables you to get information to a function, such as the number of times to
blink, and a request parameter enables you to pass data to the Arduino that is going to
handle the Web request. When the Arduino receives the Web request, it can extract
the pin settings from the request parameter and change the actual pins.
The code for the second example sketch follows:

// sketch 10-02 Internet Pins

#include <SPI.h>

#include <Ethernet.h>

// MAC address just has to be unique. This should work

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


EthernetServer server(80);

int numPins = 5;

int pins[] = {3, 4, 5, 6, 7};

int pinState[] = {0, 0, 0, 0, 0};

char line1[100];

void setup()

for (int i = 0; i < numPins; i++)

pinMode(pins[i], OUTPUT);

Serial.begin(9600);

Ethernet.begin(mac);

server.begin();

Serial.print("Server started on: ");

Serial.println(Ethernet.localIP());

You might also like