WiFI AP 01
#include <>
/*H*******************************************************
WiFi Web Server LED Blink
A simple web server that lets you blink an LED via the web.
This sketch will create a new access point (with no password).
It will then launch a new server and print out the IP address
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 13.
If the IP address of your board is yourAddress:
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off
created 25 Nov 2012
by Tom Igoe
adapted to WiFi AP by Adafruit
********************************************************/
#include
#include
#include "arduino_secrets.h"
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
//************************* VARIABLES ************************************
char ssid[] = SECRET_SSID; // YOUR NETWORK SSID (NAME)
char pass[] = SECRET_PASS;// NETWORK PASSWORD (USE FOR WPA, OR USE AS KEY FOR WEP)
int keyIndex = 0; // YOUR NETWORK KEY iNDEX NUMBER (NEEDED ONLY FOR WEP)
int led = LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
//Initialize serial and wait for port to open:
Serial.begin( 9600 );
while( !Serial )
{
;// WAIT FOR SERIAL PORT TO CONNECT. NEEDED FOR NATIVE USB PORT ONLY
}
Serial.println( "ACCESS POINT WEB SERVER" );
pinMode( led, OUTPUT ); // SET LED PIN MODE
if( WiFi.status() == WL_NO_MODULE ) // CHECK FOR WiFi MODULE
{
Serial.println( "Communication with WiFi module failed!");
while( true ); // DON'T CONTINUE
}
String fv = WiFi.firmwareVersion();
if( fv < WIFI_FIRMWARE_LATEST_VERSION)
Serial.println( "Please upgrade the firmware" );
// BY DEFAULT THE LOCAL IP ADDRESS WILL BE 192.168.4.1
// YOU CAN OVERRIDE IT WITH FOLLOWING
// WiFi.config( IPAddress( 10, 0, 0, 1 ) );
// PRINT NETWORK NAME ( SSID );
Serial.print( "Creating access point named: " );
Serial.println( ssid );
// CREATE OPEN NETWORK. CHANGE THIS LINE IF YOU WANT TO CREATE AN WEP NETWORK
status = WiFi.beginAP( ssid, pass );
if( status != WL_AP_LISTENING)
{
Serial.println( "Creating access point failed");
while( true ); // DON'T CONTINUE
}
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
// start the web server on port 80
server.begin();
// you're connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if( status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if( status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if( client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while( client.connected()) { // loop while the client's connected
if( client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if( c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if( currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click here turn the LED on
");
client.print("Click here turn the LED off
");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if( c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if( currentLine.endsWith("GET /H")) {
digitalWrite(led, HIGH); // GET /H turns the LED on
}
if( currentLine.endsWith("GET /L")) {
digitalWrite(led, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}