Skip to content

Commit 1a710da

Browse files
authored
Merge pull request #41 from tonyp7/release/v2.0
Release/v2.0
2 parents 5e4c4f0 + dc66d77 commit 1a710da

File tree

9 files changed

+678
-320
lines changed

9 files changed

+678
-320
lines changed

wifi_manager/main/Kconfig.projbuild

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
menu "Wifi Manager Configuration"
22

3+
config WIFI_MANAGER_TASK_PRIORITY
4+
int "RTOS Task Priority for the wifi_manager"
5+
default 5
6+
help
7+
Tasks spawn by the manager will have a priority of WIFI_MANAGER_TASK_PRIORITY-1. For this particular reason, minimum recommended task priority is 2.
8+
9+
config WIFI_MANAGER_MAX_RETRY
10+
int "Max Retry on failed connection"
11+
default 2
12+
help
13+
Defines when a connection is lost/attempt to connect is made, how many retries should be made before giving up.
14+
315
config DEFAULT_AP_SSID
416
string "Access Point SSID"
517
default "esp32"
@@ -20,13 +32,13 @@ config DEFAULT_AP_CHANNEL
2032

2133
config DEFAULT_AP_IP
2234
string "Access Point IP Address"
23-
default "192.168.1.1"
35+
default "10.10.0.1"
2436
help
2537
This is used for the redirection to the captive portal. It is recommended to leave unchanged.
2638

2739
config DEFAULT_AP_GATEWAY
2840
string "Access Point IP Gateway"
29-
default "192.168.1.1"
41+
default "10.10.0.1"
3042
help
3143
This is used for the redirection to the captive portal. It is recommended to leave unchanged.
3244

wifi_manager/main/dns_server.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,27 @@ Contains the freeRTOS task for the DNS server that processes the requests.
5656

5757
static const char TAG[] = "dns_server";
5858
static TaskHandle_t task_dns_server = NULL;
59+
int socket_fd;
5960

6061
void dns_server_start() {
61-
xTaskCreate(&dns_server, "dns_server", 3072, NULL, 5, &task_dns_server);
62+
xTaskCreate(&dns_server, "dns_server", 3072, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_dns_server);
63+
}
64+
65+
void dns_server_stop(){
66+
if(task_dns_server){
67+
vTaskDelete(task_dns_server);
68+
close(socket_fd);
69+
task_dns_server = NULL;
70+
}
71+
6272
}
6373

6474

6575

6676
void dns_server(void *pvParameters) {
6777

6878

69-
int socket_fd;
79+
7080
struct sockaddr_in sa, ra;
7181

7282
/* Set redirection DNS hijack to the access point IP */
@@ -108,6 +118,7 @@ void dns_server(void *pvParameters) {
108118

109119
/* Start loop to process DNS requests */
110120
for(;;) {
121+
111122
memset(data, 0x00, sizeof(data)); /* reset buffer */
112123
length = recvfrom(socket_fd, data, sizeof(data), 0, (struct sockaddr *)&client, &client_len); /* read udp request */
113124

@@ -159,10 +170,12 @@ void dns_server(void *pvParameters) {
159170
}
160171
}
161172

162-
vTaskDelay( (TickType_t)10); /* allows the freeRTOS scheduler to take over if needed. DNS daemon should not be taxing on the system */
173+
taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. DNS daemon should not be taxing on the system */
174+
163175
}
164176
close(socket_fd);
165177

178+
vTaskDelete ( NULL );
166179
}
167180

168181

