mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
Added WebServer
This commit is contained in:
parent
49912f241f
commit
ca1a1d8a9d
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
|
97
WiFi/examples/WebServer/WebServer.pde
Normal file
97
WiFi/examples/WebServer/WebServer.pde
Normal file
@ -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 <WiFi.h>
|
||||
#include <IPAddress.h>
|
||||
|
||||
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("<br />");
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user