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:
Jiang Jiang Jian 2020-05-19 14:05:51 +08:00
commit ce507da514
3 changed files with 34 additions and 31 deletions

View file

@ -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.
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.
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.

View file

@ -1,5 +1,17 @@
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
int "channel"
range 1 14

View file

@ -19,11 +19,6 @@
/*******************************************************
* Macros
*******************************************************/
//#define MESH_SET_ROOT
#ifndef MESH_SET_ROOT
#define MESH_SET_NODE
#endif
/*******************************************************
* Constants
@ -70,7 +65,7 @@ void mesh_scan_done_handler(int num)
assoc.assoc_cap, assoc.layer2_cap, MAC2STR(record.bssid),
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
&& assoc.assoc < assoc.assoc_cap && record.rssi > -70) {
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,
record.ssid, MAC2STR(record.bssid), record.primary,
record.rssi);
#ifdef MESH_SET_ROOT
#if CONFIG_MESH_SET_ROOT
if (!strcmp(CONFIG_MESH_ROUTER_SSID, (char *) record.ssid)) {
parent_found = true;
memcpy(&parent_record, &record, sizeof(record));
@ -112,7 +107,6 @@ void mesh_scan_done_handler(int num)
sizeof(parent_record.ssid));
parent.sta.bssid_set = 1;
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 (parent_record.authmode != WIFI_AUTH_OPEN) {
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",
parent_record.ssid, MAC2STR(parent_record.bssid),
parent_record.primary, parent_record.rssi);
ESP_ERROR_CHECK(esp_mesh_set_parent(&parent, NULL, my_type, my_layer));
} else {
ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(parent_record.authmode));
if (parent_record.authmode != WIFI_AUTH_OPEN) {
memcpy(&parent.sta.password, 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,
MAC2STR(parent_record.bssid), parent_record.primary,
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 {
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();
scan_config.show_hidden = 1;
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)
{
mesh_addr_t id = {0,};
static uint8_t last_layer = 0;
static int last_layer = 0;
wifi_scan_config_t scan_config = { 0 };
switch (event_id) {
@ -284,7 +268,7 @@ void mesh_event_handler(void *arg, esp_event_base_t event_base,
}
break;
default:
ESP_LOGI(MESH_TAG, "unknown id:%d", event_id);
ESP_LOGD(MESH_TAG, "event id:%d", event_id);
break;
}
}
@ -300,23 +284,33 @@ void app_main(void)
{
ESP_ERROR_CHECK(mesh_light_init());
ESP_ERROR_CHECK(nvs_flash_init());
/* tcpip initialization */
/* tcpip initialization */
ESP_ERROR_CHECK(esp_netif_init());
/* event initialization */
/* event initialization */
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* crete network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */
/* crete network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */
ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL));
/* wifi initialization */
/* wifi initialization */
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
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_wifi_set_storage(WIFI_STORAGE_FLASH));
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
ESP_ERROR_CHECK(esp_wifi_start());
/* mesh initialization */
/* mesh initialization */
ESP_ERROR_CHECK(esp_mesh_init());
ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL));
/* mesh enable IE crypto */
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 */
memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
/* router */