Figure 10-2
An HTML example
Arduino as a Web Server
The first example sketch simply uses the Arduino and Ethernet shield to make a small web
server. It’s definitely not a Google server farm, but it will allow you to send a web request
to your Arduino and view the results in a browser on your computer.
Before uploading the sketch 10–01, there are a couple of changes that you need to
make. If you look at the top of the sketch, you will see the following lines:
The first of these, the
mac
address, must be unique among all the devices connected to
your network. The second one is the IP address. Whereas most devices that you connect to
your home network will have IP addresses assigned to them
automatically by a protocol
called Dynamic Host Configuration Protocol (DHCP), this is not true for the Ethernet
shield. For this device, you have to define an IP address manually. This address cannot be
any four numbers; they must be numbers that qualify as being internal IP addresses and fit
in the range of IP addresses expected by your home router.
Typically, the first three
numbers will be something like 10.0.1.
x
or 192.168.1.
x
, where
x
is a number between 0
and 255. Some of these IP addresses will be in use by other devices on your network. To
find
an unused but valid IP address, connect to the administration page for your home
router and look for an option that says “DHCP.” You should find a list of devices and their
IP addresses, similar to that shown in
Figure 10-3
. Select a final number to use in you IP
address. In this case, 192.168.1.30 looked like a good bet, and indeed it worked fine.
Attach the Arduino to your computer using the USB lead and upload the sketch. You
can now disconnect the USB lead and attach the power supply
to the Arduino and the
Ethernet lead.
Open a connection on your computer’s browser to the IP address that you assigned for
the Ethernet shield. Something very much like
Figure 10-4
should appear.
Dostları ilə paylaş: