Skip to content

Commit 91621a2

Browse files
committed
Merge branch 'feature/add_auto_connect_demo' into 'main'
Add auto enter room when Wifi connected See merge request adf/esp-webrtc-solution!15
2 parents 1958ec4 + 03bd222 commit 91621a2

File tree

9 files changed

+145
-27
lines changed

9 files changed

+145
-27
lines changed

components/esp_webrtc/impl/apprtc_signal/signal_default.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,6 @@ static int wss_signal_send_msg(esp_peer_signaling_handle_t h, esp_peer_signaling
648648
cJSON_Delete(json);
649649
int ret = https_post(request_room, NULL, payload, NULL, NULL);
650650
free(payload);
651-
if (msg->type == ESP_PEER_SIGNALING_MSG_SDP) {
652-
snprintf(request_room, 128, "%s/r/%s", sg->client_info.base_url, sg->client_info.room_id);
653-
ESP_LOGI(TAG, "Please use browser to connect in: %s", request_room);
654-
}
655651
return ret;
656652
}
657653

solutions/common/network.c

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,65 @@ bool network_is_connected(void)
4646

4747
#ifndef CONFIG_NETWORK_USE_ETHERNET
4848

49+
static wifi_config_t wifi_config;
50+
51+
#define PART_NAME "wifi-set"
52+
#define WIFI_SSID_KEY "ssid"
53+
#define WIFI_PSW_KEY "psw"
54+
55+
static bool load_from_nvs(void)
56+
{
57+
nvs_handle_t wifi_nvs = 0;
58+
bool load_ok = false;
59+
do {
60+
esp_err_t ret = nvs_open(PART_NAME, NVS_READWRITE, &wifi_nvs);
61+
if (ret != ESP_OK) {
62+
ESP_LOGE(TAG, "Fail to open nvs ret %d", ret);
63+
break;
64+
}
65+
size_t size = sizeof(wifi_config.sta.ssid);
66+
ret = nvs_get_str(wifi_nvs, WIFI_SSID_KEY, (char*)(wifi_config.sta.ssid), &size);
67+
if (ret != ESP_OK) {
68+
break;
69+
}
70+
wifi_config.sta.ssid[size] = '\0';
71+
size = sizeof(wifi_config.sta.password);
72+
ret = nvs_get_str(wifi_nvs, WIFI_PSW_KEY, (char*)(wifi_config.sta.password), &size);
73+
if (ret != ESP_OK) {
74+
break;
75+
}
76+
wifi_config.sta.password[size] = '\0';
77+
load_ok = true;
78+
} while (0);
79+
if (wifi_nvs) {
80+
nvs_close(wifi_nvs);
81+
}
82+
return load_ok;
83+
}
84+
85+
static void store_to_nvs(void)
86+
{
87+
nvs_handle_t wifi_nvs = 0;
88+
do {
89+
esp_err_t ret = nvs_open(PART_NAME, NVS_READWRITE, &wifi_nvs);
90+
if (ret != ESP_OK) {
91+
ESP_LOGE(TAG, "Fail to open nvs ret %d", ret);
92+
break;
93+
}
94+
ret = nvs_set_str(wifi_nvs, WIFI_SSID_KEY, (char*)(wifi_config.sta.ssid));
95+
if (ret != ESP_OK) {
96+
break;
97+
}
98+
ret = nvs_set_str(wifi_nvs, WIFI_PSW_KEY, (char*)(wifi_config.sta.password));
99+
if (ret != ESP_OK) {
100+
break;
101+
}
102+
} while (0);
103+
if (wifi_nvs) {
104+
nvs_close(wifi_nvs);
105+
}
106+
}
107+
49108
static void event_handler(void *arg, esp_event_base_t event_base,
50109
int32_t event_id, void *event_data)
51110
{
@@ -59,6 +118,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
59118
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
60119
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
61120
network_set_connected(true);
121+
store_to_nvs();
62122
}
63123
}
64124

@@ -84,16 +144,16 @@ int network_init(const char *ssid, const char *password, network_connect_cb cb)
84144
&event_handler,
85145
NULL,
86146
&instance_got_ip));
87-
wifi_config_t wifi_config = {
88-
.sta = {
89-
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
90-
},
91-
};
92-
if (ssid) {
93-
memcpy(wifi_config.sta.ssid, ssid, strlen(ssid) + 1);
94-
}
95-
if (password) {
96-
memcpy(wifi_config.sta.password, password, strlen(password) + 1);
147+
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
148+
if (load_from_nvs()) {
149+
ESP_LOGI(TAG, "Force to use wifi config from nvs");
150+
} else {
151+
if (ssid) {
152+
memcpy(wifi_config.sta.ssid, ssid, strlen(ssid) + 1);
153+
}
154+
if (password) {
155+
memcpy(wifi_config.sta.password, password, strlen(password) + 1);
156+
}
97157
}
98158
connect_cb = cb;
99159
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
@@ -104,19 +164,22 @@ int network_init(const char *ssid, const char *password, network_connect_cb cb)
104164
return 0;
105165
}
106166

