mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-03 14:24:15 +01:00
101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
/*
|
|
Web Server
|
|
|
|
A simple web server that shows the value of the analog input pins.
|
|
using a WiFi shield.
|
|
|
|
Circuit:
|
|
* WiFi shield attached
|
|
* Analog inputs attached to pins A0 through A5 (optional)
|
|
|
|
created 13 July 2010
|
|
by Domenico La Fauci
|
|
modified 5 June 2011
|
|
by Tom Igoe
|
|
*/
|
|
|
|
|
|
#include <SPI.h>
|
|
#include <WiFi.h>
|
|
|
|
char ssid[] = "yourNetwork";
|
|
char pass[] = "secretpassword";
|
|
int status = WL_IDLE_STATUS;
|
|
|
|
Server server(80);
|
|
|
|
void setup() {
|
|
// initialize serial:
|
|
Serial.begin(9600);
|
|
Serial.println("Attempting to connect to WPA network...");
|
|
Serial.print("SSID: ");
|
|
Serial.println(ssid);
|
|
|
|
status = WiFi.begin(ssid, pass);
|
|
if ( status != WL_CONNECTED) {
|
|
Serial.println("Couldn't get a wifi connection");
|
|
while(true);
|
|
}
|
|
else {
|
|
server.begin();
|
|
Serial.print("Connected to wifi. My address:");
|
|
IPAddress myAddress = WiFi.localIp();
|
|
Serial.print(myAddress[0]);
|
|
Serial.print(".");
|
|
Serial.print(myAddress[1]);
|
|
Serial.print(".");
|
|
Serial.print(myAddress[2]);
|
|
Serial.print(".");
|
|
Serial.println(myAddress[3]);
|
|
}
|
|
}
|
|
|
|
|
|
void loop() {
|
|
// listen for incoming clients
|
|
Client client = server.available();
|
|
if (client) {
|
|
// an http request ends with a blank line
|
|
boolean currentLineIsBlank = true;
|
|
while (client.connected()) {
|
|
if (client.available()) {
|
|
char c = client.read();
|
|
// if you've gotten to the end of the line (received a newline
|
|
// character) and the line is blank, the http request has ended,
|
|
// so you can send a reply
|
|
if (c == '\n' && currentLineIsBlank) {
|
|
// send a standard http response header
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
client.println();
|
|
client.println("<html>");
|
|
// output the value of each analog input pin
|
|
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
|
|
client.print("analog input ");
|
|
client.print(analogChannel);
|
|
client.print(" is ");
|
|
client.print(analogRead(analogChannel));
|
|
client.println("<br />");
|
|
}
|
|
client.println("</html>");
|
|
break;
|
|
}
|
|
if (c == '\n') {
|
|
// you're starting a new line
|
|
currentLineIsBlank = true;
|
|
}
|
|
else if (c != '\r') {
|
|
// you've gotten a character on the current line
|
|
currentLineIsBlank = false;
|
|
}
|
|
}
|
|
}
|
|
// give the web browser time to receive the data
|
|
delay(1);
|
|
// close the connection:
|
|
client.stop();
|
|
}
|
|
}
|
|
|
|
|