example of HTTP server in AP mode?

marchingband
Posts: 1
Joined: Wed Mar 13, 2019 9:20 pm

example of HTTP server in AP mode?

Postby marchingband » Wed Mar 13, 2019 9:28 pm

Can anyone direct me to a working example of a simple HTTP server, which uses AP mode?
It seems like a powerful feature of ESP32 but I havn't been able to locate a working example yet.
I would like to be able to connect to the ESP32 network on another device, open a web browser, navigate to some address, and have ESP32 serve a webpage from its flash memory.

karlo.verde.13
Posts: 6
Joined: Wed Mar 13, 2019 9:49 pm
Location: Lima, Peru
Contact:

Re: example of HTTP server in AP mode?

Postby karlo.verde.13 » Fri Mar 15, 2019 9:50 pm

Hey there!

This link shows a tutorial about what you are looking for: http://www.lucadentella.it/en/2018/01/2 ... ta-softap/. Nevertheless, this example uses the Netconn API.

If you want to use the esp_http_server.h component, there is no example implemented with SoftAp, yet.

bluesensing
Posts: 3
Joined: Thu Dec 28, 2017 4:28 pm

Re: example of HTTP server in AP mode?

Postby bluesensing » Sun Mar 17, 2019 9:06 am

i am also very keen on such an working example. reading html from a sdcard

regards from berlin
gerhard

karlo.verde.13
Posts: 6
Joined: Wed Mar 13, 2019 9:49 pm
Location: Lima, Peru
Contact:

Re: example of HTTP server in AP mode?

Postby karlo.verde.13 » Mon Mar 18, 2019 10:37 am

Hey there!

Since there is no example of what you are looking for, I decided to create one.

