Hardware Reference
In-Depth Information
it must reconnect. To tell the browser that the content is about to be sent, the
server sends a blank line, and then sends the HTML data.
Example Program
For this program, you use an Arduino Uno with an Ethernet shield. This is a
continuation of the previous chapter and still uses the light sensor. You can
now read light conditions in real time by connecting to your Arduino from a
web browser.
When a connection is made, the Arduino i rst reads the analog value on A3
before displaying that value in HTML.
Sketch
Now it's time to write the sketch, as shown in Listing 9-2.
Listing 9-2: Server Sketch (fi lename: Chapter9server.ino )
1 #include <SPI.h>
2 #include <Ethernet.h>
3
4 // Enter a MAC address and IP address for your controller below.
5 // The IP address will be dependent on your local network:
6 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
7 IPAddress ip(192,168,0,177);
8
9 int lightPin = A3;
10
11 //Initialize the Ethernet server to listen for connections on port 80
12 EthernetServer server(80);
13
14 void setup() {
15 // Open serial communications
16 Serial.begin(9600);
17
18 // start the Ethernet connection and the server:
19 Ethernet.begin(mac, ip);
20 server.begin();
21 Serial.print("Server up on ");
22 Serial.println(Ethernet.localIP());
23 }
24
25 void loop() {
26 // Listen for incoming clients
27 EthernetClient client = server.available();
28
29 if (client)
30 {
Continues
 
Search WWH ::




Custom Search