diff --git a/apps/ping_pong/app.c b/apps/ping_pong/app.c index ba6cd6bf..97dc8b64 100644 --- a/apps/ping_pong/app.c +++ b/apps/ping_pong/app.c @@ -80,6 +80,16 @@ void pong_subscription_callback(const void * msgin) void appMain(void *argument) { + +#if defined(CONFIG_ESP_WIFI_AP) + volatile bool *is_sta = (volatile bool *) argument; + // Wait for a connection to the wifi AP. The first to connect + // is considered the agent + while (! *is_sta) { + usleep(100000); + } +#endif // if defined(CONFIG_ESP_WIFI_AP) + rcl_allocator_t allocator = rcl_get_default_allocator(); rclc_support_t support; diff --git a/microros_esp32_extensions/main/Kconfig.projbuild b/microros_esp32_extensions/main/Kconfig.projbuild index 06226d68..2e67b7df 100644 --- a/microros_esp32_extensions/main/Kconfig.projbuild +++ b/microros_esp32_extensions/main/Kconfig.projbuild @@ -34,6 +34,12 @@ endmenu menu "WiFi Configuration" + config ESP_WIFI_AP + bool "Wifi act as Access point" + default false + help + The ESP32 is acting as an access point + config ESP_WIFI_SSID string "WiFi SSID" default "myssid" @@ -46,6 +52,12 @@ menu "WiFi Configuration" help WiFi password (WPA or WPA2) for the example to use. + config ESP_WIFI_CHANNEL + int "Wifi channel 1 to 13" + range 1 11 + default 8 + help + config ESP_MAXIMUM_RETRY int "Maximum retry" default 5 diff --git a/microros_esp32_extensions/main/main.c b/microros_esp32_extensions/main/main.c index 830c3e32..b1b77a1d 100644 --- a/microros_esp32_extensions/main/main.c +++ b/microros_esp32_extensions/main/main.c @@ -24,6 +24,8 @@ #define ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID #define ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD #define ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY +#define ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL +#define ESP_MAX_STA_CONN 4 static EventGroupHandle_t s_wifi_event_group; @@ -33,9 +35,26 @@ static EventGroupHandle_t s_wifi_event_group; static const char *TAG = "wifi station"; static int s_retry_num = 0; + +#if defined(CONFIG_ESP_WIFI_AP) +volatile bool is_sta = false; +#endif // defined(CONFIG_ESP_WIFI_AP) + static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { +#if defined(CONFIG_ESP_WIFI_AP) + 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); + is_sta = true; + } 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); + } +#else // defined(CONFIG_ESP_WIFI_AP) if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { @@ -54,8 +73,49 @@ static void event_handler(void* arg, esp_event_base_t event_base, s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } +#endif // defined(CONFIG_ESP_WIFI_AP) +} + +#if defined(CONFIG_ESP_WIFI_AP) +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, + &event_handler, + NULL)); + + wifi_config_t wifi_config = { + .ap = { + .ssid = ESP_WIFI_SSID, + .ssid_len = strlen(ESP_WIFI_SSID), + .channel = ESP_WIFI_CHANNEL, + .password = ESP_WIFI_PASS, + .max_connection = ESP_MAX_STA_CONN, + .authmode = WIFI_AUTH_WPA_WPA2_PSK + }, + }; + if (strlen(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(WIFI_IF_AP, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d", + ESP_WIFI_SSID, ESP_WIFI_PASS, ESP_WIFI_CHANNEL); } +#endif // defined(CONFIG_ESP_WIFI_AP) +#if !defined(CONFIG_ESP_WIFI_AP) void wifi_init_sta() { s_wifi_event_group = xEventGroupCreate(); @@ -104,6 +164,7 @@ void wifi_init_sta() ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler)); vEventGroupDelete(s_wifi_event_group); } +#endif // !defined(CONFIG_ESP_WIFI_AP) static size_t uart_no = UART_NUM_0; @@ -119,8 +180,13 @@ void app_main(void) } ESP_ERROR_CHECK(ret); +#if defined(CONFIG_ESP_WIFI_AP) + ESP_LOGW(TAG, "ESP_WIFI_MODE_AP"); + wifi_init_softap(); +#else // defined(CONFIG_ESP_WIFI_AP) ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); wifi_init_sta(); +#endif // defined(CONFIG_ESP_WIFI_AP) #elif defined(RMW_UXRCE_TRANSPORT_CUSTOM) rmw_uros_set_custom_transport( true, @@ -131,6 +197,10 @@ void app_main(void) esp32_serial_read); #endif +#if defined(CONFIG_ESP_WIFI_AP) // start microROS task + xTaskCreate(appMain, "uros_task", 12*2048, &is_sta, 5, NULL); +#else // defined(CONFIG_ESP_WIFI_AP) xTaskCreate(appMain, "uros_task", 12*2048, NULL, 5, NULL); +#endif // defined(CONFIG_ESP_WIFI_AP) }