Hardware Reference
In-Depth Information
Listing 9-2 (continued)
31 Serial.println("New connection");
32 // An HTTP request ends with a blank line, wait until the
request has finished
33 boolean currentLineIsBlank = true;
34 while (client.connected())
35 {
36 if (client.available())
37 {
38 char c = client.read();
39 Serial.write(c);
40 // if you've gotten to the end of the line (received a newline
41 // character) and the line is blank, the HTTP request has ended,
42 // so you can send a reply
43 if (c == '\n' && currentLineIsBlank) {
44 // send a standard http response header
45 client.println("HTTP/1.1 200 OK");
46 client.println("Content-Type: text/html");
47 client.println("Connection: close");
48 client.println("Refresh: 5");
49 client.println();
50 client.println("<!DOCTYPE HTML>");
51 client.println("<html>");
52
53 // Get a light level reading
54 int light = analogRead(lightPin);
55
56 // Send this data as a web page
57 client.print("Current light level is ");
58 client.print(light);
59 client.println("<br />");
60
61 client.println("</html>");
62 break;
63 }
64 if (c == '\n') {
65 // you're starting a new line
66 currentLineIsBlank = true;
67 }
68 else if (c != '\r') {
69 // you've gotten a character on the current line
70 currentLineIsBlank = false;
71 }
72 }
73 }
74 // Wait a second for the client to receive data
75 delay(1);
76
77 // Close the connection
78 client.stop();
79
Search WWH ::




Custom Search