1 /* Common WiFi Init as STA for ASIO examples
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
9 #include "freertos/FreeRTOS.h"
10 #include "freertos/task.h"
11 #include "freertos/event_groups.h"
12 #include "esp_system.h"
14 #include "esp_event_loop.h"
16 #include "nvs_flash.h"
17 #include "driver/uart.h"
18 #include "esp_console.h"
19 #include "esp_vfs_dev.h"
26 /* The examples use simple WiFi configuration that you can set via
29 If you'd rather not, just change the below entries to strings with
30 the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
32 #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
33 #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
35 /* FreeRTOS event group to signal when we are connected*/
36 static EventGroupHandle_t wifi_event_group;
38 /* The event group allows multiple bits for each event,
39 but we only care about one event - are we connected
40 to the AP with an IP? */
41 const int WIFI_CONNECTED_BIT = BIT0;
43 static const char *TAG = "asio example wifi init";
46 * Definition of ASIO main method, which is called after network initialized
50 static esp_err_t event_handler(void *ctx, system_event_t *event)
52 switch(event->event_id) {
53 case SYSTEM_EVENT_STA_START:
56 case SYSTEM_EVENT_STA_GOT_IP:
57 ESP_LOGI(TAG, "got ip:%s",
58 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
59 xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
61 case SYSTEM_EVENT_AP_STACONNECTED:
62 ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d",
63 MAC2STR(event->event_info.sta_connected.mac),
64 event->event_info.sta_connected.aid);
66 case SYSTEM_EVENT_AP_STADISCONNECTED:
67 ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d",
68 MAC2STR(event->event_info.sta_disconnected.mac),
69 event->event_info.sta_disconnected.aid);
71 case SYSTEM_EVENT_STA_DISCONNECTED:
73 xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
83 wifi_event_group = xEventGroupCreate();
86 ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
88 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
89 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
90 wifi_config_t wifi_config;
91 // zero out the config struct to ensure defaults are setup
92 memset(&wifi_config, 0, sizeof(wifi_sta_config_t));
93 // only copy ssid&password from example config
94 strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID);
95 strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS);
97 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
98 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
99 ESP_ERROR_CHECK(esp_wifi_start() );
101 ESP_LOGI(TAG, "wifi_init_sta finished.");
102 ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
103 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
106 extern "C" void app_main()
109 esp_err_t ret = nvs_flash_init();
110 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
111 ESP_ERROR_CHECK(nvs_flash_erase());
112 ret = nvs_flash_init();
114 ESP_ERROR_CHECK(ret);
116 ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
119 // Initialize VFS & UART so we can use std::cout/cin
120 setvbuf(stdin, NULL, _IONBF, 0);
121 setvbuf(stdout, NULL, _IONBF, 0);
122 /* Install UART driver for interrupt-driven reads and writes */
123 ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM,
124 256, 0, 0, NULL, 0) );
125 /* Tell VFS to use UART driver */
126 esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
127 esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
128 /* Move the caret to the beginning of the next line on '\n' */
129 esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
131 // wait till we receive IP, so asio realated code can be started
132 xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY);
134 // network is ready, let's proceed with ASIO example