Skip to content

Commit 8f026e6

Browse files
pablogs9Raphael Vogelgsang
andauthored
ESP32 Wifi (#16)
* Initial * Added patch * Updated launcher task stack * udp without patch Signed-off-by: Raphael Vogelgsang <Raphael.Vogelgsang@de.bosch.com> Co-authored-by: Raphael Vogelgsang <Raphael.Vogelgsang@de.bosch.com>
1 parent 0c4dc93 commit 8f026e6

4 files changed

Lines changed: 145 additions & 8 deletions

File tree

microros_esp32_extensions/esp32_toolchain.cmake.in

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(CMAKE_SYSTEM_NAME Generic)
44
set(CMAKE_SYSTEM_PROCESSOR xtensa)
55
set(CMAKE_CROSSCOMPILING 1)
66
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
7-
set(PLATFORM_NAME "ESP32")
7+
set(PLATFORM_NAME "LwIP")
88

99
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
1010
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
@@ -18,6 +18,8 @@ set(CMAKE_CXX_FLAGS_INIT "@CXXFLAGS@" CACHE STRING "" FORCE)
1818

1919
set(idf_path "@IDF_PATH@")
2020

21+
add_definitions(-DLWIP_IPV4 -DLWIP_IPV6)
22+
2123
include_directories(
2224
"@BUILD_CONFIG_DIR@"
2325
${idf_path}/components/newlib/platform_include
@@ -35,9 +37,6 @@ include_directories(
3537
${idf_path}/components/coap/libcoap/include
3638
${idf_path}/components/tcp_transport/include
3739
${idf_path}/components/unity/include
38-
${idf_path}/components/lwip/port/esp32/include
39-
${idf_path}/components/lwip/lwip/src/include
40-
${idf_path}/components/lwip/include
4140
${idf_path}/components/esp32/include
4241
${idf_path}/components/asio/port/include
4342
${idf_path}/components/asio/asio/asio/include
@@ -72,4 +71,9 @@ include_directories(
7271
${idf_path}/components/sdmmc/include
7372
${idf_path}/components/esp_local_ctrl/include
7473
${idf_path}/components/esp_common/include
74+
75+
${idf_path}/components/lwip/lwip/src/include/lwip/apps
76+
${idf_path}/components/lwip/lwip/src/include/compat/posix
77+
${idf_path}/components/lwip/lwip/src/include
78+
${idf_path}/components/lwip/port/esp32/include
7579
)

microros_esp32_extensions/main/Kconfig.projbuild

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
menu "MicroRos Transport Settings"
1+
menu "micro-ROS Transport Settings"
22

33
menu "UART Settings (for serial transport)"
44

@@ -32,5 +32,25 @@ config MICROROS_UART_CTS
3232

3333
endmenu
3434

35+
menu "WiFi Configuration"
36+
37+
config ESP_WIFI_SSID
38+
string "WiFi SSID"
39+
default "myssid"
40+
help
41+
SSID (network name) for the example to connect to.
42+
43+
config ESP_WIFI_PASSWORD
44+
string "WiFi Password"
45+
default "mypassword"
46+
help
47+
WiFi password (WPA or WPA2) for the example to use.
48+
49+
config ESP_MAXIMUM_RETRY
50+
int "Maximum retry"
51+
default 5
52+
help
53+
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
54+
endmenu
3555

3656
endmenu
Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,127 @@
11
#include "app.h"
22

3+
#include "uxr/client/config.h"
4+
35
#include "freertos/FreeRTOS.h"
46
#include "freertos/task.h"
5-
#include "esp_system.h"
67

78
#include <driver/uart.h>
89
#include <driver/gpio.h>
910

10-
void app_main(void)
11+
#include <string.h>
12+
#include "freertos/event_groups.h"
13+
#include "esp_system.h"
14+
#include "esp_wifi.h"
15+
#include "esp_event.h"
16+
#include "esp_log.h"
17+
#include "esp_system.h"
18+
#include "nvs_flash.h"
19+
20+
#include "lwip/err.h"
21+
#include "lwip/sys.h"
22+
23+
#define ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
24+
#define ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
25+
#define ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
26+
27+
static EventGroupHandle_t s_wifi_event_group;
28+
29+
#define WIFI_CONNECTED_BIT BIT0
30+
#define WIFI_FAIL_BIT BIT1
31+
32+
static const char *TAG = "wifi station";
33+
static int s_retry_num = 0;
34+
35+
static void event_handler(void* arg, esp_event_base_t event_base,
36+
int32_t event_id, void* event_data)
1137
{
38+
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
39+
esp_wifi_connect();
40+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
41+
if (s_retry_num < ESP_MAXIMUM_RETRY) {
42+
esp_wifi_connect();
43+
s_retry_num++;
44+
ESP_LOGI(TAG, "retry to connect to the AP");
45+
} else {
46+
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
47+
}
48+
ESP_LOGI(TAG,"connect to the AP fail");
49+
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
50+
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
51+
ESP_LOGI(TAG, "got ip:%s",
52+
ip4addr_ntoa(&event->ip_info.ip));
53+
s_retry_num = 0;
54+
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
55+
}
56+
}
57+
58+
void wifi_init_sta()
59+
{
60+
s_wifi_event_group = xEventGroupCreate();
61+
62+
tcpip_adapter_init();
63+
64+
ESP_ERROR_CHECK(esp_event_loop_create_default());
65+
66+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
67+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
68+
69+
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
70+
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
71+
72+
wifi_config_t wifi_config = {
73+
.sta = {
74+
.ssid = ESP_WIFI_SSID,
75+
.password = ESP_WIFI_PASS
76+
},
77+
};
78+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
79+
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
80+
ESP_ERROR_CHECK(esp_wifi_start() );
81+
82+
ESP_LOGI(TAG, "wifi_init_sta finished.");
83+
84+
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
85+
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
86+
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
87+
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
88+
pdFALSE,
89+
pdFALSE,
90+
portMAX_DELAY);
91+
92+
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
93+
* happened. */
94+
if (bits & WIFI_CONNECTED_BIT) {
95+
ESP_LOGI(TAG, "connected to ap SSID:%s", ESP_WIFI_SSID);
96+
} else if (bits & WIFI_FAIL_BIT) {
97+
ESP_LOGI(TAG, "Failed to connect to SSID:%s", ESP_WIFI_SSID);
98+
} else {
99+
ESP_LOGE(TAG, "UNEXPECTED EVENT");
100+
}
101+
102+
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
103+
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
104+
vEventGroupDelete(s_wifi_event_group);
105+
}
106+
107+
108+
109+
void app_main(void)
110+
{
111+
// Start networkign if required
112+
#ifdef UCLIENT_PROFILE_UDP
113+
//Initialize NVS
114+
esp_err_t ret = nvs_flash_init();
115+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
116+
ESP_ERROR_CHECK(nvs_flash_erase());
117+
ret = nvs_flash_init();
118+
}
119+
ESP_ERROR_CHECK(ret);
120+
121+
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
122+
wifi_init_sta();
123+
#endif // UCLIENT_PROFILE_UDP
124+
12125
// start microROS task
13126
xTaskCreate(appMain, "uros_task", 12*2048, NULL, 5, NULL);
14127
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
CONFIG_ESP_MAIN_TASK_STACK_SIZE=25000
1+
CONFIG_ESP_MAIN_TASK_STACK_SIZE=3000
22
CONFIG_FREERTOS_UNICORE=y
33
CONFIG_ESP_TASK_WDT=n

0 commit comments

Comments
 (0)