forked from Spritetm/libesphttpd
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor cgiwifi API to use modern AJAX style (was template). Also cl…
…eanup some other cgi.
- Loading branch information
1 parent
7ff76ae
commit 787799e
Showing
12 changed files
with
872 additions
and
732 deletions.
There are no files selected for viewing
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
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,157 @@ | ||
# Libesphttpd WiFi-API | ||
|
||
Functions to configure ESP32 WiFi settings via HTTP API. | ||
|
||
## GUI | ||
See the example js/html code for the GUI here: https://github.com/chmorgan/esphttpd-freertos/blob/master/html/wifi/index.html | ||
|
||
## Functions defined in libesphttpd/cgiwifi.h | ||
|
||
* __cgiWiFiScan()__ | ||
|
||
Gets the results of an earler scan in JSON format. Optionally start a new scan. | ||
|
||
Examples: | ||
* `http://my-esp32-ip/wifi/scan?clear=1&start=1` - Clear the previous results and start a new scan. Returned APs list will be empty and inProgress will be true. | ||
|
||
Note: If client is connected via WiFi, then start=1 may interrupt communication breifly, so use sparingly. | ||
* `http://my-esp32-ip/wifi/scan` - After sending start command, poll this until `inProgress:false` and APs list contains results. | ||
|
||
Note: "enc" value is from `enum wifi_auth_mode_t`, where 0=Open, 1=WEP, 2+ is WPA. | ||
|
||
GET/POST args: | ||
```js | ||
"clear": number // 1: Clear the previous results first. | ||
"start": number // 1: Start a new scan now. | ||
``` | ||
Response: | ||
```js | ||
{ | ||
"args": { // Args are repeated here in the response | ||
"clear": number, | ||
"start": number, | ||
}, | ||
"APs": [{ | ||
"essid": string, // Name of AP discovered | ||
"bssid": string, // MAC of AP discoverd | ||
"rssi": number, // Signal strength i.e. -55 | ||
"enc": number, // WiFi security (encryption) type. | ||
"channel": number // Channel used by AP | ||
},{...}], | ||
"working": boolean, // A scan is in progress. Poll this. | ||
"success": boolean, // CGI success/fail | ||
"error": string, // Optional error message if failure | ||
} | ||
``` | ||
|
||
* __cgiWiFiConnect()__ | ||
|
||
Set WiFi STAtion (ESP WiFI Client) settings and trigger a connection. | ||
|
||
Note: The "success" response of this CGI call does not indicate if the WiFi connection succeeds. Poll /wifi/sta (cgiWiFiConnStatus) for connection pending/success/fail. | ||
|
||
Examples: | ||
* http://my-esp32-ip/wifi/connect?ssid=my-ssid&pass=mysecretpasswd - Trigger a connection attempt to the AP with the given SSID and password. | ||
|
||
GET/POST args: | ||
```js | ||
"ssid": string | ||
"pass": string | ||
``` | ||
Response: | ||
```js | ||
{ | ||
"args": { // Args are repeated here in the response | ||
"ssid": string, | ||
"pass": string, | ||
}, | ||
"success": boolean, // CGI success/fail | ||
"error": string, // Optional error message if failure | ||
} | ||
``` | ||
|
||
* __cgiWiFiSetMode()__ | ||
|
||
CGI used to get/set the WiFi mode. | ||
|
||
The mode values are defined by `enum wifi_mode_t` | ||
```c | ||
0 /**< null mode */ | ||
1 /**< WiFi station mode */ | ||
2 /**< WiFi soft-AP mode */ | ||
3 /**< WiFi station + soft-AP mode */ | ||
``` | ||
|
||
Examples | ||
* i.e. http://ip/wifi/mode?mode=1 - Change mode to WIFI_MODE_STA | ||
|
||
GET/POST args: | ||
```js | ||
"mode": number // The desired Mode (as number specified in enum wifi_mode_t) | ||
"force": number // 1: Force the change, regardless of whether ESP's STA is connected to an AP. | ||
``` | ||
Response: | ||
```js | ||
{ | ||
"args": { // Args are repeated here in the response | ||
"mode": number, | ||
"force": number, | ||
}, | ||
"mode": number, // The current Mode (as number specified in enum wifi_mode_t) | ||
"mode_str": string, // The current Mode (as a string specified in wifi_mode_names[]= "Disabled","STA","AP""AP+STA") | ||
"success": boolean, // CGI success/fail | ||
"error": string, // Optional error message if failure | ||
} | ||
``` | ||
|
||
* __cgiWiFiStartWps()__ | ||
|
||
CGI for triggering a WPS push button connection attempt. | ||
|
||
* __cgiWiFiAPSettings()__ | ||
|
||
CGI for get/set settings in AP mode. | ||
|
||
Examples: | ||
* http://ip/wifi/ap?ssid=myssid&pass=mypass&chan=1 - Change AP settings | ||
|
||
GET/POST args: | ||
```js | ||
"chan": number, | ||
"ssid": string, | ||
"pass": string | ||
``` | ||
Response: | ||
```js | ||
{ | ||
"args": { // Args are repeated here in the response | ||
"chan": number, | ||
"ssid": string, | ||
"pass": string, | ||
}, | ||
"enabled" : boolean, // AP is enabled | ||
"success": boolean, // CGI success/fail | ||
"error": string, // Optional error message if failure | ||
} | ||
``` | ||
|
||
* __cgiWiFiConnStatus()__ | ||
|
||
CGI returning the current state of the WiFi STA connection to an AP. | ||
|
||
Examples: | ||
* `http://my-esp32-ip/wifi/sta` - Get the state of the STAtion | ||
|
||
Response: | ||
```js | ||
{ | ||
"ssid": string, // SSID that the STAtion should connect to. | ||
"pass": string, // WiFi network password. | ||
"enabled" : boolean, // STA is enabled | ||
"ip" : string, // Optional IP address of STAtion (only if connected) | ||
"working": boolean, // A connect is in progress. Poll this. | ||
"connected": boolean, // STAtion is connected to a WiFi network. Poll this. | ||
"success": boolean, // CGI success/fail | ||
"error": string, // Optional error message if failure | ||
} | ||
``` |
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
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,43 @@ | ||
/* Functions commonly used in cgi handlers for libesphttpd */ | ||
|
||
#ifndef CGI_COMMON_H | ||
#define CGI_COMMON_H | ||
|
||
#include "libesphttpd/httpd.h" | ||
#include "cJSON.h" | ||
|
||
// Parses *allArgs (i.e. connData->getArgs or connData->post.buff) for a signed integer by name of *argName and returns int value at *pvalue. | ||
bool cgiGetArgDecS32(const char *allArgs, const char *argName, int *pvalue, char *buff, int buffLen); | ||
// Parses *allArgs (i.e. connData->getArgs or connData->post.buff) for a unsigned int (i.e. ?uintval=123) | ||
bool cgiGetArgDecU32(const char *allArgs, const char *argName, uint32_t *pvalue, char *buff, int buffLen); | ||
// Parses *allArgs (i.e. connData->getArgs or connData->post.buff) for a uint32_t from a hexadecimal string (i.e. ?hexval=0123ABCD ) | ||
bool cgiGetArgHexU32(const char *allArgs, const char *argName, uint32_t *pvalue, char *buff, int buffLen); | ||
// Parses *allArgs (i.e. connData->getArgs or connData->post.buff) for a string value. (just a wrapper for httpdFindArg()) | ||
bool cgiGetArgString(const char *allArgs, const char *argName, char *buff, int buffLen); | ||
|
||
|
||
void cgiJsonResponseHeaders(HttpdConnData *connData); | ||
void cgiJavascriptResponseHeaders(HttpdConnData *connData); | ||
|
||
/** | ||
* Example usage of cgiJsonResponseCommonMulti for multipart json response (i.e. larger than 1kB) | ||
* | ||
CgiStatus cgiFn(HttpdConnData *connData) | ||
{ | ||
cJSON *jsroot = NULL; | ||
if (connData->cgiData == NULL) | ||
{ | ||
//First call to this cgi. | ||
jsroot = cJSON_CreateObject(); | ||
... | ||
cgiJsonResponseHeaders(connData); | ||
} | ||
return cgiJsonResponseCommonMulti(connData, &connData->cgiData, jsroot); // Send the json response! | ||
} | ||
*/ | ||
CgiStatus cgiJsonResponseCommonMulti(HttpdConnData *connData, void **statepp, cJSON *jsroot); | ||
CgiStatus cgiJsonResponseCommonSingle(HttpdConnData *connData, cJSON *jsroot); | ||
|
||
CgiStatus cgiJavascriptResponseCommon(HttpdConnData *connData, cJSON *jsroot, const char *jsObjName); | ||
|
||
#endif //CGI_COMMON_H |
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
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
Oops, something went wrong.