167+
int network_get_mac(uint8_t mac[6])
168+
{
169+
esp_wifi_get_mac(WIFI_IF_STA, mac);
170+
return 0;
171+
}
172+
107173
int network_connect_wifi(const char *ssid, const char *password)
108174
{
109-
wifi_config_t wifi_config = {
110-
.sta = {
111-
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
112-
},
113-
};
175+
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
114176
if (ssid) {
115177
memcpy(wifi_config.sta.ssid, ssid, strlen(ssid) + 1);
116178
}
117179
if (password) {
118180
memcpy(wifi_config.sta.password, password, strlen(password) + 1);
119181
}
182+
network_connected = false;
120183
esp_wifi_disconnect();
121184
esp_wifi_stop();
122185
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
@@ -125,7 +188,7 @@ int network_connect_wifi(const char *ssid, const char *password)
125188
}
126189

127190
#else
128-
191+
static esp_eth_handle_t *eth_handles = NULL;
129192
static void eth_event_handler(void *arg, esp_event_base_t event_base,
130193
int32_t event_id, void *event_data)
131194
{
@@ -139,7 +202,6 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
139202
ESP_LOGI(TAG, "Ethernet Link Up");
140203
ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
141204
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
142-
network_set_connected(true);
143205
break;
144206
case ETHERNET_EVENT_DISCONNECTED:
145207
ESP_LOGI(TAG, "Ethernet Link Down");
@@ -169,13 +231,13 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
169231
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
170232
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
171233
ESP_LOGI(TAG, "~~~~~~~~~~~");
234+
network_set_connected(true);
172235
}
173236

174237
int network_init(const char *ssid, const char *password, network_connect_cb cb)
175238
{
176239
// Initialize Ethernet driver
177240
uint8_t eth_port_cnt = 0;
178-
esp_eth_handle_t *eth_handles;
179241
ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt));
180242

181243
// Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
@@ -227,6 +289,14 @@ int network_init(const char *ssid, const char *password, network_connect_cb cb)
227289
return 0;
228290
}
229291

