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.
- /* WiFi softAP Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_system.h"
- #include "esp_wifi.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "nvs_flash.h"
- #include "lwip/err.h"
- #include "lwip/sys.h"
- #include "esp_netif.h"
- #include "lwip/sockets.h"
- #include <lwip/netdb.h>
- /* The examples use WiFi configuration that you can set via project configuration menu.
- If you'd rather not, just change the below entries to strings with
- the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
- */
- #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
- #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
- #define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
- #define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
- #define CONFIG_EXAMPLE_IPV4
- #define PORT 22
- #define MY_IP_ADDR "192.168.4.56"
- #define TCPIP_SERVER
- static const char *TAG = "wifi softAP";
- static void wifi_event_handler(void* arg, esp_event_base_t event_base,
- int32_t event_id, void* event_data)
- {
- if (event_id == WIFI_EVENT_AP_STACONNECTED) {
- wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
- ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
- MAC2STR(event->mac), event->aid);
- } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
- wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
- ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
- MAC2STR(event->mac), event->aid);
- }
- }
- void wifi_init_softap(void)
- {
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- esp_netif_create_default_wifi_ap();
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
- wifi_config_t wifi_config = {
- .ap = {
- .ssid = EXAMPLE_ESP_WIFI_SSID,
- .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
- .channel = EXAMPLE_ESP_WIFI_CHANNEL,
- .password = EXAMPLE_ESP_WIFI_PASS,
- .max_connection = EXAMPLE_MAX_STA_CONN,
- .authmode = WIFI_AUTH_WPA_WPA2_PSK
- },
- };
- if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
- wifi_config.ap.authmode = WIFI_AUTH_OPEN;
- }
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
- ESP_ERROR_CHECK(esp_wifi_start());
- ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
- EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
- }
- #ifdef TCPIP_SERVER
- static void do_retransmit(const int sock)
- {
- int len;
- char rx_buffer[128];
- do {
- len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
- if (len < 0) {
- ESP_LOGE(TAG, "Error occurred during receiving: errno %d", errno);
- } else if (len == 0) {
- ESP_LOGW(TAG, "Connection closed");
- } else {
- rx_buffer[len] = 0; // Null-terminate whatever is received and treat it like a string
- ESP_LOGI(TAG, "Received %d bytes: %s", len, rx_buffer);
- // send() can return less bytes than supplied length.
- // Walk-around for robust implementation.
- int to_write = len;
- while (to_write > 0) {
- int written = send(sock, rx_buffer + (len - to_write), to_write, 0);
- if (written < 0) {
- ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
- }
- to_write -= written;
- }
- }
- } while (len > 0);
- }
- static void tcp_server_task(void *pvParameters)
- {
- char addr_str[128];
- int addr_family;
- int ip_protocol;
- #ifdef CONFIG_EXAMPLE_IPV4
- struct sockaddr_in dest_addr, client_addr;
- //dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- dest_addr.sin_addr.s_addr = htonl(MY_IP_ADDR);
- dest_addr.sin_family = AF_INET;
- dest_addr.sin_port = htons(PORT);
- addr_family = AF_INET;
- ip_protocol = IPPROTO_IP;
- inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
- ESP_LOGI(TAG, "My IP : %s", addr_str);
- #else // IPV6
- struct sockaddr_in6 dest_addr;
- bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
- dest_addr.sin6_family = AF_INET6;
- dest_addr.sin6_port = htons(PORT);
- addr_family = AF_INET6;
- ip_protocol = IPPROTO_IPV6;
- inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
- #endif
- int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
- if (listen_sock < 0) {
- ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
- vTaskDelete(NULL);
- return;
- }
- ESP_LOGI(TAG, "Socket created");
- int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
- if (err != 0) {
- ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
- goto CLEAN_UP;
- }
- ESP_LOGI(TAG, "Socket bound, port %d", PORT);
- err = listen(listen_sock, 1);
- if (err != 0) {
- ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
- goto CLEAN_UP;
- }
- while (1) {
- ESP_LOGI(TAG, "Socket listening");
- #if 0
- struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
- uint addr_len = sizeof(source_addr);
- int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
- if (sock < 0) {
- ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
- break;
- }
- // Convert ip address to string
- if (source_addr.sin6_family == PF_INET) {
- inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
- } else if (source_addr.sin6_family == PF_INET6) {
- inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
- }
- ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
- #else
- uint addr_len = sizeof(client_addr);
- int sock = accept(listen_sock, (struct sockaddr *)&client_addr, &addr_len);
- if (sock < 0) {
- ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
- break;
- }
- inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, addr_str, sizeof(addr_str));
- ESP_LOGI(TAG, "Server : %s client connected.\n", addr_str);
- #endif
- do_retransmit(sock);
- shutdown(sock, 0);
- close(sock);
- }
- CLEAN_UP:
- close(listen_sock);
- vTaskDelete(NULL);
- }
- #endif
- void app_main(void)
- {
- //Initialize NVS
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
- wifi_init_softap();
- #ifdef TCPIP_SERVER
- xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL);
- #endif
- }