Hardware Reference
In-Depth Information
Creating a web server on Arduino
For the rest of this chapter, we are going to take another approach. We are still going to
make the Arduino with the Ethernet Shield work as an independent system, but this time
we are even going to create a web server on the Arduino board. This is actually similar to
the project we saw in the Chapter 2 , Sending Data to a Web Server , but this time it is the
Arduino board itself that will display the data as it is measured.
The sketch for this part starts by including the right libraries, as shown in the following
code:
#include "DHT.h"
#include <Ethernet.h>
#include <SPI.h>
Define the pin and the type of the sensor in the following manner:
#define DHTPIN 7
#define DHTTYPE DHT11
Also, you have to define the MAC address of your Ethernet shield as usual, as shown in the
following line of code:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };
We will also define a default IP address in case the DHCP fails, as shown in the following
line:
IPAddress ip(192,168,1,50);
This is where the new feature comes in. So far, we only used the EthernetClient class
to define a client that will connect to a remote server. Here, however, because we want to
create a server right on the Arduino board, we are going to use the EthernetServer
class, as shown in the following line of code:
EthernetServer server(80);
You can use the port 80 for your server, as it will be much easier to access the server from
your web browser.
Search WWH ::




Custom Search