From 13bdd662959768ea6d4c0ae9402bcf97e50bd190 Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Fri, 30 Oct 2020 04:05:03 +0000 Subject: [PATCH] Initial HTTP server associated with the WiFi AP --- main/ap.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- sdkconfig | 2 +- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/main/ap.cpp b/main/ap.cpp index d559389..759e2ce 100644 --- a/main/ap.cpp +++ b/main/ap.cpp @@ -5,6 +5,7 @@ #include "tcpip_adapter.h" #include "esp_wifi.h" #include "esp_event_loop.h" +#include "esp_http_server.h" #include "format.h" #include "fifo.h" @@ -16,6 +17,47 @@ #ifdef WITH_AP +// ============================================================================================================ + +static void RespChunk(httpd_req_t *Req, const char *Resp, int Len) { httpd_resp_send_chunk(Req, Resp, Len); } +static void RespChunk(httpd_req_t *Req, const char *Resp) { httpd_resp_send_chunk(Req, Resp, strlen(Resp)); } +static void RespEnd(httpd_req_t *Req) { httpd_resp_send_chunk(Req, 0, 0); }; + +static esp_err_t status_get_handler(httpd_req_t *Req) +{ char Line[64]; + RespChunk(Req, "\ +\r\n\ +\r\n\ +OGN-Tracker status\r\n\ +"); + int Len=Parameters.Print(Line); + RespChunk(Req, Line, Len); + RespChunk(Req, "\r\n"); + RespEnd(Req); + return ESP_OK; } + +static const httpd_uri_t HTTPstatus = +{ .uri = "/status.html", + .method = HTTP_GET, + .handler = status_get_handler, + .user_ctx = 0 }; + +static httpd_handle_t HTTPserver = 0; + +static esp_err_t HTTP_Start(int MaxSockets=4, int Port=80) +{ httpd_config_t Config = HTTPD_DEFAULT_CONFIG(); + Config.server_port = Port; + Config.task_priority = tskIDLE_PRIORITY+3; + Config.max_open_sockets = MaxSockets; + esp_err_t Err=httpd_start(&HTTPserver, &Config); if(Err!=ESP_OK) return Err; + httpd_register_uri_handler(HTTPserver, &HTTPstatus); + return Err; } + +static void HTTP_Stop(void) +{ if(HTTPserver) httpd_stop(HTTPserver); HTTPserver=0; } + +// ============================================================================================================ + DataServer PortServer; static FIFO AP_TxFIFO; @@ -32,6 +74,8 @@ static int AP_TxPush(size_t MaxLen=256) // transmit pa AP_TxFIFO.flushReadBlock(Len); // remove the transmitted block from the FIFO return Len; } // return number of transmitted bytes +// ============================================================================================================ + extern "C" void vTaskAP(void* pvParameters) { esp_err_t Err; @@ -63,7 +107,15 @@ void vTaskAP(void* pvParameters) xSemaphoreGive(CONS_Mutex); #endif - vTaskDelay(1000); + Err=HTTP_Start(); +#ifdef DEBUG_PRINT + xSemaphoreTake(CONS_Mutex, portMAX_DELAY); + Format_String(CONS_UART_Write, "HTTP_Start() => "); + Format_SignDec(CONS_UART_Write, Err); + Format_String(CONS_UART_Write, "\n"); + xSemaphoreGive(CONS_Mutex); +#endif + // vTaskDelay(1000); for( ; ; ) // main (endless) loop { Err=AP_TxPush(); diff --git a/sdkconfig b/sdkconfig index a482a53..add3812 100644 --- a/sdkconfig +++ b/sdkconfig @@ -555,7 +555,7 @@ CONFIG_LWIP_LOCAL_HOSTNAME="espressif" # CONFIG_LWIP_L2_TO_L3_COPY is not set # CONFIG_LWIP_IRAM_OPTIMIZATION is not set CONFIG_LWIP_TIMERS_ONDEMAND=y -CONFIG_LWIP_MAX_SOCKETS=8 +CONFIG_LWIP_MAX_SOCKETS=10 # CONFIG_LWIP_USE_ONLY_LWIP_SELECT is not set # CONFIG_LWIP_SO_REUSE is not set # CONFIG_LWIP_SO_RCVBUF is not set