292+
int network_get_mac(uint8_t mac[6])
293+
{
294+
if (eth_handles) {
295+
esp_eth_ioctl(eth_handles[0], ETH_CMD_G_MAC_ADDR, mac);
296+
}
297+
return 0;
298+
}
299+
230300
int network_connect_wifi(const char *ssid, const char *password)
231301
{
232302
ESP_LOGE(TAG, "Using ethernet now not support wifi config");

solutions/common/network.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ typedef int (*network_connect_cb)(bool connected);
3333
*/
3434
int network_init(const char *ssid, const char *password, network_connect_cb cb);
3535

36+
/**
37+
* @brief Get current network mac
38+
*
39+
* @param[out] mac Network mac to store
40+
*
41+
* @return
42+
* - 0 On success
43+
* - Others Fail to initialized
44+
*/
45+
int network_get_mac(uint8_t mac[6]);
46+
3647
/**
3748
* @brief Check network connected or not
3849
*

solutions/doorbell_demo/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ After the board boots up, it will attempt to connect to the configured Wi-Fi SSI
3434
wifi ssid psw
3535
```
3636

37-
Once the Wi-Fi is connected successfully, use the `join` command to join a random room. Make sure the room is empty before entering:
37+
Once connected to Wi-Fi, the board will automatically try to join a room generated from its MAC address. The room name will appear in the console:
3838
```
39-
join mytestroom
39+
W (9801) Webrtc_Test: Please use browser to join in espxxxxxx on https://webrtc.espressif.cn/doorbell
40+
```
41+
User also can use `leave` command leave the room and use the `join` command to join other random room. Make sure the room is empty before entering:
42+
```
43+
join random_room_id
44+
```
45+
If you're the first to join, you’ll see:
46+
```
47+
Initials set to 1
4048
```
4149

4250
Then, use a Chrome/Edge browser to enter the same room at [DoorBellDemo](https://webrtc.espressif.cn/doorbell).
@@ -64,3 +72,8 @@ Then, use a Chrome/Edge browser to enter the same room at [DoorBellDemo](https:/
6472
- **`esp_webrtc_enable_peer_connection` API**: A new API is introduced to manually control the connection and disconnection of the peer connection.
6573

6674
All other steps follow the typical call flow of `esp_webrtc`. For more details on the standard connection build flow, refer to the [Connection Build Flow](../../components/esp_webrtc/README.md#typical-call-sequence-of-esp_webrtc).
75+
76+
### QA
77+
- If the board unexpectedly leaves the room, fail to re-enter same room.
78+
Server will keep the room for 1-2 minutes before timing out. The user must wait for the timeout to expire before retrying.
79+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
idf_component_register(SRCS "webrtc.c" "main.c" "board.c" "media_sys.c"
2-
EMBED_TXTFILES "ring.aac" "open.aac"
2+
EMBED_TXTFILES "ring.aac" "open.aac" "join.aac"
33
INCLUDE_DIRS ".")

solutions/doorbell_demo/main/join.aac

15.1 KB
Binary file not shown.

solutions/doorbell_demo/main/main.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ static void thread_scheduler(const char *thread_name, media_lib_thread_cfg_t *th
189189
thread_cfg->priority = 18;
190190
thread_cfg->core_id = 1;
191191
}
192+
if (strcmp(thread_name, "start") == 0) {
193+
thread_cfg->stack_size = 6 * 1024;
194+
}
192195
if (strcmp(thread_name, "venc") == 0) {
193196
#if CONFIG_IDF_TARGET_ESP32S3
194197
thread_cfg->stack_size = 20 * 1024;
@@ -203,9 +206,28 @@ static void thread_scheduler(const char *thread_name, media_lib_thread_cfg_t *th
203206
#endif
204207
}
205208

209+
static char* gen_room_id_use_mac(void)
210+
{
211+
static char room_mac[16];
212+
uint8_t mac[6];
213+
network_get_mac(mac);
214+
snprintf(room_mac, sizeof(room_mac)-1, "esp_%02x%02x%02x", mac[3], mac[4], mac[5]);
215+
return room_mac;
216+
}
217+
206218
static int network_event_handler(bool connected)
207219
{
208-
if (connected == false) {
220+
if (connected) {
221+
// Enter into Room directly
222+
RUN_ASYNC(start, {
223+
char *room = gen_room_id_use_mac();
224+
snprintf(room_url, sizeof(room_url), "https://webrtc.espressif.cn/join/%s", room);
225+
ESP_LOGI(TAG, "Start to join in room %s", room);
226+
if (start_webrtc(room_url) == 0) {
227+
ESP_LOGW(TAG, "Please use browser to join in %s on https://webrtc.espressif.cn/doorbell", room);
228+
}
229+
});
230+
} else {
209231
stop_webrtc();
210232
}
211233
return 0;

solutions/doorbell_demo/main/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern "C" {
4747
* When enable `NETWORK_USE_ETHERNET` will cause socket error
4848
* User must replace it to a unused GPIO instead (like GPIO27)
4949
*/
50-
#define DOOR_BELL_RING_BUTTON 27
50+
#define DOOR_BELL_RING_BUTTON 35
5151

5252
#ifdef __cplusplus
5353
}

solutions/doorbell_demo/main/webrtc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef enum {
3838
typedef enum {
3939
DOOR_BELL_TONE_RING,
4040
DOOR_BELL_TONE_OPEN_DOOR,
41+
DOOR_BELL_TONE_JOIN_SUCCESS,
4142
} door_bell_tone_type_t;
4243

4344
typedef struct {
@@ -54,12 +55,15 @@ extern const uint8_t ring_music_start[] asm("_binary_ring_aac_start");
5455
extern const uint8_t ring_music_end[] asm("_binary_ring_aac_end");
5556
extern const uint8_t open_music_start[] asm("_binary_open_aac_start");
5657
extern const uint8_t open_music_end[] asm("_binary_open_aac_end");
58+
extern const uint8_t join_music_start[] asm("_binary_join_aac_start");
59+
extern const uint8_t join_music_end[] asm("_binary_join_aac_end");
5760

5861
static int play_tone(door_bell_tone_type_t type)
5962
{
6063
door_bell_tone_data_t tone_data[] = {
6164
{ ring_music_start, ring_music_end, 4000 },
6265
{ open_music_start, open_music_end, 0 },
66+
{ join_music_start, join_music_end, 0 },
6367
};
6468
if (type >= sizeof(tone_data) / sizeof(tone_data[0])) {
6569
return 0;
@@ -225,6 +229,8 @@ int start_webrtc(char *url)
225229
ret = esp_webrtc_start(webrtc);
226230
if (ret != 0) {
227231
ESP_LOGE(TAG, "Fail to start webrtc");
232+
} else {
233+
play_tone(DOOR_BELL_TONE_JOIN_SUCCESS);
228234
}
229235
return ret;
230236
}

0 commit comments

Comments
 (0)