Socket for echo server on ESP32-WROWER-B module in access point mode

irgibon
Posts: 7
Joined: Thu May 23, 2019 11:05 am

Socket for echo server on ESP32-WROWER-B module in access point mode

Postby irgibon » 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?

  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "freertos/event_groups.h"
  4. #include "esp_wifi.h"
  5. #include "esp_system.h"
  6. #include "esp_event.h"
  7. #include "esp_event_loop.h"
  8. #include "nvs_flash.h"
  9.  
  10. #include "lwip/err.h"
  11. #include "lwip/sockets.h"
  12. #include "lwip/sys.h"
  13. #include <lwip/netdb.h>
  14.  
  15. #include "string.h"
  16.  
  17. #define SOFT_AP_SSID         CONFIG_WIFI_SSID
  18. #define SOFT_AP_PASSWORD     CONFIG_WIFI_PASSWORD
  19.  
  20. #define SOFT_AP_SERVER_PORT             (8080)
  21. #define SOFT_AP_SERVER_MAX_LISTEN       (1)
  22. #define SOFT_AP_SERVER_BUFFER_LENGTH    (32)
  23.  
  24. #define SOFT_AP_IP_ADDRESS_1 (192)
  25. #define SOFT_AP_IP_ADDRESS_2 (168)
  26. #define SOFT_AP_IP_ADDRESS_3 (4)
  27. #define SOFT_AP_IP_ADDRESS_4 (1)
  28.  
  29. #define SOFT_AP_GW_ADDRESS_1 SOFT_AP_IP_ADDRESS_1
  30. #define SOFT_AP_GW_ADDRESS_2 SOFT_AP_IP_ADDRESS_2
  31. #define SOFT_AP_GW_ADDRESS_3 SOFT_AP_IP_ADDRESS_3
  32. #define SOFT_AP_GW_ADDRESS_4 SOFT_AP_IP_ADDRESS_4
  33.  
  34. #define SOFT_AP_NM_ADDRESS_1 (255)
  35. #define SOFT_AP_NM_ADDRESS_2 (255)
  36. #define SOFT_AP_NM_ADDRESS_3 (255)
  37. #define SOFT_AP_NM_ADDRESS_4 (0)
  38.  
  39. static uint8_t buf[SOFT_AP_SERVER_BUFFER_LENGTH];
  40. static int sock;
  41.  
  42. static void setver_task(void *pvParameters)
  43. {
  44.     int listener, bytes_read;
  45.     struct sockaddr_in addr;
  46.  
  47.     listener = socket(PF_INET, SOCK_STREAM, 0);
  48.     if(listener < 0) abort();
  49.  
  50.     addr.sin_family = PF_INET;
  51.     addr.sin_port = htons(SOFT_AP_SERVER_PORT);
  52.     addr.sin_addr.s_addr = htonl(INADDR_ANY);
  53.  
  54.     if(bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0) abort();
  55.  
  56.     listen(listener, SOFT_AP_SERVER_MAX_LISTEN);
  57.  
  58.     sock = accept(listener, NULL, NULL);
  59.     if(sock < 0) abort();
  60.  
  61.      while(TRUE)
  62.      {
  63.          bytes_read = recv(sock, buf, MASSIV_SIZE(buf), 0);
  64.          if(bytes_read <= 0) break;
  65.          send(sock, buf, bytes_read, 0);
  66.      }
  67.  
  68.      close(sock);
  69. }
  70.  
  71. static esp_err_t wifiEventHandler(void* userParameter, system_event_t* event)
  72. {
  73.     switch(event->event_id)
  74.     {
  75.         case SYSTEM_EVENT_AP_STACONNECTED:
  76.         {
  77.             xTaskCreate(setver_task, "setver_task", 4096, NULL, 5, NULL);
  78.             break;
  79.         }
  80.         case SYSTEM_EVENT_AP_STADISCONNECTED:
  81.         {
  82.             break;
  83.         }
  84.         default:
  85.         {
  86.             break;
  87.         }
  88.     }
  89.     return ESP_OK;
  90. }
  91.  
  92. static void launchSoftAp(void)
  93. {
  94.     tcpip_adapter_ip_info_t ipAddressInfo;
  95.     wifi_init_config_t wifiConfiguration = WIFI_INIT_CONFIG_DEFAULT();
  96.  
  97.     ESP_ERROR_CHECK(nvs_flash_init());
  98.     tcpip_adapter_init();
  99.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  100.     ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  101.     memset(&ipAddressInfo, 0, sizeof(ipAddressInfo));
  102.  
  103.     IP4_ADDR(
  104.         &ipAddressInfo.ip,
  105.         SOFT_AP_IP_ADDRESS_1,
  106.         SOFT_AP_IP_ADDRESS_2,
  107.         SOFT_AP_IP_ADDRESS_3,
  108.         SOFT_AP_IP_ADDRESS_4);
  109.     IP4_ADDR(
  110.         &ipAddressInfo.gw,
  111.         SOFT_AP_GW_ADDRESS_1,
  112.         SOFT_AP_GW_ADDRESS_2,
  113.         SOFT_AP_GW_ADDRESS_3,
  114.         SOFT_AP_GW_ADDRESS_4);
  115.     IP4_ADDR(
  116.         &ipAddressInfo.netmask,
  117.         SOFT_AP_NM_ADDRESS_1,
  118.         SOFT_AP_NM_ADDRESS_2,
  119.         SOFT_AP_NM_ADDRESS_3,
  120.         SOFT_AP_NM_ADDRESS_4);
  121.  
  122.     ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &ipAddressInfo));
  123.     ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
  124.  
  125.     ESP_ERROR_CHECK(esp_event_loop_init(wifiEventHandler, NULL));
  126.     ESP_ERROR_CHECK(esp_wifi_init(&wifiConfiguration));
  127.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  128.  
  129.     wifi_config_t apConfiguration = {
  130.         .ap = {
  131.             .ssid = SOFT_AP_SSID,
  132.             .password = SOFT_AP_PASSWORD,
  133.             .ssid_len = strlen(SOFT_AP_SSID),
  134.             .authmode = WIFI_AUTH_WPA2_PSK,
  135.             .max_connection = SOFT_AP_SERVER_MAX_LISTEN,
  136.             .ssid_hidden = 0,
  137.             .beacon_interval = 120
  138.         }
  139.     };
  140.  
  141.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &apConfiguration));
  142.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  143.     ESP_ERROR_CHECK(esp_wifi_start());
  144. }
  145.  
  146. void app_main(void)
  147. {
  148.     launchSoftAp();
  149. }

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

