From 667678b7b8f7deb122de78f87d4bccd019db0e29 Mon Sep 17 00:00:00 2001 From: Lee Leahy Date: Tue, 19 Nov 2024 09:21:16 -1000 Subject: [PATCH] 4-10: Add 4_10_WiFi_AP_Server example --- .../4_10_WiFi_AP_Server.ino | 98 ++++++++++ .../4_10_WiFi_AP_Server/HTTP_Server.ino | 125 +++++++++++++ Example_Sketches/4_10_WiFi_AP_Server/makefile | 167 ++++++++++++++++++ .../4_10_WiFi_AP_Server/secrets.h | 5 + 4 files changed, 395 insertions(+) create mode 100644 Example_Sketches/4_10_WiFi_AP_Server/4_10_WiFi_AP_Server.ino create mode 100644 Example_Sketches/4_10_WiFi_AP_Server/HTTP_Server.ino create mode 100644 Example_Sketches/4_10_WiFi_AP_Server/makefile create mode 100644 Example_Sketches/4_10_WiFi_AP_Server/secrets.h diff --git a/Example_Sketches/4_10_WiFi_AP_Server/4_10_WiFi_AP_Server.ino b/Example_Sketches/4_10_WiFi_AP_Server/4_10_WiFi_AP_Server.ino new file mode 100644 index 0000000..f331b3e --- /dev/null +++ b/Example_Sketches/4_10_WiFi_AP_Server/4_10_WiFi_AP_Server.ino @@ -0,0 +1,98 @@ +/* + WiFi Web Server LED Blink + + A simple web server that lets you blink an LED via the web. + This sketch will print the IP address of your WiFi Shield (once connected) + to the Serial monitor. From there, you can open that address in a web browser + to turn on and off the LED on pin 5. + + If the IP address of your shield is yourAddress: + http://yourAddress/H turns the LED on + http://yourAddress/L turns it off + + This example is written for a network using WPA2 encryption. For insecure + WEP or WPA, change the Wifi.begin() call and use Wifi.setMinSecurity() accordingly. + + Circuit: + * WiFi shield attached + * LED attached to pin 5 + + created for arduino 25 Nov 2012 + by Tom Igoe + +ported for sparkfun esp32 +31.01.2017 by Jan Hendrik Berlin + + Updated to support SparkFun RTK EVK, 21 Nov 2024 +*/ + +#include // IDF built-in +#include +#include "secrets.h" + +#define WIFI_MODE WIFI_MODE_AP +//#define WIFI_MODE WIFI_MODE_STA + +#define SERVER_PORT 80 + +void setup() +{ + IPAddress ipAddress; + uint8_t protocols; + const char * ssid; + esp_err_t status; + + Serial.begin(115200); + Serial.println(); + Serial.printf("%s\r\n", __FILE__); + + delay(10); + + // We start by connecting to a WiFi network + + if (WIFI_MODE == WIFI_MODE_AP) + { + ssid = apSSID; + WiFi.softAP(apSSID, apPassword, 1, false, 4, false); + } + else + { + ssid = staSSID; + WiFi.begin(staSSID, staPassword); + } + Serial.printf("%s SSID: %s\r\n", + (WIFI_MODE == WIFI_MODE_AP) ? "Soft AP broadcasting" + : "Station connected to", + ssid); + + // Determine the protocols that are enabled + protocols = 0; + status = esp_wifi_get_protocol(WIFI_IF_AP, &protocols); + if (status != ESP_OK) + Serial.printf("ERROR: esp_wifi_get_protocol failed, status: 0x%x!\r\n", status); + + // Make sure the 802.3 protocols are enabled + protocols |= WIFI_PROTOCOL_11B + | WIFI_PROTOCOL_11N +// | WIFI_PROTOCOL_11AX + | WIFI_PROTOCOL_11G; + status = esp_wifi_set_protocol(WIFI_IF_AP, protocols); + if (status != ESP_OK) + Serial.printf("ERROR: esp_wifi_set_protocol failed, status: 0x%x!\r\n", status); + + while (!(WiFi.AP.started() || (WiFi.STA.started() && WiFi.STA.hasIP()))) + { + delay(50); + Serial.print("."); + } + Serial.println(); + + // Start the web server + ipAddress = (WIFI_MODE == WIFI_MODE_AP) ? WiFi.softAPIP() : WiFi.localIP(); + serverBegin(ipAddress, SERVER_PORT); +} + +void loop() +{ + serverUpdate(); +} diff --git a/Example_Sketches/4_10_WiFi_AP_Server/HTTP_Server.ino b/Example_Sketches/4_10_WiFi_AP_Server/HTTP_Server.ino new file mode 100644 index 0000000..1ec441c --- /dev/null +++ b/Example_Sketches/4_10_WiFi_AP_Server/HTTP_Server.ino @@ -0,0 +1,125 @@ +/* + 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; +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) +{ + // Turn off the LED + ledOff(); + pinMode(LED_STATUS, OUTPUT); + + // 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); + } + 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("\r\n"); + client.printf(" \r\n"); + client.printf(" LED Control\r\n"); + client.printf(" \r\n"); + client.printf(" \r\n"); + client.printf("

