Skip to content

Commit

Permalink
4-11:Add example 4_11_WiFi_AP_Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeLeahy2 committed Nov 24, 2024
1 parent aa354e4 commit a8fdaa4
Show file tree
Hide file tree
Showing 5 changed files with 933 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Example_Sketches/4_11_WiFi_AP_Setup/4_11_WiFi_AP_Setup.ino
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();
}
152 changes: 152 additions & 0 deletions Example_Sketches/4_11_WiFi_AP_Setup/HTTP_Server.ino
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();
}
}
}
}
Loading

0 comments on commit a8fdaa4

Please sign in to comment.