I tried the code you suggested. Here is my code and result.
/* 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_mac.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_system.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/dns.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 F_SET 1
#define F_RES 0
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASS = "11111111";
static const char *TAG = "wifi_AP";
void http_get_task (void *pvParameters);
unsigned char F_WIFI_CONNECT;
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
wifi_event_sta_connected_t *event_stacon ;
wifi_event_sta_disconnected_t *event_stadis;
ip_event_got_ip_t *ip_sta;
wifi_event_ap_staconnected_t *event_apcon;
wifi_event_ap_stadisconnected_t *event_apdiscon;
if(event_base == WIFI_EVENT)
{
switch (event_id)
{
case WIFI_EVENT_STA_START: //2
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_STA_START]");
esp_wifi_connect();
break;
case WIFI_EVENT_STA_CONNECTED://4
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_STA_CONNECTED]");
event_stacon = (wifi_event_sta_connected_t*) event_data;
ESP_LOGE(TAG,"STA_CONNECTED-SSID[%s]",event_stacon->ssid);
break;
case WIFI_EVENT_STA_DISCONNECTED://5
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_STA_DISCONNECTED]");
event_stadis = ( wifi_event_sta_disconnected_t*)event_data;
ESP_LOGE(TAG, "STA_DISON_SSID:[%s]",event_stadis->ssid);
esp_wifi_connect();
break;
case WIFI_EVENT_STA_STOP://3
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_STA_STOP]");
break;
case WIFI_EVENT_AP_START: //12
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_AP_START]");
break;
case WIFI_EVENT_AP_STOP://13
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_AP_STOP]");
break;
case WIFI_EVENT_AP_STACONNECTED: //14
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_AP_STACONNECTED]");
event_apcon = (wifi_event_ap_staconnected_t*)event_data;
ESP_LOGE(TAG, "AP_CON -station_MAC:["MACSTR"] , AID=%d",MAC2STR(event_apcon->mac), event_apcon->aid);
break;
case WIFI_EVENT_AP_STADISCONNECTED: //15
ESP_LOGE(TAG,"WIFI_HNDR:[WIFI_EVENT_AP_STADISCONNECTED]");
event_apdiscon = (wifi_event_ap_stadisconnected_t*)event_data;
ESP_LOGE(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event_apdiscon->mac), event_apdiscon->aid);
break;
default: break;
}
} //END_WIFi_EVENT
if(event_base == IP_EVENT)
{
switch (event_id)
{
case IP_EVENT_STA_GOT_IP ://0
ESP_LOGE(TAG,"WIFI_HNDR:[IP_EVENT_STA_GOT_IP]");
ip_sta = (ip_event_got_ip_t*)event_data;
ESP_LOGE(TAG, "ETHIP:" IPSTR, IP2STR(&ip_sta->ip_info.ip));
ESP_LOGE(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_sta->ip_info.netmask));
ESP_LOGE(TAG, "ETHGW:" IPSTR, IP2STR(&ip_sta->ip_info.gw));
F_WIFI_CONNECT = F_SET;
break;
case IP_EVENT_STA_LOST_IP://1
ESP_LOGE(TAG,"WIFI_HNDR:[IP_EVENT_STA_LOST_IP]");
break;
case IP_EVENT_AP_STAIPASSIGNED:
ESP_LOGE(TAG,"IP_AP_STAIPASSIGNED");
break;
}
}
}
int size_ssid;
void wifi_init_sta(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_init_cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_cfg));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
wifi_config_t wifi_config_sta;
memset(&wifi_config_sta, 0, sizeof(wifi_config_t));
size_ssid = sizeof(WIFI_SSID);
ESP_LOGE(TAG,"Size ssid :[%i]", size_ssid);
strlcpy((char*)wifi_config_sta.sta.ssid,WIFI_SSID,12);
//strlcpy((char*)wifi_config_sta.sta.password,WIFI_PASS,sizeof(WIFI_PASS));
wifi_config_sta.sta.threshold.authmode = WIFI_AUTH_OPEN;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config_sta) ); // ESP_IF_WIFI_STA
ESP_ERROR_CHECK(esp_wifi_start() );
//"WiFi_Bondarenko_Family","hDj8BP2b74b
}
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);
F_WIFI_CONNECT = F_RES;
wifi_init_sta();
xTaskCreate(&http_get_task, "http_get_task", 8096, NULL, 5, NULL);
}
//HTTP
#define WEB_SERVER "example.com"
#define WEB_PORT "80"
#define WEB_PATH "/"
static const char *REQUEST = "GET " WEB_PATH " HTTP/1.0\r\n"
"Host: "WEB_SERVER":"WEB_PORT"\r\n"
"User-Agent: esp-idf/1.0 esp32\r\n"
"\r\n";
#define WAITE_WIFI_CONNECT 0
#define HTTP_REQUST 1
void http_get_task (void *pvParameters)
{
struct addrinfo hints;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *res;
struct in_addr *addr;
int s, r;
char recv_buff[512];
int err ;
unsigned char state;
state = WAITE_WIFI_CONNECT;
for(;;)
{
if(state == WAITE_WIFI_CONNECT)
{
vTaskDelay(1000/portTICK_PERIOD_MS);
ESP_LOGI(TAG,"Waite connect WiFi");
if(F_WIFI_CONNECT == F_SET)
{
ESP_LOGI(TAG,"WIFI_CONNECTED!");
state = HTTP_REQUST;
}
}
if(state == HTTP_REQUST)
{
err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res);
if(err !=0 || res == NULL)
{
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr));
s = socket(res->ai_family, res->ai_socktype, 0);
if(s < 0) {
ESP_LOGE(TAG, "... Failed to allocate socket.");
freeaddrinfo(res);
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "... allocated socket");
if(connect(s, res->ai_addr, res->ai_addrlen) != 0) {
ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
close(s);
freeaddrinfo(res);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "... connected");
freeaddrinfo(res);
if (write(s, REQUEST, strlen(REQUEST)) < 0) {
ESP_LOGE(TAG, "... socket send failed");
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "... socket send success");
struct timeval receiving_timeout;
receiving_timeout.tv_sec = 5;
receiving_timeout.tv_usec = 0;
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout,
sizeof(receiving_timeout)) < 0) {
ESP_LOGE(TAG, "... failed to set socket receiving timeout");
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "... set socket receiving timeout success");
/* Read HTTP response */
do {
bzero(recv_buff, sizeof(recv_buff));
r = read(s, recv_buff, sizeof(recv_buff)-1);
for(int i = 0; i < r; i++) {
putchar(recv_buff[i]);
}
} while(r > 0);
ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d.", r, errno);
close(s);
for(int countdown = 10; countdown >= 0; countdown--) {
ESP_LOGI(TAG, "%d... ", countdown);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
ESP_LOGI(TAG, "Starting again!");
}//else
}
vTaskDelete(NULL);
}
Result:
e[0;31mE (137) wifi_AP: Size ssid :[4]e[0m
e[0;31mE (317) wifi_AP: WIFI_HNDR:[WIFI_EVENT_STA_START]e[0m
e[0;31mE (977) wifi_AP: WIFI_HNDR:[WIFI_EVENT_STA_CONNECTED]e[0m
e[0;31mE (977) wifi_AP: STA_CONNECTED-SSID[Wokwi-GUEST]e[0m
e[0;31mE (1977) wifi_AP: WIFI_HNDR:[IP_EVENT_STA_GOT_IP]e[0m
e[0;31mE (1977) wifi_AP: ETHIP:10.10.0.2e[0m
e[0;31mE (1977) wifi_AP: ETHMASK:255.255.0.0e[0m
e[0;31mE (1977) wifi_AP: ETHGW:10.0.0.1e[0m
e[0;31mE (2317) wifi_AP: DNS lookup failed err=200 res=0x0e[0m
e[0;31mE (3317) wifi_AP: DNS lookup failed err=200 res=0x0e[0m
e[0;31mE (4317) wifi_AP: DNS lookup failed err=200 res=0x0e[0m