Skip to content

Commit a507345

Browse files
authored
Merge pull request #73 from david-cermak/fix/split_local_remote_wifi
[examples]: Split two station example into local/remote sources
2 parents 57a9e56 + 79ef9d6 commit a507345

File tree

4 files changed

+125
-78
lines changed

4 files changed

+125
-78
lines changed

components/esp_wifi_remote/examples/two_stations/README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,48 @@
33
## How to use this example
44

55
* Run `server` example as the slave project (communication coprocessor)
6-
* Configure both the server and the client with mutual keys/certs as described in [mqtt example](../mqtt/README.md)
6+
* Select the backend solution
7+
- (in case of EPPP configure both the server and the client with mutual keys/certs as described in [mqtt example](../mqtt/README.md))
78
* Configure SSID and Password for two access points
89
- one for local Wi-Fi (using the Wi-Fi library on the chip itself)
910
- the second one for remote Wi-Fi (using the network offloading -- communication coprocessor)
1011
* Build and run the example
1112

12-
## Note
13+
## Wi-Fi types limitation
14+
15+
The `esp_wifi_remote` component faces type name conflicts when used alongside local Wi-Fi functionality. Both APIs use identical type names (e.g., `wifi_config_t`) but with different definitions for different hardware targets.
16+
17+
### The Solution: Separate Compilation Units
18+
19+
The component requires **separate compilation units** for local and remote Wi-Fi code:
20+
21+
```c
22+
// two_stations.c - Local Wi-Fi only
23+
#include "esp_wifi.h"
24+
25+
// remote_station.c - Remote Wi-Fi only
26+
#include "injected/esp_wifi.h" // SLAVE_ prefixed types
27+
#include "esp_wifi_remote.h"
28+
```
29+
30+
### How It Works
31+
32+
Wi-Fi remote provides generated "injected" headers with slave-specific types by transforming SOC capabilities:
33+
34+
```python
35+
# generate_and_check.py transforms:
36+
SOC_WIFI_SLAVE_SOC_WIFI_
37+
IDF_TARGET_SLAVE_IDF_TARGET_
38+
```
39+
40+
This allows the same Wi-Fi types to coexist with different definitions based on the target hardware.
41+
42+
### Best Practices
43+
44+
- Include injected headers before `esp_wifi_remote.h` in remote Wi-Fi files
45+
- Keep local and remote Wi-Fi code in separate `.c` files
46+
- Use function declarations to call between compilation units
1347

14-
This example supports EPPP RPC library with both UART and SPI transport options. Configure the transport method and pins using `idf.py menuconfig` under "WiFi Remote" settings.
1548

1649
## Example output
1750

