Socket for echo server on ESP32-WROWER-B module in access point mode
Posted: Wed Jul 17, 2019 1:26 pm
Hello! Help solve the problem, please.
I implement a simple echo server on the ESP32-WROWER-B module installed on the ESP-WROWER-KIT v.4.1 board in the “access point (WIFI_MODE_AP)” mode. The firmware works, but somehow strange. I use Windows 10. It detects the network created and successfully connects to it. The "ping 192.168.4.1" and "ping 192.168.4.2" commands are successfully executed. However, for some reason, the client program can establish a connection and exchange data only if it opens a socket at 192.168.4.1. An attempt to open a socket on 192.168.4.2 fails. It seems to me that the client program should interact with the server according to 192.168.4.2. Or am I wrong?
I implement a simple echo server on the ESP32-WROWER-B module installed on the ESP-WROWER-KIT v.4.1 board in the “access point (WIFI_MODE_AP)” mode. The firmware works, but somehow strange. I use Windows 10. It detects the network created and successfully connects to it. The "ping 192.168.4.1" and "ping 192.168.4.2" commands are successfully executed. However, for some reason, the client program can establish a connection and exchange data only if it opens a socket at 192.168.4.1. An attempt to open a socket on 192.168.4.2 fails. It seems to me that the client program should interact with the server according to 192.168.4.2. Or am I wrong?
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "esp_wifi.h"
- #include "esp_system.h"
- #include "esp_event.h"
- #include "esp_event_loop.h"
- #include "nvs_flash.h"
- #include "lwip/err.h"
- #include "lwip/sockets.h"
- #include "lwip/sys.h"
- #include <lwip/netdb.h>
- #include "string.h"
- #define SOFT_AP_SSID CONFIG_WIFI_SSID
- #define SOFT_AP_PASSWORD CONFIG_WIFI_PASSWORD
- #define SOFT_AP_SERVER_PORT (8080)
- #define SOFT_AP_SERVER_MAX_LISTEN (1)
- #define SOFT_AP_SERVER_BUFFER_LENGTH (32)
- #define SOFT_AP_IP_ADDRESS_1 (192)
- #define SOFT_AP_IP_ADDRESS_2 (168)
- #define SOFT_AP_IP_ADDRESS_3 (4)
- #define SOFT_AP_IP_ADDRESS_4 (1)
- #define SOFT_AP_GW_ADDRESS_1 SOFT_AP_IP_ADDRESS_1
- #define SOFT_AP_GW_ADDRESS_2 SOFT_AP_IP_ADDRESS_2
- #define SOFT_AP_GW_ADDRESS_3 SOFT_AP_IP_ADDRESS_3
- #define SOFT_AP_GW_ADDRESS_4 SOFT_AP_IP_ADDRESS_4
- #define SOFT_AP_NM_ADDRESS_1 (255)
- #define SOFT_AP_NM_ADDRESS_2 (255)
- #define SOFT_AP_NM_ADDRESS_3 (255)
- #define SOFT_AP_NM_ADDRESS_4 (0)
- static uint8_t buf[SOFT_AP_SERVER_BUFFER_LENGTH];
- static int sock;
- static void setver_task(void *pvParameters)
- {
- int listener, bytes_read;
- struct sockaddr_in addr;
- listener = socket(PF_INET, SOCK_STREAM, 0);
- if(listener < 0) abort();
- addr.sin_family = PF_INET;
- addr.sin_port = htons(SOFT_AP_SERVER_PORT);
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- if(bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0) abort();
- listen(listener, SOFT_AP_SERVER_MAX_LISTEN);
- sock = accept(listener, NULL, NULL);
- if(sock < 0) abort();
- while(TRUE)
- {
- bytes_read = recv(sock, buf, MASSIV_SIZE(buf), 0);
- if(bytes_read <= 0) break;
- send(sock, buf, bytes_read, 0);
- }
- close(sock);
- }
- static esp_err_t wifiEventHandler(void* userParameter, system_event_t* event)
- {
- switch(event->event_id)
- {
- case SYSTEM_EVENT_AP_STACONNECTED:
- {
- xTaskCreate(setver_task, "setver_task", 4096, NULL, 5, NULL);
- break;
- }
- case SYSTEM_EVENT_AP_STADISCONNECTED:
- {
- break;
- }
- default:
- {
- break;
- }
- }
- return ESP_OK;
- }
- static void launchSoftAp(void)
- {
- tcpip_adapter_ip_info_t ipAddressInfo;
- wifi_init_config_t wifiConfiguration = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(nvs_flash_init());
- tcpip_adapter_init();
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
- memset(&ipAddressInfo, 0, sizeof(ipAddressInfo));
- IP4_ADDR(
- &ipAddressInfo.ip,
- SOFT_AP_IP_ADDRESS_1,
- SOFT_AP_IP_ADDRESS_2,
- SOFT_AP_IP_ADDRESS_3,
- SOFT_AP_IP_ADDRESS_4);
- IP4_ADDR(
- &ipAddressInfo.gw,
- SOFT_AP_GW_ADDRESS_1,
- SOFT_AP_GW_ADDRESS_2,
- SOFT_AP_GW_ADDRESS_3,
- SOFT_AP_GW_ADDRESS_4);
- IP4_ADDR(
- &ipAddressInfo.netmask,
- SOFT_AP_NM_ADDRESS_1,
- SOFT_AP_NM_ADDRESS_2,
- SOFT_AP_NM_ADDRESS_3,
- SOFT_AP_NM_ADDRESS_4);
- ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ipAddressInfo));
- ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
- ESP_ERROR_CHECK(esp_event_loop_init(wifiEventHandler, NULL));
- ESP_ERROR_CHECK(esp_wifi_init(&wifiConfiguration));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
- wifi_config_t apConfiguration = {
- .ap = {
- .ssid = SOFT_AP_SSID,
- .password = SOFT_AP_PASSWORD,
- .ssid_len = strlen(SOFT_AP_SSID),
- .authmode = WIFI_AUTH_WPA2_PSK,
- .max_connection = SOFT_AP_SERVER_MAX_LISTEN,
- .ssid_hidden = 0,
- .beacon_interval = 120
- }
- };
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &apConfiguration));
- ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
- ESP_ERROR_CHECK(esp_wifi_start());
- }
- void app_main(void)
- {
- launchSoftAp();
- }