ESP32 SoftAp static ip config

louiey
Posts: 5
Joined: Sat Sep 12, 2020 4:55 am

ESP32 SoftAp static ip config

Postby louiey » Sat Sep 12, 2020 5:23 am

Hello.

I'm trying esp32 softAP example code for my reference.

1. Run softAP
2. I could connect from my PC via WiFi
3. I could see leg message from terminal and I could confirm I connected at softAP
4. starts tcp_server_task and waiting to listen from socket
4. I want to send "Hello" message from PC to ESP and want to get return message from ESP for echo message
5. I tried to connect ESP 32 via tcp_client tool and couldn't make it work

Could you help how to config static IP address of ESP and what I'm wrong with below code to make it work?

Many thanks.
  1. /*  WiFi softAP Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <string.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_system.h"
  13. #include "esp_wifi.h"
  14. #include "esp_event.h"
  15. #include "esp_log.h"
  16. #include "nvs_flash.h"
  17.  
  18. #include "lwip/err.h"
  19. #include "lwip/sys.h"
  20.  
  21. #include "esp_netif.h"
  22. #include "lwip/sockets.h"
  23. #include <lwip/netdb.h>
  24.  
  25. /* The examples use WiFi configuration that you can set via project configuration menu.
  26.  
  27.    If you'd rather not, just change the below entries to strings with
  28.    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  29. */
  30. #define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
  31. #define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
  32. #define EXAMPLE_ESP_WIFI_CHANNEL   CONFIG_ESP_WIFI_CHANNEL
  33. #define EXAMPLE_MAX_STA_CONN       CONFIG_ESP_MAX_STA_CONN
  34.  
  35. #define CONFIG_EXAMPLE_IPV4
  36. #define PORT 22
  37.  
  38. #define MY_IP_ADDR  "192.168.4.56"
  39.  
  40.  
  41. #define TCPIP_SERVER
  42.  
  43. static const char *TAG = "wifi softAP";
  44.  
  45. static void wifi_event_handler(void* arg, esp_event_base_t event_base,
  46.                                     int32_t event_id, void* event_data)
  47. {
  48.     if (event_id == WIFI_EVENT_AP_STACONNECTED) {
  49.         wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
  50.         ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
  51.                  MAC2STR(event->mac), event->aid);
  52.     } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
  53.         wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
  54.         ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
  55.                  MAC2STR(event->mac), event->aid);
  56.     }
  57. }
  58.  
  59. void wifi_init_softap(void)
  60. {
  61.     ESP_ERROR_CHECK(esp_netif_init());
  62.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  63.     esp_netif_create_default_wifi_ap();
  64.  
  65.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  66.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  67.  
  68.     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
  69.  
  70.     wifi_config_t wifi_config = {
  71.         .ap = {
  72.             .ssid = EXAMPLE_ESP_WIFI_SSID,
  73.             .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
  74.             .channel = EXAMPLE_ESP_WIFI_CHANNEL,
  75.             .password = EXAMPLE_ESP_WIFI_PASS,
  76.             .max_connection = EXAMPLE_MAX_STA_CONN,
  77.             .authmode = WIFI_AUTH_WPA_WPA2_PSK
  78.         },
  79.     };
  80.     if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
  81.         wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  82.     }
  83.  
  84.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  85.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
  86.     ESP_ERROR_CHECK(esp_wifi_start());
  87.  
  88.     ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
  89.              EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
  90. }
  91.  
  92. #ifdef TCPIP_SERVER
  93. static void do_retransmit(const int sock)
  94. {
  95.     int len;
  96.     char rx_buffer[128];
  97.  
  98.     do {
  99.         len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  100.         if (len < 0) {
  101.             ESP_LOGE(TAG, "Error occurred during receiving: errno %d", errno);
  102.         } else if (len == 0) {
  103.             ESP_LOGW(TAG, "Connection closed");
  104.         } else {
  105.             rx_buffer[len] = 0; // Null-terminate whatever is received and treat it like a string
  106.             ESP_LOGI(TAG, "Received %d bytes: %s", len, rx_buffer);
  107.  
  108.             // send() can return less bytes than supplied length.
  109.             // Walk-around for robust implementation.
  110.             int to_write = len;
  111.             while (to_write > 0) {
  112.                 int written = send(sock, rx_buffer + (len - to_write), to_write, 0);
  113.                 if (written < 0) {
  114.                     ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  115.                 }
  116.                 to_write -= written;
  117.             }
  118.         }
  119.     } while (len > 0);
  120. }
  121.  
  122. static void tcp_server_task(void *pvParameters)
  123. {
  124.     char addr_str[128];
  125.     int addr_family;
  126.     int ip_protocol;
  127.  
  128. #ifdef CONFIG_EXAMPLE_IPV4
  129.     struct sockaddr_in dest_addr, client_addr;
  130.     //dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  131.     dest_addr.sin_addr.s_addr = htonl(MY_IP_ADDR);
  132.     dest_addr.sin_family = AF_INET;
  133.     dest_addr.sin_port = htons(PORT);
  134.     addr_family = AF_INET;
  135.     ip_protocol = IPPROTO_IP;
  136.     inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
  137.     ESP_LOGI(TAG, "My IP : %s", addr_str);
  138. #else // IPV6
  139.     struct sockaddr_in6 dest_addr;
  140.     bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
  141.     dest_addr.sin6_family = AF_INET6;
  142.     dest_addr.sin6_port = htons(PORT);
  143.     addr_family = AF_INET6;
  144.     ip_protocol = IPPROTO_IPV6;
  145.     inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  146. #endif
  147.  
  148.     int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  149.     if (listen_sock < 0) {
  150.         ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  151.         vTaskDelete(NULL);
  152.         return;
  153.     }
  154.     ESP_LOGI(TAG, "Socket created");
  155.  
  156.     int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  157.     if (err != 0) {
  158.         ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  159.         goto CLEAN_UP;
  160.     }
  161.     ESP_LOGI(TAG, "Socket bound, port %d", PORT);
  162.  
  163.     err = listen(listen_sock, 1);
  164.     if (err != 0) {
  165.         ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
  166.         goto CLEAN_UP;
  167.     }
  168.  
  169.     while (1) {
  170.  
  171.         ESP_LOGI(TAG, "Socket listening");
  172. #if 0
  173.         struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
  174.         uint addr_len = sizeof(source_addr);
  175.         int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
  176.         if (sock < 0) {
  177.             ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
  178.             break;
  179.         }
  180.  
  181.         // Convert ip address to string
  182.         if (source_addr.sin6_family == PF_INET) {
  183.             inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  184.         } else if (source_addr.sin6_family == PF_INET6) {
  185.             inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  186.         }
  187.         ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
  188. #else
  189.         uint addr_len = sizeof(client_addr);
  190.         int sock = accept(listen_sock, (struct sockaddr *)&client_addr, &addr_len);
  191.         if (sock < 0) {
  192.             ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
  193.             break;
  194.         }
  195.         inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, addr_str, sizeof(addr_str));
  196.         ESP_LOGI(TAG, "Server : %s client connected.\n", addr_str);
  197. #endif
  198.         do_retransmit(sock);
  199.  
  200.         shutdown(sock, 0);
  201.         close(sock);
  202.     }
  203.  
  204. CLEAN_UP:
  205.     close(listen_sock);
  206.     vTaskDelete(NULL);
  207. }
  208. #endif
  209.  
  210. void app_main(void)
  211. {
  212.     //Initialize NVS
  213.     esp_err_t ret = nvs_flash_init();
  214.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  215.       ESP_ERROR_CHECK(nvs_flash_erase());
  216.       ret = nvs_flash_init();
  217.     }
  218.     ESP_ERROR_CHECK(ret);
  219.  
  220.     ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
  221.     wifi_init_softap();
  222. #ifdef TCPIP_SERVER
  223.     xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL);
  224. #endif
  225. }
  226.  