@@ -57,3 +90,4 @@ I (7381) rpc_client: WIFI GW:192.168.32.3
5790
I (7381) rpc_client: WIFI mask:255.255.254.0
5891
I (7381) two wifi stations: connected to ap SSID:EspressifSystems
5992
I (7391) main_task: Returned from app_main()
93+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
idf_component_register(SRCS "two_stations.c"
1+
idf_component_register(SRCS "two_stations.c" "remote_station.c"
22
INCLUDE_DIRS ".")
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#include "freertos/FreeRTOS.h"
7+
#include "freertos/event_groups.h"
8+
// It is important to include *injected* headers before *esp-wifi-remote*
9+
// to use **SOC_SLAVE** Wi-Fi types in API typedefs, enums, structs
10+
#include "injected/esp_wifi.h"
11+
#include "esp_wifi_remote.h"
12+
#include "esp_event.h"
13+
#include "esp_log.h"
14+
15+
#define WIFI_REMOTE_CONNECTED_BIT BIT2
16+
#define WIFI_REMOTE_FAIL_BIT BIT3
17+
#define WIFI_REMOTE_BITS (WIFI_REMOTE_CONNECTED_BIT | WIFI_REMOTE_FAIL_BIT)
18+
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
19+
20+
static const char *TAG_remote = "two_stations_remote";
21+
static int s_retry_num = 0;
22+
static EventGroupHandle_t s_wifi_event_group;
23+
24+
static void event_handler_remote(void* arg, esp_event_base_t event_base,
25+
int32_t event_id, void* event_data)
26+
{
27+
if (event_base == WIFI_REMOTE_EVENT && event_id == WIFI_EVENT_STA_START) {
28+
esp_wifi_remote_connect();
29+
} else if (event_base == WIFI_REMOTE_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
30+
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
31+
esp_wifi_remote_connect();
32+
s_retry_num++;
33+
ESP_LOGI(TAG_remote, "retry to connect to the AP");
34+
} else {
35+
xEventGroupSetBits(s_wifi_event_group, WIFI_REMOTE_FAIL_BIT);
36+
}
37+
ESP_LOGI(TAG_remote,"connect to the AP fail");
38+
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
39+
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
40+
ESP_LOGI(TAG_remote, "Remote Wi-Fi got IP:" IPSTR, IP2STR(&event->ip_info.ip));
41+
s_retry_num = 0;
42+
xEventGroupSetBits(s_wifi_event_group, WIFI_REMOTE_CONNECTED_BIT);
43+
}
44+
}
45+
46+
void wifi_init_remote_sta(void)
47+
{
48+
esp_wifi_remote_create_default_sta();
49+
50+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
51+
ESP_ERROR_CHECK(esp_wifi_remote_init(&cfg));
52+
53+
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_REMOTE_EVENT, ESP_EVENT_ANY_ID, event_handler_remote, NULL));
54+
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler_remote, NULL));
55+
56+
wifi_config_t wifi_config = {
57+
.sta = {
58+
.ssid = CONFIG_ESP_WIFI_REMOTE_SSID,
59+
.password = CONFIG_ESP_WIFI_REMOTE_PASSWORD,
60+
},
61+
};
62+
ESP_ERROR_CHECK(esp_wifi_remote_set_mode(WIFI_MODE_STA));
63+
ESP_ERROR_CHECK(esp_wifi_remote_set_config(WIFI_IF_STA, &wifi_config));
64+
ESP_ERROR_CHECK(esp_wifi_remote_start());
65+
66+
ESP_LOGI(TAG_remote, "wifi_init_remote_sta finished.");
67+
68+
/* Waiting until either the connection is established the same way for REMOTE wifi */
69+
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_REMOTE_BITS, pdFALSE, pdFALSE, portMAX_DELAY);
70+
71+
if (bits & WIFI_REMOTE_CONNECTED_BIT) {
72+
ESP_LOGI(TAG_remote, "connected to ap SSID:%s", CONFIG_ESP_WIFI_REMOTE_SSID);
73+
} else if (bits & WIFI_REMOTE_FAIL_BIT) {
74+
ESP_LOGW(TAG_remote, "Failed to connect to SSID:%s", CONFIG_ESP_WIFI_REMOTE_SSID);
75+
} else {
76+
ESP_LOGE(TAG_remote, "UNEXPECTED EVENT");
77+
}
78+
}

components/esp_wifi_remote/examples/two_stations/main/two_stations.c

Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
6-
/* WiFi station Example
7-
8-
This example code is in the Public Domain (or CC0 licensed, at your option.)
9-
10-
Unless required by applicable law or agreed to in writing, this
11-
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12-
CONDITIONS OF ANY KIND, either express or implied.
13-
*/
146
#include <string.h>
157
#include "freertos/FreeRTOS.h"
168
#include "freertos/event_groups.h"
@@ -19,7 +11,6 @@
1911
#include "esp_event.h"
2012
#include "esp_log.h"
2113
#include "nvs_flash.h"
22-
#include "esp_wifi_remote.h"
2314
#include "console_ping.h"
2415
#include "iperf_cmd.h"
2516

@@ -29,14 +20,20 @@
2920
#define WIFI_CONNECTED_BIT BIT0
3021
#define WIFI_FAIL_BIT BIT1
3122
#define WIFI_BITS (WIFI_CONNECTED_BIT | WIFI_FAIL_BIT)
32-
#define WIFI_REMOTE_CONNECTED_BIT BIT2
33-
#define WIFI_REMOTE_FAIL_BIT BIT3
34-
#define WIFI_REMOTE_BITS (WIFI_REMOTE_CONNECTED_BIT | WIFI_REMOTE_FAIL_BIT)
3523
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
3624

3725
static int s_retry_num = 0;
3826
static EventGroupHandle_t s_wifi_event_group;
3927

