mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-19 09:42:11 +03:00
Added WebServer
This commit is contained in:
@ -29,10 +29,12 @@ Client Server::available(byte* status)
|
|||||||
if (WiFiClass::_server_port[sock] != 0)
|
if (WiFiClass::_server_port[sock] != 0)
|
||||||
{
|
{
|
||||||
Client client(sock);
|
Client client(sock);
|
||||||
*status = client.status();
|
int _status = client.status();
|
||||||
|
if (status != NULL)
|
||||||
|
*status = _status;
|
||||||
|
|
||||||
if (WiFiClass::_server_port[sock] == _port &&
|
if (WiFiClass::_server_port[sock] == _port &&
|
||||||
*status == ESTABLISHED)
|
_status == ESTABLISHED)
|
||||||
{
|
{
|
||||||
return client; //TODO
|
return client; //TODO
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ private:
|
|||||||
void* pcb;
|
void* pcb;
|
||||||
public:
|
public:
|
||||||
Server(uint16_t);
|
Server(uint16_t);
|
||||||
Client available(uint8_t*);
|
Client available(uint8_t* status = NULL);
|
||||||
void begin();
|
void begin();
|
||||||
virtual void write(uint8_t);
|
virtual void write(uint8_t);
|
||||||
virtual void write(const char *str);
|
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)
|
void execCmd(char* buf)
|
||||||
{
|
{
|
||||||
Serial.print("Executing command: ");
|
Serial.print("\nExecuting command: ");
|
||||||
Serial.println(buf);
|
Serial.println(buf);
|
||||||
server.write(buf);
|
server.write(buf);
|
||||||
}
|
}
|
||||||
@ -159,7 +159,7 @@ void loop()
|
|||||||
//delay(2000);
|
//delay(2000);
|
||||||
if (client) {
|
if (client) {
|
||||||
if (!gotAMessage) {
|
if (!gotAMessage) {
|
||||||
Serial.println("We have a new client");
|
Serial.println("\nWe have a new client\n");
|
||||||
client.println("Hello, client!");
|
client.println("Hello, client!");
|
||||||
gotAMessage = true;
|
gotAMessage = true;
|
||||||
}
|
}
|
||||||
@ -181,14 +181,14 @@ void loop()
|
|||||||
|
|
||||||
while (client.available())
|
while (client.available())
|
||||||
{
|
{
|
||||||
delay(10);
|
|
||||||
dataBuf[idx] = client.read();
|
dataBuf[idx] = client.read();
|
||||||
|
if (idx == 0) Serial.print("Client chatting...: ");
|
||||||
Serial.print(dataBuf[idx]);
|
Serial.print(dataBuf[idx]);
|
||||||
if (dataBuf[idx] = 0xa)
|
if ((dataBuf[idx] == 0xa)/*||(dataBuf[idx] == 0xd)*/)
|
||||||
{
|
{
|
||||||
dataBuf[idx+1]=0;
|
dataBuf[idx+1]=0;
|
||||||
//Serial.println((char*)dataBuf);
|
//Serial.println((char*)dataBuf);
|
||||||
//execCmd((char*)dataBuf);
|
execCmd((char*)dataBuf);
|
||||||
idx=0;
|
idx=0;
|
||||||
}else{
|
}else{
|
||||||
++idx;
|
++idx;
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
// Maxmium number of socket
|
// Maxmium number of socket
|
||||||
#define MAX_SOCK_NUM 4
|
#define MAX_SOCK_NUM 4
|
||||||
//Maximum number of attempts to establish wifi connection
|
//Maximum number of attempts to establish wifi connection
|
||||||
#define WL_MAX_ATTEMPT_CONNECTION 5
|
#define WL_MAX_ATTEMPT_CONNECTION 10
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WL_IDLE_STATUS,
|
WL_IDLE_STATUS,
|
||||||
|
Reference in New Issue
Block a user