ESP_Sprite
Posts: 8999
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 SoftAp static ip config

Postby ESP_Sprite » Sun Sep 13, 2020 8:22 am

Hard to say... can you expand on 'could not make it work'? Can you ping the ESP, can you at least connect to the socket?

louiey
Posts: 5
Joined: Sat Sep 12, 2020 4:55 am

Re: ESP32 SoftAp static ip config

Postby louiey » Mon Sep 14, 2020 1:27 am

1. I can connect on esp32_ap wifi from my PC
- I can see log messages which prints my IP from terminal
2. esp32 waiting for accept
3. I set esp32 IP address to 192.168.4.56
4. Connect to esp32 with "192.168.4.56" port 22 from PC
- cannot connect to esp32 from PC
- if I try ping, there is no response
5. According to log from esp32, it says its IP is 63.64.51.212
- I tried to connect this IP from PC and couldn't connected, I just saw "connecting" message from PC
- if I try ping, there is no response

Actually I connected at esp32_ap wifi from PC and couldn't connect using tcp/ip.
IP address which I expected seems to be not working or I missed some parameters to use static ip.

Below is log message from esp32
++++++++++
I (505) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (592) wifi softAP: ESP_WIFI_MODE_AP
I (602) wifi:wifi driver task: 3ffc1a24, prio:23, stack:6656, core=0
I (602) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (602) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (632) wifi:wifi firmware version: 3ea4c76
I (632) wifi:config NVS flash: enabled
I (632) wifi:config nano formating: disabled
I (632) wifi:Init dynamic tx buffer num: 32
I (632) wifi:Init data frame dynamic rx buffer num: 32
I (642) wifi:Init management frame dynamic rx buffer num: 32
I (642) wifi:Init management short buffer num: 32
I (652) wifi:Init static rx buffer size: 1600
I (652) wifi:Init static rx buffer num: 10
I (662) wifi:Init dynamic rx buffer num: 32
I (772) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0
I (772) wifi:mode : softAP (24:0a:c4:e0:43:81)
I (772) wifi:Total power save buffer number: 16
I (772) wifi:Init max length of beacon: 752/752
I (782) wifi:Init max length of beacon: 752/752
I (792) wifi softAP: wifi_init_softap finished. SSID:esp32_iu password:iuplus2020 channel:1
I (792) wifi softAP: My IP : 63.64.51.212 ===> I set to 192.168.4.56 and esp says different ip
I (802) wifi softAP: Socket created
I (802) wifi softAP: Socket bound, port 22
I (802) wifi softAP: Socket listening ===> after this line, it waits for accept from PC
I (3562) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
I (3562) wifi:station: 20:0d:b0:33:d9:c9 join, AID=1, bgn, 40U
I (3582) wifi softAP: station 20:0d:b0:33:d9:c9 join, AID=1
I (3592) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2

