WiFI AP 01
#include <>
/*H*******************************************************
WiFi Web Server LED Blink
A simple web server that lets you blink an LED via web.
This sketch will create a new access point (with no password).
It willn launch a new server and print out IP address
to Serial monitor. Fromre, you can open that address in a web browser
to turn on and off LED on pin 13.
If IP address of your board is yourAddress:
http://yourAddress/H turns LED on
http://yourAddress/L turns it off
created 25 Nov 2012
by Tom Igoe
adapted to WiFi AP by Adafruit
********************************************************/
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
void printWiFiStatus();
//************************* 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 firmware" );
// BY DEFAULT 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 A 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
server.begin(); // START WEB SERVER ON PORT 80
printWiFiStatus(); // YOU'RE CONNECTED NOW, SO PRINT OUT STATUS
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
if( status != WiFi.status())// COMPARE PREVIOUS STATUS TO CURRENT STATUS
{
status = WiFi.status(); // IT HAS CHANGED UPDATE VARIABLE
if( status == WL_AP_CONNECTED)
Serial.println("Device connected to AP"); // DEVICE IS CONND TO AP
else // DEVICE DISCONNECTED FROM AP, AND 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 SERIAL PORT
String currentLine = ""; // HOLD INCOMING DATA FROM CLIENT
while( client.connected())
{ // LOOP WHILE CLIENT'S CONNECTED
if( client.available() )
{ // IFRE'S CLIENT BYTES TO READ
char c = client.read(); // READ A BYTE,n
Serial.write( c ); // PRINT IT OUT SERIAL MONITOR
if( c == '\n')
{ // IF BYTE IS A NEWLINE CHARACTER
// IF CURRENT LINE IS BLANK, GOT TWO NEWLINE CHARACTERS IN ROW
// THAT'S END OF CLIENT HTTP REQUEST, SEND 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 CLIENT KNOWS WHAT'S COMING,N A BLANK LINE:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// CONTENT OF HTTP RESPONSE FOLLOWS HEADER:
client.print("Click <a href=\"/H\">here</a> "
"turn LED on<br>");
client.print("Click <a href=\"/L\">here</a> "
"turn LED off<br>");
// HTTP RESPONSE ENDS WITH ANOTHER BLANK LINE:
client.println();
// BREAK OUT OF WHILE LOOP:
break;
}
else // IF YOU GOT A NEWLINE,N CLEAR CURRENTlINE:
currentLine = "";
}
else if( c != '\r') // ANYTHING ELSE BUT CR CHARACTER,
currentLine += c; // APPEND TO CURRENTlINE
// CHECK IF CLIENT REQUEST WAS "GET /H" OR "GET /L":
if( currentLine.endsWith("GET /H"))
digitalWrite( led, HIGH); // GET /H TURNS LED ON
if( currentLine.endsWith( "GET /L" ) )
digitalWrite( led, LOW ); // GET /L TURNS LED OFF
}
}
client.stop(); // CLOSE CONNECTION:
Serial.println( "client disconnected" );
}
}
/*F********************************************************************
*
**********************************************************************/
void
printWiFiStatus()
{
Serial.print( "SSID: "); // PRINT SSID OF NETWORK YOU'RE ATTACHED TO:
Serial.println( WiFi.SSID());
IPAddress ip = WiFi.localIP(); // PRINT YOUR WiFi SHIELD'S IP ADDRESS:
Serial.print( "IP Address: ");
Serial.println( ip );
Serial.print( "To see this page in action, open a browser to http://");
Serial.println( ip ); // PRINT WHERE TO GO IN A BROWSER:
}