Status LED Control

\r\n"); + client.printf(" Click here to turn the status LED (pin %d) on.
\r\n", LED_STATUS); + client.printf(" Click here to turn the status LED (pin %d) off.
\r\n", LED_STATUS); + client.printf(" \r\n"); + client.printf("\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(); + else if (headerLine.endsWith("GET /L")) + ledOff(); + } +} + +// Update the server +void serverUpdate() +{ + // Wait for a client connection + if (!client) + { + client = server->accept(); + if (client) + headerLine = ""; + } + + // Determine is a new client connection request was received + if (client) + { + // Process the request from the server + if (client.connected()) + serverProcessRequest(); + else + { + client.stop(); + } + } +} diff --git a/Example_Sketches/4_10_WiFi_AP_Server/makefile b/Example_Sketches/4_10_WiFi_AP_Server/makefile new file mode 100644 index 0000000..78f4de3 --- /dev/null +++ b/Example_Sketches/4_10_WiFi_AP_Server/makefile @@ -0,0 +1,167 @@ +###################################################################### +# makefile +# +# Builds the example +###################################################################### + +########## +# Source files +########## + +EXAMPLE_SKETCH=4_10_WiFi_AP_Server + +EXECUTABLES += example + +PARTITION_CSV_FILE=RTKEverywhere + +ifeq ($(OS),Windows_NT) +# Windows NT utilities +CLEAR=cls +COPY=copy +DELETE=rmdir /s +DIR_LISTING=dir +TERMINAL_APP= +TERMINAL_PORT="COM3" +TERMINAL_PARAMS= + +# Windows NT generic paths +USER_DIRECTORY_PATH=C:\Users\$(USERNAME) +ARDUINO_LIBRARY_PATH=$(USER_DIRECTORY_PATH)\Documents\Arduino\libraries +HOME_BOARD_PATH=$(USER_DIRECTORY_PATH)\AppData\Local\Arduino15\packages\esp32 +PATCH_SRC_PATH=Patch\ + +# Windows NT patch source paths +PARTITION_SRC_PATH=..\$(PARTITION_CSV_FILE).csv +PATCH_SRC_PATH=Patch\ + +# Windows NT patch destination paths +BLE_PATCH_DST_PATH=$(ARDUINO_LIBRARY_PATH)\ESP32_BleSerial\src\ +MBED_LIB_DEST_PATH=$(HOME_BOARD_PATH)\tools\esp32-arduino-libs\${{ env.ESP_IDF }}\esp32/lib\ +PARTITION_DST_PATH=$(HOME_BOARD_PATH)\hardware\esp32\$(ESP_CORE_VERSION)\tools\partitions\$(PARTITION_CSV_FILE).csv + +else +# Linux utilities +CLEAR=clear +COPY=cp +DELETE=rm -Rf +DIR_LISTING=ls +TERMINAL_APP=minicom +#TERMINAL_PORT="/dev/ttyACM1" +TERMINAL_PORT="/dev/ttyUSB0" +TERMINAL_PARAMS=-b 115200 -8 < /dev/tty + +# Linux generic paths +USER_DIRECTORY_PATH=~ +ARDUINO_LIBRARY_PATH=$(USER_DIRECTORY_PATH)/Arduino/libraries +ESP_IDF_PATH=$(HOME_BOARD_PATH)/tools/esp32-arduino-libs +HOME_BOARD_PATH=$(USER_DIRECTORY_PATH)/.arduino15/packages/esp32 + +# Linux patch source paths +PARTITION_SRC_PATH=../$(PARTITION_CSV_FILE).csv +PATCH_SRC_PATH=Patch/ + +# Linux patch destination paths +BLE_PATCH_DST_PATH=$(ARDUINO_LIBRARY_PATH)/ESP32_BleSerial/src/ +MBED_LIB_DEST_PATH=$(ESP_IDF_PATH)/$(ESP_IDF_VERSION)/esp32/lib/ +PARTITION_DST_PATH=$(HOME_BOARD_PATH)/hardware/esp32/$(ESP_CORE_VERSION)/tools/partitions/$(PARTITION_CSV_FILE).csv + +endif + +########## +# Buid all the sources - must be first +########## + +.PHONY: all + +all: $(EXECUTABLES) + +########## +# Add ESP32 board support +########## + +.PHONY: arduino-config + +arduino-config: + arduino-cli config init --overwrite --additional-urls "https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json,https://espressif.github.io/arduino-esp32/package_esp32_dev_index.json" + +ESP_CORE_VERSION=3.0.1 +#ESP_CORE_VERSION=3.0.5 +#ESP_CORE_VERSION=3.0.7 + +.PHONY: core-update + +core-update: + arduino-cli core update-index + arduino-cli core install esp32:esp32@$(ESP_CORE_VERSION) + +.PHONY: partition-update + +partition-update: + $(COPY) $(PARTITION_SRC_PATH) $(PARTITION_DST_PATH) + +########## +# Build an example +########## + +.PHONY: example + +example: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin + +DEBUG_LEVEL=none +#DEBUG_LEVEL=debug +PSRAM=disabled +#PSRAM=enabled + +build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin: $(EXAMPLE_SKETCH).ino *.ino *.h makefile + $(CLEAR) + arduino-cli compile --fqbn "esp32:esp32:esp32":DebugLevel=$(DEBUG_LEVEL),PSRAM=$(PSRAM) $< \ + --warnings default \ + --build-property build.partitions=$(PARTITION_CSV_FILE) \ + --build-property upload.maximum_size=6291456 \ + --build-property "compiler.cpp.extra_flags=-MMD -c \"-DPOINTPERFECT_TOKEN=$(POINTPERFECT_TOKEN)\" \"-DFIRMWARE_VERSION_MAJOR=$(FIRMWARE_VERSION_MAJOR)\" \"-DFIRMWARE_VERSION_MINOR=$(FIRMWARE_VERSION_MINOR)\" \"-DENABLE_DEVELOPER=$(ENABLE_DEVELOPER)\"" \ + --export-binaries + +########## +# Upload the example +########## + +ESPTOOL_PATH=~/Arduino/hardware/espressif/esp32/tools/esptool +#UPLOAD_SPEED=460800 +UPLOAD_SPEED=921600 +BOOT_LOADER_PATH=~/SparkFun/SparkFun_RTK_Firmware_Uploader/RTK_Firmware_Uploader/resource + +.PHONY: upload + +upload: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin + python3 $(ESPTOOL_PATH)/esptool.py \ + --chip esp32 \ + --port $(TERMINAL_PORT) \ + --baud $(UPLOAD_SPEED) \ + --before default_reset \ + --after hard_reset \ + write_flash \ + --flash_mode dio \ + --flash_freq 80m \ + --flash_size detect \ + --compress \ + 0x1000 $(BOOT_LOADER_PATH)/RTK_Surveyor.ino.bootloader.bin \ + 0x8000 $(BOOT_LOADER_PATH)/RTK_Surveyor_Partitions_16MB.bin \ + 0xe000 $(BOOT_LOADER_PATH)/boot_app0.bin \ + 0x10000 $< + $(TERMINAL_APP) -D $(TERMINAL_PORT) $(TERMINAL_PARAMS) + +########## +# Terminal +########## + +.PHONY: terminal + +terminal: + $(TERMINAL_APP) -D $(TERMINAL_PORT) $(TERMINAL_PARAMS) + +########## +# Clean up the example +########## + +clean: + $(DELETE) build diff --git a/Example_Sketches/4_10_WiFi_AP_Server/secrets.h b/Example_Sketches/4_10_WiFi_AP_Server/secrets.h new file mode 100644 index 0000000..c2f2a06 --- /dev/null +++ b/Example_Sketches/4_10_WiFi_AP_Server/secrets.h @@ -0,0 +1,5 @@ +const char * apSSID = "SoftAP"; +const char * apPassword = "Password"; + +const char * staSSID = "Your_SSID_here"; +const char * staPassword = "Your_Password_here";