++++++++++

ESP_Sprite
Posts: 8999
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 SoftAp static ip config

Postby ESP_Sprite » Mon Sep 14, 2020 8:29 am

According to the source code you posted, you never configure the AP to a fixed IP at all. You just set the source IP for your socket to that IP, then 'magically' assume the entire AP will also use that IP.

boarchuz
Posts: 566
Joined: Tue Aug 21, 2018 5:28 am

Re: ESP32 SoftAp static ip config

Postby boarchuz » Mon Sep 14, 2020 8:53 am

Also MY_IP_ADDR is a string representation of an IP so htonl is not doing what you expect:

Code: Select all

#define MY_IP_ADDR  "192.168.4.56"
dest_addr.sin_addr.s_addr = htonl(MY_IP_ADDR);
You can use inet_addr or inet_aton to convert that to an IP, or use IP4_ADDR/esp_netif_ip4_makeu32 macros.

louiey
Posts: 5
Joined: Sat Sep 12, 2020 4:55 am

Re: ESP32 SoftAp static ip config

Postby louiey » Tue Sep 15, 2020 11:51 pm

boarchuz wrote:
Mon Sep 14, 2020 8:53 am
Also MY_IP_ADDR is a string representation of an IP so htonl is not doing what you expect:

Code: Select all

#define MY_IP_ADDR  "192.168.4.56"
dest_addr.sin_addr.s_addr = htonl(MY_IP_ADDR);
You can use inet_addr or inet_aton to convert that to an IP, or use IP4_ADDR/esp_netif_ip4_makeu32 macros.
Thanks for correction!!!

louiey
Posts: 5
Joined: Sat Sep 12, 2020 4:55 am

Re: ESP32 SoftAp static ip config

Postby louiey » Tue Sep 15, 2020 11:53 pm

ESP_Sprite wrote:
Mon Sep 14, 2020 8:29 am
According to the source code you posted, you never configure the AP to a fixed IP at all. You just set the source IP for your socket to that IP, then 'magically' assume the entire AP will also use that IP.
Can you give more in details how to config fixed ip with esp-idf?
I could see API at arduino based but not sure how based on esp-idf.

louiey
Posts: 5
Joined: Sat Sep 12, 2020 4:55 am

Re: ESP32 SoftAp static ip config

Postby louiey » Wed Sep 16, 2020 12:39 am

Hi.

I just fix the issue and attached is my working code for someone who has same question.
Attachments
softap_example_main.c
(7.67 KiB) Downloaded 934 times

Who is online

Users browsing this forum: Bing [Bot], bnp117, EB Solution and 76 guests