void wifi_init_sta()
{
ESP_LOGI(TAG,"ESP-IDF tcpip_adapter");
s_wifi_event_group =
xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
#if CONFIG_STATIC_IP
//* Stop DHCP client */
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
/* Set STATIC IP Address */
tcpip_adapter_ip_info_t ipInfo;
IP4_ADDR(&ipInfo.ip, 192, 168, 10,
100);
IP4_ADDR(&ipInfo.gw, 192, 168, 10,
1);
IP4_ADDR(&ipInfo.netmask, 255, 255,
255, 0);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA,
&ipInfo);
/*
if we should not be using DHCP (for
example we are using static IP addresses),
then we need to instruct the ESP32 of
the locations of the DNS servers manually.
Google publicly makes available two
name servers with the addresses of 8.8.8.8 and 8.8.4.4.
*/
ip_addr_t d;
d.type = IPADDR_TYPE_V4;
d.u_addr.ip4.addr = 0x08080808;
//8.8.8.8 Google Public DNS(Primary)
dns_setserver(0, &d);
d.u_addr.ip4.addr = 0x08080404;
//8.8.4.4 Google Public DNS(Secondary)
dns_setserver(1, &d);
#endif
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, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT,
IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA,
&wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta
finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s
password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
// wait for IP_EVENT_STA_GOT_IP
while(1) {
/* Wait forever for
WIFI_CONNECTED_BIT to be set within the event group.
Clear
the bits beforeexiting. */
EventBits_t uxBits =
xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT, /* The bits within the event group to
waitfor. */
pdTRUE, /*
WIFI_CONNECTED_BIT should be cleared before returning. */
pdFALSE, /*
Don't waitfor both bits, either bit will do. */
portMAX_DELAY);/* Wait forever. */
if ( ( uxBits &
WIFI_CONNECTED_BIT ) == WIFI_CONNECTED_BIT ){
ESP_LOGI(TAG, "WIFI_CONNECTED_BIT");
break;
}
}
ESP_LOGI(TAG, "Got IP Address.");
}
|