28+
#if CONFIG_ESP_WIFI_REMOTE_ENABLE
29+
/* Remote Wi-Fi is defined in another compilation unit,
30+
* as wifi_remote API uses the same type names as local WiFi
31+
* and since the types might be slightly different for different
32+
* targets, we cannot combine both in a single compilation unit
33+
*/
34+
void wifi_init_remote_sta(void);
35+
#endif
36+
4037
#if CONFIG_ESP_WIFI_LOCAL_ENABLE
4138
static const char *TAG_local = "two_stations_local";
4239

@@ -67,32 +64,6 @@ static void event_handler(void* arg, esp_event_base_t event_base,
6764
}
6865
#endif
6966

70-
#if CONFIG_ESP_WIFI_REMOTE_ENABLE
71-
static const char *TAG_remote = "two_stations_remote";
72-
73-
static void event_handler_remote(void* arg, esp_event_base_t event_base,
74-
int32_t event_id, void* event_data)
75-
{
76-
if (event_base == WIFI_REMOTE_EVENT && event_id == WIFI_EVENT_STA_START) {
77-
esp_wifi_remote_connect();
78-
} else if (event_base == WIFI_REMOTE_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
79-
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
80-
esp_wifi_remote_connect();
81-
s_retry_num++;
82-
ESP_LOGI(TAG_remote, "retry to connect to the AP");
83-
} else {
84-
xEventGroupSetBits(s_wifi_event_group, WIFI_REMOTE_FAIL_BIT);
85-
}
86-
ESP_LOGI(TAG_remote,"connect to the AP fail");
87-
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
88-
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
89-
ESP_LOGI(TAG_remote, "Remote Wi-Fi got IP:" IPSTR, IP2STR(&event->ip_info.ip));
90-
s_retry_num = 0;
91-
xEventGroupSetBits(s_wifi_event_group, WIFI_REMOTE_CONNECTED_BIT);
92-
}
93-
}
94-
#endif
95-
9667
static void init_system_components(void)
9768
{
9869
s_wifi_event_group = xEventGroupCreate();
@@ -137,42 +108,6 @@ static void wifi_init_sta(void)
137108
}
138109
#endif
139110

140-
#if CONFIG_ESP_WIFI_REMOTE_ENABLE
141-
static void wifi_init_remote_sta(void)
142-
{
143-
esp_wifi_remote_create_default_sta();
144-
145-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
146-
ESP_ERROR_CHECK(esp_wifi_remote_init(&cfg));
147-
148-
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_REMOTE_EVENT, ESP_EVENT_ANY_ID, event_handler_remote, NULL));
149-
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler_remote, NULL));
150-
151-
wifi_config_t wifi_config = {
152-
.sta = {
153-
.ssid = CONFIG_ESP_WIFI_REMOTE_SSID,
154-
.password = CONFIG_ESP_WIFI_REMOTE_PASSWORD,
155-
},
156-
};
157-
ESP_ERROR_CHECK(esp_wifi_remote_set_mode(WIFI_MODE_STA));
158-
ESP_ERROR_CHECK(esp_wifi_remote_set_config(WIFI_IF_STA, &wifi_config));
159-
ESP_ERROR_CHECK(esp_wifi_remote_start());
160-
161-
ESP_LOGI(TAG_remote, "wifi_init_remote_sta finished.");
162-
163-
/* Waiting until either the connection is established the same way for REMOTE wifi */
164-
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_REMOTE_BITS, pdFALSE, pdFALSE, portMAX_DELAY);
165-
166-
if (bits & WIFI_REMOTE_CONNECTED_BIT) {
167-
ESP_LOGI(TAG_remote, "connected to ap SSID:%s", CONFIG_ESP_WIFI_REMOTE_SSID);
168-
} else if (bits & WIFI_REMOTE_FAIL_BIT) {
169-
ESP_LOGW(TAG_remote, "Failed to connect to SSID:%s", CONFIG_ESP_WIFI_REMOTE_SSID);
170-
} else {
171-
ESP_LOGE(TAG_remote, "UNEXPECTED EVENT");
172-
}
173-
}
174-
#endif
175-
176111
void app_main(void)
177112
{
178113
esp_log_level_set("*", ESP_LOG_INFO);

0 commit comments

Comments
 (0)