-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from LeeLeahy2/4-11-WiFi_AP_Setup
4-11:Add example 4_11_WiFi_AP_Setup
- Loading branch information
Showing
5 changed files
with
933 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
Example_Sketches/4_11_WiFi_AP_Setup/4_11_WiFi_AP_Setup.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/********************************************************************** | ||
4_11_WiFi_AP_Setup.ino | ||
Example sketch to demonstrate WiFi AP setup | ||
**********************************************************************/ | ||
|
||
//**************************************** | ||
// Includes | ||
//**************************************** | ||
|
||
#include <Arduino.h> // Built-in | ||
#include <ESPmDNS.h> // Built-in | ||
#include <esp_wifi.h> // IDF built-in | ||
#include <Network.h> // Built-in | ||
#include <WiFi.h> // Built-in | ||
|
||
#include <secrets.h> // Host name, SSIDs and passwords | ||
|
||
bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false; | ||
|
||
#define systemPrintf Serial.printf | ||
#define systemPrintln Serial.println | ||
|
||
#define SERVER_PORT 80 | ||
|
||
//**************************************** | ||
// WiFi support | ||
//**************************************** | ||
|
||
typedef uint8_t WIFI_CHANNEL_t; | ||
typedef uint16_t WIFI_START_LIST_t; | ||
|
||
bool debug; | ||
bool display; | ||
bool mDnsRunning; | ||
bool softApHasIp; | ||
bool softApRunning; | ||
int softApStationCount; | ||
|
||
//********************************************************************* | ||
// Entry point for the application | ||
void setup() | ||
{ | ||
// Initialize the USB serial port | ||
Serial.begin(115200); | ||
systemPrintln(); | ||
systemPrintf(__FILE__ "\r\n"); | ||
|
||
// Initialize WiFi | ||
wifiBegin(); | ||
} | ||
|
||
//********************************************************************* | ||
// Idle loop for core 1 of the application | ||
void loop() | ||
{ | ||
wifiUpdate(); | ||
serverUpdate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
WiFi Web Server LED Blink | ||
A simple web server that lets you blink an LED via the web. | ||
*/ | ||
|
||
#define LED_STATUS 2 | ||
|
||
NetworkClient client; | ||
String headerLine; | ||
String pageTitle; | ||
NetworkServer * server; | ||
|
||
// Turn off the LED | ||
void ledOff() | ||
{ | ||
digitalWrite(LED_STATUS, LOW); | ||
} | ||
|
||
// Turn on the LED | ||
void ledOn() | ||
{ | ||
digitalWrite(LED_STATUS, HIGH); | ||
} | ||
|
||
// Initialize the server | ||
bool serverBegin(IPAddress ipAddress, uint16_t port) | ||
{ | ||
// Determine if the server is already started | ||
if (!server) | ||
{ | ||
// Allocate the server structure | ||
server = new NetworkServer(ipAddress, port); | ||
if (server) | ||
{ | ||
server->begin(); | ||
|
||
// Display the server IP address and port for remote connections | ||
Serial.printf("WiFi Server: %s:%d\r\n", ipAddress.toString().c_str(), port); | ||
|
||
// Turn off the LED | ||
ledOff(); | ||
pinMode(LED_STATUS, OUTPUT); | ||
} | ||
else | ||
Serial.printf("ERROR: Failed to allocate the server!\r\n"); | ||
} | ||
|
||
// Return the initialization status | ||
return (server != nullptr); | ||
} | ||
|
||
void serverProcessRequest() | ||
{ | ||
int getPosition; | ||
|
||
// Read the HTTP request from the remote client | ||
while (client.available()) | ||
{ | ||
char c = client.read(); | ||
|
||
// Ignore this line | ||
if (c == '\n') | ||
headerLine = ""; | ||
|
||
// Build the header line | ||
else if (c != '\r') | ||
headerLine += c; | ||
|
||
// The HTTP header ends with a blank line | ||
// Display the web-page after receiving the entire request | ||
else if (headerLine.length() == 0) | ||
{ | ||
// Respond with the web page contents | ||
// 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(); | ||
|
||
// Web page contents | ||
client.printf("<html>\r\n"); | ||
client.printf(" <head>\r\n"); | ||
client.printf(" <title>%s</title>\r\n", pageTitle.c_str()); | ||
client.printf(" </head>\r\n"); | ||
client.printf(" <body>\r\n"); | ||
client.printf(" <h1>Status %s</h1>\r\n", pageTitle.c_str()); | ||
client.printf(" Click <a href=\"/H\">here</a> to turn the status LED (pin %d) on.<br>\r\n", LED_STATUS); | ||
client.printf(" Click <a href=\"/L\">here</a> to turn the status LED (pin %d) off.<br>\r\n", LED_STATUS); | ||
client.printf(" </body>\r\n"); | ||
client.printf("</html>\r\n"); | ||
|
||
// The HTTP response ends with another blank line: | ||
client.println(); | ||
|
||
// Break the client connection | ||
client.stop(); | ||
} | ||
|
||
// Process the request | ||
if (headerLine.endsWith("GET /H")) | ||
{ | ||
ledOn(); | ||
pageTitle = "LED On"; | ||
} | ||
else if (headerLine.endsWith("GET /L")) | ||
{ | ||
ledOff(); | ||
pageTitle = "LED Off"; | ||
} | ||
} | ||
} | ||
|
||
void serverStop() | ||
{ | ||
if (server) | ||
{ | ||
server->stop(); | ||
delete server; | ||
server = nullptr; | ||
} | ||
} | ||
|
||
// Update the server | ||
void serverUpdate() | ||
{ | ||
if (server) | ||
{ | ||
// Wait for a client connection | ||
if (!client) | ||
{ | ||
client = server->accept(); | ||
if (client) | ||
{ | ||
headerLine = ""; | ||
pageTitle = "LED Control"; | ||
} | ||
} | ||
|
||
// Determine is a new client connection request was received | ||
if (client) | ||
{ | ||
// Process the request from the server | ||
if (client.connected()) | ||
serverProcessRequest(); | ||
else | ||
{ | ||
client.stop(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.