Re: Socket for echo server on ESP32-WROWER-B module in access point mode

Postby ESP_Sprite » Thu Jul 18, 2019 6:21 am

I don't get your issue? The ESP32 is 192.168.4.1, the Win10 machine is 192.168.4.2, so if you want to connect to the ESP32, you should connect to 192.168.4.1. If you connect to 192.168.4.2, you will try to connect to a server running on your Win10 machine.

irgibon
Posts: 7
Joined: Thu May 23, 2019 11:05 am

Re: Socket for echo server on ESP32-WROWER-B module in access point mode

Postby irgibon » Thu Jul 18, 2019 7:50 am

Thank you very much, I figured it out.

User avatar
rudi ;-)
Posts: 1728
Joined: Fri Nov 13, 2015 3:25 pm

Re: Socket for echo server on ESP32-WROWER-B module in access point mode

Postby rudi ;-) » Thu Jul 18, 2019 9:09 am

irgibon wrote:
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?

Your ESP32 is an WIFI AP in this code
and listen on Port

Code: Select all

#define SOFT_AP_SERVER_PORT             (8080)
if you connect to these Wifi AP with your WIN10 Wifi then you
can controll the IP on your WIN10, usually DHCP is on,
so your ESP32 Wifi will give the WIN10 Wifi Client an IP ( example 192.168.4.2 )

now you can ping from WIN10 to ESP32
ping 192.168.4.1

you should get an answere usually.

cause the ESP32 Wifi AP port 8080 is open for income ( 192.168.4.1:8080 )
you can example connect from WIN10 to ESP32 with an TCP Client

connect the IP 192.168.4.1 at Port 8080
the esp32 listener will accept, create a socket handle
and your code says:

Code: Select all

while(TRUE)
     {
         bytes_read = recv(sock, buf, MASSIV_SIZE(buf), 0);
         if(bytes_read <= 0) break;
         send(sock, buf, bytes_read, 0);
     }
it is a simple echo server, so you should send a "hello" and close the cmd with CRLF from your TCP Client on WIN10
CR=Carriage Return
LF=Line Feed

the "hello" should come back from ESP32 to your WIN10 TCP Client

TCP Client example:
https://packetsender.com/
https://www.putty.org/
...

open putty, connect to IP 192.168.4.1 use Port 8080
Send "hello" and CR LF you should get back the hello in the Terminal as ECHO

hope this helps
best wishes
rudi ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: cdollar and 69 guests