Merge branch 'bugfix/mesh_manual_networking_example' into 'master'
example: fix mesh manual networking example See merge request espressif/esp-idf!6869
This commit is contained in:
commit
ce507da514
|
@ -2,11 +2,8 @@
|
||||||
|
|
||||||
ESP-MESH provides the function of self-organized networking, but if users disable this function on one node, users must designate a parent for this node.
|
ESP-MESH provides the function of self-organized networking, but if users disable this function on one node, users must designate a parent for this node.
|
||||||
|
|
||||||
|
|
||||||
This example demonstrates how to scan a list of parent candidates, choose an appropriate parent and set as the parent of this node.
|
This example demonstrates how to scan a list of parent candidates, choose an appropriate parent and set as the parent of this node.
|
||||||
|
|
||||||
If no parent is found through this scan, enable the self-organized function to let the ESP-MESH handle it by itself.
|
|
||||||
|
|
||||||
Open project configuration menu (`idf.py menuconfig`) to configure the mesh network channel, router SSID, router password and mesh softAP settings.
|
Open project configuration menu (`idf.py menuconfig`) to configure the mesh network channel, router SSID, router password and mesh softAP settings.
|
||||||
|
|
||||||
When the mesh network is established and if you happen to run this example on ESP-WROVER-KIT boards, the RGB light indicator will show you on which layer devices are. The pink reprents root; the yellow reprents layer 2; the red reprents layer 3; the blue reprents layer 4; the green reprents layer 5; the white reprents layer greater than 5.
|
When the mesh network is established and if you happen to run this example on ESP-WROVER-KIT boards, the RGB light indicator will show you on which layer devices are. The pink reprents root; the yellow reprents layer 2; the red reprents layer 3; the blue reprents layer 4; the green reprents layer 5; the white reprents layer greater than 5.
|
||||||
|
|
|
@ -1,5 +1,17 @@
|
||||||
menu "Example Configuration"
|
menu "Example Configuration"
|
||||||
|
|
||||||
|
choice
|
||||||
|
bool "Device type"
|
||||||
|
default MESH_SET_ROOT
|
||||||
|
help
|
||||||
|
Device type (root or node).
|
||||||
|
|
||||||
|
config MESH_SET_ROOT
|
||||||
|
bool "MESH_SET_ROOT"
|
||||||
|
config MESH_SET_NODE
|
||||||
|
bool "MESH_SET_NODE"
|
||||||
|
endchoice
|
||||||
|
|
||||||
config MESH_CHANNEL
|
config MESH_CHANNEL
|
||||||
int "channel"
|
int "channel"
|
||||||
range 1 14
|
range 1 14
|
||||||
|
|
|
@ -19,11 +19,6 @@
|
||||||
/*******************************************************
|
/*******************************************************
|
||||||
* Macros
|
* Macros
|
||||||
*******************************************************/
|
*******************************************************/
|
||||||
//#define MESH_SET_ROOT
|
|
||||||
|
|
||||||
#ifndef MESH_SET_ROOT
|
|
||||||
#define MESH_SET_NODE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*******************************************************
|
/*******************************************************
|
||||||
* Constants
|
* Constants
|
||||||
|
@ -70,7 +65,7 @@ void mesh_scan_done_handler(int num)
|
||||||
assoc.assoc_cap, assoc.layer2_cap, MAC2STR(record.bssid),
|
assoc.assoc_cap, assoc.layer2_cap, MAC2STR(record.bssid),
|
||||||
record.primary, record.rssi, MAC2STR(assoc.mesh_id), assoc.encrypted ? "IE Encrypted" : "IE Unencrypted");
|
record.primary, record.rssi, MAC2STR(assoc.mesh_id), assoc.encrypted ? "IE Encrypted" : "IE Unencrypted");
|
||||||
|
|
||||||
#ifdef MESH_SET_NODE
|
#if CONFIG_MESH_SET_NODE
|
||||||
if (assoc.mesh_type != MESH_IDLE && assoc.layer_cap
|
if (assoc.mesh_type != MESH_IDLE && assoc.layer_cap
|
||||||
&& assoc.assoc < assoc.assoc_cap && record.rssi > -70) {
|
&& assoc.assoc < assoc.assoc_cap && record.rssi > -70) {
|
||||||
if (assoc.layer < parent_assoc.layer || assoc.layer2_cap < parent_assoc.layer2_cap) {
|
if (assoc.layer < parent_assoc.layer || assoc.layer2_cap < parent_assoc.layer2_cap) {
|
||||||
|
@ -91,7 +86,7 @@ void mesh_scan_done_handler(int num)
|
||||||
ESP_LOGI(MESH_TAG, "[%d]%s, "MACSTR", channel:%u, rssi:%d", i,
|
ESP_LOGI(MESH_TAG, "[%d]%s, "MACSTR", channel:%u, rssi:%d", i,
|
||||||
record.ssid, MAC2STR(record.bssid), record.primary,
|
record.ssid, MAC2STR(record.bssid), record.primary,
|
||||||
record.rssi);
|
record.rssi);
|
||||||
#ifdef MESH_SET_ROOT
|
#if CONFIG_MESH_SET_ROOT
|
||||||
if (!strcmp(CONFIG_MESH_ROUTER_SSID, (char *) record.ssid)) {
|
if (!strcmp(CONFIG_MESH_ROUTER_SSID, (char *) record.ssid)) {
|
||||||
parent_found = true;
|
parent_found = true;
|
||||||
memcpy(&parent_record, &record, sizeof(record));
|
memcpy(&parent_record, &record, sizeof(record));
|
||||||
|
@ -112,7 +107,6 @@ void mesh_scan_done_handler(int num)
|
||||||
sizeof(parent_record.ssid));
|
sizeof(parent_record.ssid));
|
||||||
parent.sta.bssid_set = 1;
|
parent.sta.bssid_set = 1;
|
||||||
memcpy(&parent.sta.bssid, parent_record.bssid, 6);
|
memcpy(&parent.sta.bssid, parent_record.bssid, 6);
|
||||||
ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(parent_record.authmode));
|
|
||||||
if (my_type == MESH_ROOT) {
|
if (my_type == MESH_ROOT) {
|
||||||
if (parent_record.authmode != WIFI_AUTH_OPEN) {
|
if (parent_record.authmode != WIFI_AUTH_OPEN) {
|
||||||
memcpy(&parent.sta.password, CONFIG_MESH_ROUTER_PASSWD,
|
memcpy(&parent.sta.password, CONFIG_MESH_ROUTER_PASSWD,
|
||||||
|
@ -121,7 +115,9 @@ void mesh_scan_done_handler(int num)
|
||||||
ESP_LOGW(MESH_TAG, "<PARENT>%s, "MACSTR", channel:%u, rssi:%d",
|
ESP_LOGW(MESH_TAG, "<PARENT>%s, "MACSTR", channel:%u, rssi:%d",
|
||||||
parent_record.ssid, MAC2STR(parent_record.bssid),
|
parent_record.ssid, MAC2STR(parent_record.bssid),
|
||||||
parent_record.primary, parent_record.rssi);
|
parent_record.primary, parent_record.rssi);
|
||||||
|
ESP_ERROR_CHECK(esp_mesh_set_parent(&parent, NULL, my_type, my_layer));
|
||||||
} else {
|
} else {
|
||||||
|
ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(parent_record.authmode));
|
||||||
if (parent_record.authmode != WIFI_AUTH_OPEN) {
|
if (parent_record.authmode != WIFI_AUTH_OPEN) {
|
||||||
memcpy(&parent.sta.password, CONFIG_MESH_AP_PASSWD,
|
memcpy(&parent.sta.password, CONFIG_MESH_AP_PASSWD,
|
||||||
strlen(CONFIG_MESH_AP_PASSWD));
|
strlen(CONFIG_MESH_AP_PASSWD));
|
||||||
|
@ -133,22 +129,10 @@ void mesh_scan_done_handler(int num)
|
||||||
parent_assoc.assoc_cap, parent_assoc.layer2_cap,
|
parent_assoc.assoc_cap, parent_assoc.layer2_cap,
|
||||||
MAC2STR(parent_record.bssid), parent_record.primary,
|
MAC2STR(parent_record.bssid), parent_record.primary,
|
||||||
parent_record.rssi);
|
parent_record.rssi);
|
||||||
}
|
|
||||||
ESP_ERROR_CHECK(esp_mesh_set_parent(&parent, (mesh_addr_t *)&parent_assoc.mesh_id, my_type, my_layer));
|
ESP_ERROR_CHECK(esp_mesh_set_parent(&parent, (mesh_addr_t *)&parent_assoc.mesh_id, my_type, my_layer));
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGW(MESH_TAG,
|
|
||||||
"<Warning>no parent found, modify IE crypto configuration and scan");
|
|
||||||
if (CONFIG_MESH_IE_CRYPTO_FUNCS) {
|
|
||||||
/* modify IE crypto key */
|
|
||||||
ESP_LOGW(MESH_TAG, "<Config>modify IE crypto key to %s", CONFIG_MESH_IE_CRYPTO_KEY);
|
|
||||||
ESP_ERROR_CHECK(esp_mesh_set_ie_crypto_funcs(&g_wifi_default_mesh_crypto_funcs));
|
|
||||||
ESP_ERROR_CHECK(esp_mesh_set_ie_crypto_key(CONFIG_MESH_IE_CRYPTO_KEY, strlen(CONFIG_MESH_IE_CRYPTO_KEY)));
|
|
||||||
} else {
|
|
||||||
/* disable IE crypto */
|
|
||||||
ESP_LOGW(MESH_TAG, "<Config>disable IE crypto");
|
|
||||||
ESP_ERROR_CHECK(esp_mesh_set_ie_crypto_funcs(NULL));
|
|
||||||
}
|
|
||||||
esp_wifi_scan_stop();
|
esp_wifi_scan_stop();
|
||||||
scan_config.show_hidden = 1;
|
scan_config.show_hidden = 1;
|
||||||
scan_config.scan_type = WIFI_SCAN_TYPE_PASSIVE;
|
scan_config.scan_type = WIFI_SCAN_TYPE_PASSIVE;
|
||||||
|
@ -160,7 +144,7 @@ void mesh_event_handler(void *arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void *event_data)
|
int32_t event_id, void *event_data)
|
||||||
{
|
{
|
||||||
mesh_addr_t id = {0,};
|
mesh_addr_t id = {0,};
|
||||||
static uint8_t last_layer = 0;
|
static int last_layer = 0;
|
||||||
wifi_scan_config_t scan_config = { 0 };
|
wifi_scan_config_t scan_config = { 0 };
|
||||||
|
|
||||||
switch (event_id) {
|
switch (event_id) {
|
||||||
|
@ -284,7 +268,7 @@ void mesh_event_handler(void *arg, esp_event_base_t event_base,
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ESP_LOGI(MESH_TAG, "unknown id:%d", event_id);
|
ESP_LOGD(MESH_TAG, "event id:%d", event_id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -311,12 +295,22 @@ void app_main(void)
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&config));
|
ESP_ERROR_CHECK(esp_wifi_init(&config));
|
||||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL));
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL));
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
/* mesh initialization */
|
/* mesh initialization */
|
||||||
ESP_ERROR_CHECK(esp_mesh_init());
|
ESP_ERROR_CHECK(esp_mesh_init());
|
||||||
ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL));
|
ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL));
|
||||||
/* mesh enable IE crypto */
|
/* mesh enable IE crypto */
|
||||||
mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
|
mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
|
||||||
|
#if CONFIG_MESH_IE_CRYPTO_FUNCS
|
||||||
|
/* modify IE crypto key */
|
||||||
|
ESP_ERROR_CHECK(esp_mesh_set_ie_crypto_funcs(&g_wifi_default_mesh_crypto_funcs));
|
||||||
|
ESP_ERROR_CHECK(esp_mesh_set_ie_crypto_key(CONFIG_MESH_IE_CRYPTO_KEY, strlen(CONFIG_MESH_IE_CRYPTO_KEY)));
|
||||||
|
#else
|
||||||
|
/* disable IE crypto */
|
||||||
|
ESP_LOGI(MESH_TAG, "<Config>disable IE crypto");
|
||||||
|
ESP_ERROR_CHECK(esp_mesh_set_ie_crypto_funcs(NULL));
|
||||||
|
#endif
|
||||||
/* mesh ID */
|
/* mesh ID */
|
||||||
memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
|
memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
|
||||||
/* router */
|
/* router */
|
||||||
|
|
Loading…
Reference in a new issue