Skip to content

Add new slave_wifi_rest_api example: REST Wi-Fi control based on original slave (IDFGH-16209) #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(pppos_slave)
105 changes: 105 additions & 0 deletions components/eppp_link/examples/slave_wifi_rest_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# ESP32 [eppp\_link/slave](../slave/) based Example – Modifications and REST Wi-Fi Control

[See the original example here](../slave/)

This document describes the key modifications and usage instructions for the enhanced [slave](../slave/) example, enabling REST-based Wi-Fi configuration and monitoring.

---

## Overview

This version extends the original [slave](../slave/) PPP example with a full-featured REST API for Wi-Fi management. All new functionality is backward compatible; Wi-Fi can still be set via Kconfig, but runtime control is now possible from your browser or scripts.

---

## Key Changes

### 1. REST API for Wi-Fi

* Added HTTP server (`esp_http_server`) and JSON parsing (`cJSON`).
* REST endpoints:

* `GET /wifi/status` — current status (connected/disconnected, SSID, RSSI).
* `GET /wifi/scan` — scan available Wi-Fi networks.
* `POST /wifi/connect` — connect to network (JSON: `{ "ssid": ..., "password": ... }`).
* `POST /wifi/disconnect` — disconnect from Wi-Fi.
* `OPTIONS /*` — CORS preflight for all URIs.

### 2. Dynamic Wi-Fi Reconfiguration

* Supports runtime change of Wi-Fi credentials without reboot or reflash.
* Connect/disconnect sequence is carefully synchronized using event groups for stability.
* If no default SSID is set, the device waits for configuration via REST.

### 3. Event Management Improvements

* Uses EventGroup bits to distinguish connection, failure, and disconnection events.
* Manual reconnect flag to suppress auto-retry logic during REST-initiated reconnects.

### 4. CORS Support

* All REST responses include `Access-Control-Allow-Origin: *` for easy browser access.

---

## How to Use

### Build & Flash

* Add `esp_http_server` and `cJSON` to your project.
* Build and flash as usual with ESP-IDF.

### Default Startup

* If Wi-Fi SSID and password are set in `sdkconfig`, ESP32 tries to connect at startup.
* If no SSID set, device waits for Wi-Fi config from REST client.

### REST API Usage

Interact from browser, curl, or your own UI:

* **Get status:**

```bash
curl http://<esp32_ip>/wifi/status
```
* **Scan networks:**

```bash
curl http://<esp32_ip>/wifi/scan
```
* **Connect:**

```bash
curl -X POST http://<esp32_ip>/wifi/connect -H "Content-Type: application/json" -d '{"ssid":"YOUR_SSID","password":"YOUR_PASS"}'
```
* **Disconnect:**

```bash
curl -X POST http://<esp32_ip>/wifi/disconnect
```

> See [Web panel](webPanel/) for a ready-to-use web UI!

---

## Implementation Notes

* All Wi-Fi connect/disconnect operations are handled asynchronously and robustly.
* If REST connect/disconnect is called in the middle of another operation, API will wait for proper sequence.
* All actions are CORS-enabled for direct use from web apps.
* Wi-Fi scan returns SSID, RSSI, and auth mode for each visible AP (max 16).

---

## Example: Integrating with Your UI

You can use the provided web UI ([See documentation for the web panel](webPanel/README.md)
) or integrate these endpoints into any admin dashboard, mobile app, or automation script.

---

## Credits

* Enhanced by \glarionenko, \2025
* Based on original code © 2023-2025 Espressif Systems (Shanghai) CO LTD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if(CONFIG_EXAMPLE_WIFI_OVER_EPPP_CHANNEL)
set(wifi_over_channels channel_wifi_station.c)
endif()
Comment on lines +1 to +3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest removing this option in this example (just to simplify it and focus on what's important)

  • that would also remove the Kconfig option and the source channel_wifi_station.c


idf_component_register(
SRCS eppp_slave.c
${wifi_over_channels}
INCLUDE_DIRS "."
REQUIRES json esp_wifi nvs_flash esp_http_server
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
menu "Example Configuration"

config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.

config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.

config ESP_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.

config EXAMPLE_SPI_HOST
int "SPI Host"
depends on EPPP_LINK_DEVICE_SPI
default 1
range 0 2
help
SPI host to use (SPI1_HOST=0, SPI2_HOST=1, SPI3_HOST=2).

config EXAMPLE_SPI_MOSI_PIN
int "MOSI Pin Number"
depends on EPPP_LINK_DEVICE_SPI
default 23
range 0 39
help
Pin number of SPI MOSI.

config EXAMPLE_SPI_MISO_PIN
int "MISO Pin Number"
depends on EPPP_LINK_DEVICE_SPI
default 19
range 0 39
help
Pin number of SPI MISO.

config EXAMPLE_SPI_SCLK_PIN
int "SCLK Pin Number"
depends on EPPP_LINK_DEVICE_SPI
default 18
range 0 39
help
Pin number of SPI SCLK.

config EXAMPLE_SPI_CS_PIN
int "CS Pin Number"
depends on EPPP_LINK_DEVICE_SPI
default 5
range 0 39
help
Pin number of SPI CS.

config EXAMPLE_SPI_INTR_PIN
int "Interrupt Pin Number"
depends on EPPP_LINK_DEVICE_SPI
default 17
range 0 39
help
Pin number of SPI interrupt.

config EXAMPLE_SPI_FREQUENCY
int "SPI Frequency (Hz)"
depends on EPPP_LINK_DEVICE_SPI
default 1000000
range 100000 80000000
help
SPI frequency in Hz.

config EXAMPLE_UART_TX_PIN
int "TXD Pin Number"
depends on EPPP_LINK_DEVICE_UART
default 18
range 0 31
help
Pin number of UART TX.

config EXAMPLE_UART_RX_PIN
int "RXD Pin Number"
depends on EPPP_LINK_DEVICE_UART
default 17
range 0 31
help
Pin number of UART RX.

config EXAMPLE_UART_BAUDRATE
int "Baudrate"
depends on EPPP_LINK_DEVICE_UART
default 921600
range 0 4000000
help
Baudrate used by the PPP over UART

config EXAMPLE_WIFI_OVER_EPPP_CHANNEL
bool "Use WiFi over EPPP channel"
default n
depends on EPPP_LINK_CHANNELS_SUPPORT
help
Enable this option to use WiFi over EPPP channel.
If this option is enabled, the example will only start the Wi-Fi driver,
but the Wi-Fi netif will reside on client's end and will channel
the Rx and Tx data via EPPP channels.

endmenu
Loading
Loading