wifi_manager/main/dns_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ typedef struct __attribute__((__packed__)) dns_answer_t{
125125

126126
void dns_server(void *pvParameters);
127127
void dns_server_start();
128+
void dns_server_stop();
128129

129130

130131

wifi_manager/main/http_server.c

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ function to process requests, decode URLs, serve files, etc. etc.
6060
#include "wifi_manager.h"
6161

6262

63-
EventGroupHandle_t http_server_event_group = NULL;
64-
EventBits_t uxBits;
63+
//EventGroupHandle_t http_server_event_group = NULL;
64+
//EventBits_t uxBits;
6565

6666
static const char TAG[] = "http_server";
67+
static TaskHandle_t task_http_server = NULL;
68+
6769

6870
/* embedded binary data */
6971
extern const uint8_t style_css_start[] asm("_binary_style_css_start");
@@ -90,21 +92,14 @@ const static char http_redirect_hdr_end[] = "/\n\n";
9092

9193

9294

93-
void http_server_set_event_start(){
94-
if(!http_server_event_group) http_server_event_group = xEventGroupCreate();
95-
xEventGroupSetBits(http_server_event_group, HTTP_SERVER_START_BIT_0 );
95+
void http_server_start(){
96+
if(task_http_server == NULL){
97+
xTaskCreate(&http_server, "http_server", 2048, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_http_server);
98+
}
9699
}
97100

98-
99101
void http_server(void *pvParameters) {
100102

101-
if(!http_server_event_group) http_server_event_group = xEventGroupCreate();
102-
103-
/* do not start the task until wifi_manager says it's safe to do so! */
104-
ESP_LOGD(TAG, "waiting for start bit");
105-
uxBits = xEventGroupWaitBits(http_server_event_group, HTTP_SERVER_START_BIT_0, pdFALSE, pdTRUE, portMAX_DELAY );
106-
ESP_LOGD(TAG, "received start bit, starting server");
107-
108103
struct netconn *conn, *newconn;
109104
err_t err;
110105
conn = netconn_new(NETCONN_TCP);
@@ -117,10 +112,12 @@ void http_server(void *pvParameters) {
117112
http_server_netconn_serve(newconn);
118113
netconn_delete(newconn);
119114
}
120-
vTaskDelay( (TickType_t)10); /* allows the freeRTOS scheduler to take over if needed */
115+
taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
121116
} while(err == ERR_OK);
122117
netconn_close(conn);
123118
netconn_delete(conn);
119+
120+
vTaskDelete( NULL );
124121
}
125122

126123

@@ -162,98 +159,104 @@ void http_server_netconn_serve(struct netconn *conn) {
162159

163160
if(line) {
164161

165-
166-
/* captive portal functionality: redirect to the access point IP addresss */
162+
/* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
167163
int lenH = 0;
168164
char *host = http_server_get_header(save_ptr, "Host: ", &lenH);
169-
if ((sizeof(host) > 0) && !strstr(host, DEFAULT_AP_IP)) {
165+
/* determine if Host is from the STA IP address */
166+
wifi_manager_lock_sta_ip_string(portMAX_DELAY);
167+
bool access_from_sta_ip = lenH > 0?strstr(host, wifi_manager_get_sta_ip_string()):false;
168+
wifi_manager_unlock_sta_ip_string();
169+
170+
if (lenH > 0 && !strstr(host, DEFAULT_AP_IP) && !access_from_sta_ip) {
170171
netconn_write(conn, http_redirect_hdr_start, sizeof(http_redirect_hdr_start) - 1, NETCONN_NOCOPY);
171172
netconn_write(conn, DEFAULT_AP_IP, sizeof(DEFAULT_AP_IP) - 1, NETCONN_NOCOPY);
172173
netconn_write(conn, http_redirect_hdr_end, sizeof(http_redirect_hdr_end) - 1, NETCONN_NOCOPY);
173174
}
174-
/* default page */
175-
else if(strstr(line, "GET / ")) {
176-
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
177-
netconn_write(conn, index_html_start, index_html_end - index_html_start, NETCONN_NOCOPY);
178-
}
179-
else if(strstr(line, "GET /jquery.js ")) {
180-
netconn_write(conn, http_jquery_gz_hdr, sizeof(http_jquery_gz_hdr) - 1, NETCONN_NOCOPY);
181-
netconn_write(conn, jquery_gz_start, jquery_gz_end - jquery_gz_start, NETCONN_NOCOPY);
182-
}
183-
else if(strstr(line, "GET /code.js ")) {
184-
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
185-
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
186-
}
187-
else if(strstr(line, "GET /ap.json ")) {
188-
/* if we can get the mutex, write the last version of the AP list */
189-
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
190-
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
191-
char *buff = wifi_manager_get_ap_list_json();
192-
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
193-
wifi_manager_unlock_json_buffer();
175+
else{
176+
/* default page */
177+
if(strstr(line, "GET / ")) {
178+
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
179+
netconn_write(conn, index_html_start, index_html_end - index_html_start, NETCONN_NOCOPY);
194180
}
195-
else{
196-
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
197-
ESP_LOGD(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
181+
else if(strstr(line, "GET /jquery.js ")) {
182+
netconn_write(conn, http_jquery_gz_hdr, sizeof(http_jquery_gz_hdr) - 1, NETCONN_NOCOPY);
183+
netconn_write(conn, jquery_gz_start, jquery_gz_end - jquery_gz_start, NETCONN_NOCOPY);
198184
}
199-
/* request a wifi scan */
200-
wifi_manager_scan_async();
201-
}
202-
else if(strstr(line, "GET /style.css ")) {
203-
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
204-
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
205-
}
206-
else if(strstr(line, "GET /status.json ")){
207-
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
208-
char *buff = wifi_manager_get_ip_info_json();
209-
if(buff){
185+
else if(strstr(line, "GET /code.js ")) {
186+
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
187+
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
188+
}
189+
else if(strstr(line, "GET /ap.json ")) {
190+
/* if we can get the mutex, write the last version of the AP list */
191+
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
210192
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
193+
char *buff = wifi_manager_get_ap_list_json();
211194
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
212195
wifi_manager_unlock_json_buffer();
213196
}
214197
else{
215198
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
199+
ESP_LOGD(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
216200
}
201+
/* request a wifi scan */
202+
wifi_manager_scan_async();
217203
}
218-
else{
219-
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
220-
ESP_LOGD(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
204+
else if(strstr(line, "GET /style.css ")) {
205+
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
206+
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
221207
}
222-
}
223-
else if(strstr(line, "DELETE /connect.json ")) {
224-
ESP_LOGD(TAG, "http_server_netconn_serve: DELETE /connect.json");
225-
/* request a disconnection from wifi and forget about it */
226-
wifi_manager_disconnect_async();
227-
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
228-
}
229-
else if(strstr(line, "POST /connect.json ")) {
230-
ESP_LOGD(TAG, "http_server_netconn_serve: POST /connect.json");
231-
232-
bool found = false;
233-
int lenS = 0, lenP = 0;
234-
char *ssid = NULL, *password = NULL;
235-
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
236-
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
237-
238-
if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE){
239-
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
240-
memset(config, 0x00, sizeof(wifi_config_t));
241-
memcpy(config->sta.ssid, ssid, lenS);
242-
memcpy(config->sta.password, password, lenP);
243-
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call");
244-
wifi_manager_connect_async();
245-
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
246-
found = true;
208+
else if(strstr(line, "GET /status.json ")){
209+
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
210+
char *buff = wifi_manager_get_ip_info_json();
211+
if(buff){
212+
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
213+
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
214+
wifi_manager_unlock_json_buffer();
215+
}
216+
else{
217+
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
218+
}
219+
}
220+
else{
221+
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
222+
ESP_LOGD(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
223+
}
224+
}
225+
else if(strstr(line, "DELETE /connect.json ")) {
226+
ESP_LOGD(TAG, "http_server_netconn_serve: DELETE /connect.json");
227+
/* request a disconnection from wifi and forget about it */
228+
wifi_manager_disconnect_async();
229+
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
247230
}
231+
else if(strstr(line, "POST /connect.json ")) {
232+
ESP_LOGD(TAG, "http_server_netconn_serve: POST /connect.json");
233+
234+
bool found = false;
235+
int lenS = 0, lenP = 0;
236+
char *ssid = NULL, *password = NULL;
237+
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
238+
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
239+
240+
if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE){
241+
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
242+
memset(config, 0x00, sizeof(wifi_config_t));
243+
memcpy(config->sta.ssid, ssid, lenS);
244+
memcpy(config->sta.password, password, lenP);
245+
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call");
246+
wifi_manager_connect_async();
247+
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
248+
found = true;
249+
}
250+
251+
if(!found){
252+
/* bad request the authentification header is not complete/not the correct format */
253+
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
254+
}
248255

249-
if(!found){
250-
/* bad request the authentification header is not complete/not the correct format */
256+
}
257+
else{
251258
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
252259
}
253-
254-
}
255-
else{
256-
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
257260
}
258261
}
259262
else{

wifi_manager/main/http_server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ extern "C" {
4343

4444
void http_server(void *pvParameters);
4545
void http_server_netconn_serve(struct netconn *conn);
46-
void http_server_set_event_start();
46+
//void http_server_set_event_start(); //TODO: delete
47+
void http_server_start();
4748

4849
/**
4950
* @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.

wifi_manager/main/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ <h1>About this app...</h1>
119119
</header>
120120
<h2></h2>
121121
<section>
122-
<p><strong>esp32-wifi-manager</strong>, &copy; 2017, Tony Pottier<br />Licender under the MIT License.</p>
122+
<p><strong>esp32-wifi-manager</strong>, &copy; 2017-2019, Tony Pottier<br />Licender under the MIT License.</p>
123123
<p>
124124
This app would not be possible without the following libraries:
125125
</p>

wifi_manager/main/main.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ SOFTWARE.
5252

5353

5454

55-
static TaskHandle_t task_http_server = NULL;
56-
static TaskHandle_t task_wifi_manager = NULL;
55+
5756

5857
/* @brief tag used for ESP serial console messages */
5958
static const char TAG[] = "main";
@@ -65,8 +64,8 @@ static const char TAG[] = "main";
6564
void monitoring_task(void *pvParameter)
6665
{
6766
for(;;){
68-
ESP_LOGD(TAG, "free heap: %d",esp_get_free_heap_size());
69-
vTaskDelay(10000 / portTICK_PERIOD_MS);
67+
ESP_LOGI(TAG, "free heap: %d",esp_get_free_heap_size());
68+
vTaskDelay( pdMS_TO_TICKS(10000) );
7069
}
7170
}
7271

@@ -75,17 +74,10 @@ void app_main()
7574
{
7675

7776

78-
/* disable the default wifi logging */
79-
esp_log_level_set("wifi", ESP_LOG_NONE);
80-
81-
/* initialize flash memory */
82-
nvs_flash_init();
8377

84-
/* start the HTTP Server task */
85-
xTaskCreate(&http_server, "http_server", 2048, NULL, 5, &task_http_server);
8678

87-
/* start the wifi manager task */
88-
xTaskCreate(&wifi_manager, "wifi_manager", 4096, NULL, 4, &task_wifi_manager);
79+
/* start the wifi manager */
80+
wifi_manager_start();
8981

9082
/* your code should go here. Here we simply create a task on core 2 that monitors free heap memory */
9183
xTaskCreatePinnedToCore(&monitoring_task, "monitoring_task", 2048, NULL, 1, NULL, 1);

0 commit comments

Comments
 (0)