NOTES
  • Some header files are unnecessary.
  • This example also includes IP configuration.
  • The URI handler has minimal code in this example but you can define it as you wish.
  • If you have an Android device, I recommend that you use this app: https://play.google.com/store/apps/deta ... tprequests.
  1. #include "string.h"
  2.  
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/event_groups.h"
  6. #include "esp_wifi.h"
  7. #include "esp_system.h"
  8. #include "esp_event.h"
  9. #include "esp_event_loop.h"
  10. #include "esp_log.h"
  11. #include "nvs_flash.h"
  12. #include "esp_http_server.h"
  13.  
  14. #define SOFT_AP_SSID "ESP32 SoftAP"
  15. #define SOFT_AP_PASSWORD "Password"
  16.  
  17. #define SOFT_AP_IP_ADDRESS_1 192
  18. #define SOFT_AP_IP_ADDRESS_2 168
  19. #define SOFT_AP_IP_ADDRESS_3 5
  20. #define SOFT_AP_IP_ADDRESS_4 18
  21.  
  22. #define SOFT_AP_GW_ADDRESS_1 192
  23. #define SOFT_AP_GW_ADDRESS_2 168
  24. #define SOFT_AP_GW_ADDRESS_3 5
  25. #define SOFT_AP_GW_ADDRESS_4 20
  26.  
  27. #define SOFT_AP_NM_ADDRESS_1 255
  28. #define SOFT_AP_NM_ADDRESS_2 255
  29. #define SOFT_AP_NM_ADDRESS_3 255
  30. #define SOFT_AP_NM_ADDRESS_4 0
  31.  
  32. #define SERVER_PORT 3500
  33. #define HTTP_METHOD HTTP_POST
  34. #define URI_STRING "/test"
  35.  
  36. static httpd_handle_t httpServerInstance = NULL;
  37.  
  38. static esp_err_t methodHandler(httpd_req_t* httpRequest){
  39.     ESP_LOGI("HANDLER","This is the handler for the <%s> URI", httpRequest->uri);
  40.     return ESP_OK;
  41. }
  42.  
  43. static httpd_uri_t testUri = {
  44.     .uri       = URI_STRING,
  45.     .method    = HTTP_METHOD,
  46.     .handler   = methodHandler,
  47.     .user_ctx  = NULL,
  48. };
  49.  
  50. static void startHttpServer(void){
  51.     httpd_config_t httpServerConfiguration = HTTPD_DEFAULT_CONFIG();
  52.     httpServerConfiguration.server_port = SERVER_PORT;
  53.     if(httpd_start(&httpServerInstance, &httpServerConfiguration) == ESP_OK){
  54.         ESP_ERROR_CHECK(httpd_register_uri_handler(httpServerInstance, &testUri));
  55.     }
  56. }
  57.  
  58. static void stopHttpServer(void){
  59.     if(httpServerInstance != NULL){
  60.         ESP_ERROR_CHECK(httpd_stop(httpServerInstance));
  61.     }
  62. }
  63.  
  64. static esp_err_t wifiEventHandler(void* userParameter, system_event_t *event) {
  65.     switch(event->event_id){
  66.     case SYSTEM_EVENT_AP_STACONNECTED:
  67.         startHttpServer();
  68.         break;
  69.     case SYSTEM_EVENT_AP_STADISCONNECTED:
  70.         stopHttpServer();
  71.         break;
  72.     default:
  73.         break;
  74.     }
  75.     return ESP_OK;
  76. }
  77.  
  78. static void launchSoftAp(){
  79.     ESP_ERROR_CHECK(nvs_flash_init());
  80.     tcpip_adapter_init();
  81.     ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  82.     tcpip_adapter_ip_info_t ipAddressInfo;
  83.     memset(&ipAddressInfo, 0, sizeof(ipAddressInfo));
  84.     IP4_ADDR(
  85.         &ipAddressInfo.ip,
  86.         SOFT_AP_IP_ADDRESS_1,
  87.         SOFT_AP_IP_ADDRESS_2,
  88.         SOFT_AP_IP_ADDRESS_3,
  89.         SOFT_AP_IP_ADDRESS_4);
  90.     IP4_ADDR(
  91.         &ipAddressInfo.gw,
  92.         SOFT_AP_GW_ADDRESS_1,
  93.         SOFT_AP_GW_ADDRESS_2,
  94.         SOFT_AP_GW_ADDRESS_3,
  95.         SOFT_AP_GW_ADDRESS_4);
  96.     IP4_ADDR(
  97.         &ipAddressInfo.netmask,
  98.         SOFT_AP_NM_ADDRESS_1,
  99.         SOFT_AP_NM_ADDRESS_2,
  100.         SOFT_AP_NM_ADDRESS_3,
  101.         SOFT_AP_NM_ADDRESS_4);
  102.     ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ipAddressInfo));
  103.     ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
  104.     ESP_ERROR_CHECK(esp_event_loop_init(wifiEventHandler, NULL));
  105.     wifi_init_config_t wifiConfiguration = WIFI_INIT_CONFIG_DEFAULT();
  106.     ESP_ERROR_CHECK(esp_wifi_init(&wifiConfiguration));
  107.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  108.     wifi_config_t apConfiguration = {
  109.         .ap = {
  110.             .ssid = SOFT_AP_SSID,
  111.             .password = SOFT_AP_PASSWORD,
  112.             .ssid_len = 0,
  113.             //.channel = default,
  114.             .authmode = WIFI_AUTH_WPA2_PSK,
  115.             .ssid_hidden = 0,
  116.             .max_connection = 1,
  117.             .beacon_interval = 150,
  118.         },
  119.     };
  120.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &apConfiguration));
  121.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  122.     ESP_ERROR_CHECK(esp_wifi_start());
  123. }
  124.  
  125. void app_main(void){
  126.     launchSoftAp();
  127.     while(1) vTaskDelay(10);
  128. }

Hope it helps
Best regards
Verde :mrgreen:

Return to “ESP32 IDF”

Who is online

Users browsing this forum: No registered users and 14 guests