From ca1a1d8a9d451db55b30c463d1355253e4570178 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Thu, 21 Apr 2011 19:16:39 +0200 Subject: [PATCH] Added WebServer --- WiFi/Server.cpp | 6 +- WiFi/Server.h | 2 +- WiFi/examples/WebServer/WebServer.pde | 97 +++++++++++++++++++ .../wifi_Server_example_WPA.pde | 10 +- WiFi/utility/wl_definitions.h | 2 +- 5 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 WiFi/examples/WebServer/WebServer.pde diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index 8dd091496..90ef0a321 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -29,10 +29,12 @@ Client Server::available(byte* status) if (WiFiClass::_server_port[sock] != 0) { Client client(sock); - *status = client.status(); + int _status = client.status(); + if (status != NULL) + *status = _status; if (WiFiClass::_server_port[sock] == _port && - *status == ESTABLISHED) + _status == ESTABLISHED) { return client; //TODO } diff --git a/WiFi/Server.h b/WiFi/Server.h index e48c6a55b..cf586c6ad 100755 --- a/WiFi/Server.h +++ b/WiFi/Server.h @@ -15,7 +15,7 @@ private: void* pcb; public: Server(uint16_t); - Client available(uint8_t*); + Client available(uint8_t* status = NULL); void begin(); virtual void write(uint8_t); virtual void write(const char *str); diff --git a/WiFi/examples/WebServer/WebServer.pde b/WiFi/examples/WebServer/WebServer.pde new file mode 100644 index 000000000..83869073f --- /dev/null +++ b/WiFi/examples/WebServer/WebServer.pde @@ -0,0 +1,97 @@ +/* + 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 + */ + +#include +#include + +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; + +Server server(80); + +int startWiFiWpa() +{ + Serial.println("\nSetup WiFi Wpa..."); + //strcpy(ssid, "AndroidAP9647"); + strcpy(ssid, "Cariddi"); + Serial.print("SSID: "); + Serial.println(ssid); + const char *pass = "1234567890"; + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) + { + Serial.println("Connection Failed"); + } + return status; +} + +void setup() +{ + // start the WiFi connection and the server: + Serial.begin(9600); + Serial.println("*** Start WebServer WiFi example ***"); + + int _status = startWiFiWpa(); + if ( _status == WL_CONNECTED) + { + Serial.println("\nStarting server..."); + server.begin(); + } +} + +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(); + + // 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("
"); + } + 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(); + } +} diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde index 6ea176fa3..e9b27b15d 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -144,7 +144,7 @@ void setup() void execCmd(char* buf) { - Serial.print("Executing command: "); + Serial.print("\nExecuting command: "); Serial.println(buf); server.write(buf); } @@ -159,7 +159,7 @@ void loop() //delay(2000); if (client) { if (!gotAMessage) { - Serial.println("We have a new client"); + Serial.println("\nWe have a new client\n"); client.println("Hello, client!"); gotAMessage = true; } @@ -181,14 +181,14 @@ void loop() while (client.available()) { - delay(10); dataBuf[idx] = client.read(); + if (idx == 0) Serial.print("Client chatting...: "); Serial.print(dataBuf[idx]); - if (dataBuf[idx] = 0xa) + if ((dataBuf[idx] == 0xa)/*||(dataBuf[idx] == 0xd)*/) { dataBuf[idx+1]=0; //Serial.println((char*)dataBuf); - //execCmd((char*)dataBuf); + execCmd((char*)dataBuf); idx=0; }else{ ++idx; diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index a1dd6d525..e28064a3d 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -23,7 +23,7 @@ // Maxmium number of socket #define MAX_SOCK_NUM 4 //Maximum number of attempts to establish wifi connection -#define WL_MAX_ATTEMPT_CONNECTION 5 +#define WL_MAX_ATTEMPT_CONNECTION 10 typedef enum { WL_IDLE_STATUS,