I'm also trying to build a routerless mesh network (using tree topology, with esp-idf v4.2). It seems that I have the network forming with a chosen root device, but I'm not getting messages through. Debug shows that devices are connecting. See below:
I (1882) wifi: new:<1,1>, old:<1,0>, ap:<1,1>, sta:<0,0>, prof:1
I (1892) wifi: station: 10:52:1c:66:98:60 join, AID=1, bgn, 40U
I (1892) wifi: new:<1,1>, old:<1,1>, ap:<1,1>, sta:<0,0>, prof:1
I (1902) wifi: station: 10:52:1c:69:48:00 join, AID=2, bgn, 40U
I (1912) mesh_main: <MESH_EVENT_PS_CHILD_DUTY>cidx:0, 10:52:1c:66:98:60, duty:12
I (1912) mesh_main: <MESH_EVENT_PS_CHILD_DUTY>cidx:1, 10:52:1c:69:48:00, duty:12
W (1922) mesh_main: <MESH_EVENT_ROUTING_TABLE_ADD>add 1, new:2, layer:1
I (1932) mesh_main: <MESH_EVENT_CHILD_CONNECTED>aid:1, 10:52:1c:66:98:60
W (1932) mesh_main: <MESH_EVENT_ROUTING_TABLE_ADD>add 1, new:3, layer:1
I (1942) mesh_main: <MESH_EVENT_CHILD_CONNECTED>aid:2, 10:52:1c:69:48:00
I'd like to start by sending JSON strings from the nodes to the root device. Instead of seeing the JSON strings, I'm getting this at the root debug:
I (9362) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (9362) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (9372) mesh: [RXQ]<max:128 = cfg:128 + extra:0>self:0, <max:128 = cfg:128 + extra:0>tods:0
I (10732) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (10732) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (10752) mesh: [RXQ]<max:128 = cfg:128 + extra:0>self:0, <max:128 = cfg:128 + extra:0>tods:0
I (11032) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (11032) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (11042) mesh: [RXQ]<max:128 = cfg:128 + extra:0>self:0, <max:128 = cfg:128 + extra:0>tods:0
I (11212) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (11212) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (11222) mesh: [RXQ]<max:128 = cfg:128 + extra:0>self:0, <max:128 = cfg:128 + extra:0>tods:0
I (12542) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (12542) mesh: [TXQ]<max:128>up(0, be:0), down(0, be:0), mgmt:0, xon(req:0, rsp:12), bcast:0, wnd(0, parent:00:00:00:00:00:00)
I (12552) mesh: [RXQ]<max:128 = cfg:128 + extra:0>self:0, <max:128 = cfg:128 + extra:0>tods:0
I am starting with the
esp-idf/examples/mesh/internal_communication/ example.
This is the edited/added code I'm using. Is there anyone else trying to do the same, with some insight, or interested in working through this with me?
Code: Select all
// #define ROUTER_DEVICE //uncomment for normal router linked mesh
#define ROOT_DEVICE //uncomment for root device
// #define NODE_DEVICE //uncomment for non-root devices
void root_task(void)
{
ESP_LOGI(MESH_TAG, "ROOT TASK EVENT");
// int recv_count = 0;
esp_err_t err;
mesh_addr_t from;
mesh_data_t data;
int flag = 0;
data.data = rx_buf;
data.size = RX_SIZE;
data.proto = MESH_PROTO_JSON;
data.tos = MESH_TOS_P2P;
err = esp_mesh_recv(&from, &data, portMAX_DELAY, &flag, NULL, 0);
if (err == ESP_OK && data.size) {
ESP_LOGW(MESH_TAG, "GOT MESSAGE FROM NODE "MACSTR" :%s", MAC2STR(from.addr), data.data);
}else{
ESP_LOGE(MESH_TAG, "err:0x%x, size:%d", err, data.size);
}
}
void node_task(void)
{
mesh_data_t data_out;
uint8_t buffer[20] = "{\"Hello\":\"World\"}";
data_out.data = buffer;
data_out.size = sizeof(buffer);
data_out.proto = MESH_PROTO_JSON;
data_out.tos = MESH_TOS_P2P; //with retransmission
// data_out.tos = MESH_TOS_DEF; //no retransmission
ESP_LOGI(MESH_TAG, "NODE TASK EVENT %s", data_out.data);
esp_mesh_send(NULL, &data_out, 0, NULL, 1);
}
void app_main(void)
{
ESP_ERROR_CHECK(mesh_light_init());
ESP_ERROR_CHECK(nvs_flash_init());
/* tcpip initialization */
ESP_ERROR_CHECK(esp_netif_init());
/* event initialization */
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* create 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_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));
#ifdef ROOT_DEVICE
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_ps(WIFI_PS_NONE));
#endif
#ifdef NODE_DEVICE
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_ps(WIFI_PS_NONE));
#endif
ESP_ERROR_CHECK(esp_wifi_start());
/* 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));
#ifdef ROOT_DEVICE
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_mesh_set_type(MESH_ROOT));
#endif
#ifdef NODE_DEVICE
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_mesh_fix_root(true));
#endif
/* set mesh topology */
ESP_ERROR_CHECK(esp_mesh_set_topology(CONFIG_MESH_TOPOLOGY));
/* set mesh max layer according to the topology */
ESP_ERROR_CHECK(esp_mesh_set_max_layer(CONFIG_MESH_MAX_LAYER));
ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
ESP_ERROR_CHECK(esp_mesh_set_xon_qsize(128));
#ifdef CONFIG_MESH_ENABLE_PS
/* Enable mesh PS function */
ESP_ERROR_CHECK(esp_mesh_enable_ps());
/* better to increase the associate expired time, if a small duty cycle is set. */
ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(60));
/* better to increase the announce interval to avoid too much management traffic, if a small duty cycle is set. */
ESP_ERROR_CHECK(esp_mesh_set_announce_interval(600, 3300));
#else
/* Disable mesh PS function */
ESP_ERROR_CHECK(esp_mesh_disable_ps());
ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
#endif
mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
/* mesh ID */
memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
cfg.channel = CONFIG_MESH_CHANNEL;
#ifdef ROUTER_DEVICE
/* router */
// cfg.channel = CONFIG_MESH_CHANNEL;
cfg.router.ssid_len = strlen(CONFIG_MESH_ROUTER_SSID);
memcpy((uint8_t *) &cfg.router.ssid, CONFIG_MESH_ROUTER_SSID, cfg.router.ssid_len);
memcpy((uint8_t *) &cfg.router.password, CONFIG_MESH_ROUTER_PASSWD,
strlen(CONFIG_MESH_ROUTER_PASSWD));
#endif
/* mesh softAP */
ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(CONFIG_MESH_AP_AUTHMODE));
cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS;
memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD,
strlen(CONFIG_MESH_AP_PASSWD));
ESP_ERROR_CHECK(esp_mesh_set_config(&cfg));
/* mesh start */
ESP_ERROR_CHECK(esp_mesh_start());
#ifdef ROOT_DEVICE
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_mesh_set_self_organized(true, false));
#endif
#ifdef NODE_DEVICE
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_mesh_set_self_organized(true, false));
#endif
#ifdef CONFIG_MESH_ENABLE_PS
/* set the device active duty cycle. (default:12, MESH_PS_DEVICE_DUTY_REQUEST) */
ESP_ERROR_CHECK(esp_mesh_set_active_duty_cycle(CONFIG_MESH_PS_DEV_DUTY, CONFIG_MESH_PS_DEV_DUTY_TYPE));
/* set the network active duty cycle. (default:12, -1, MESH_PS_NETWORK_DUTY_APPLIED_ENTIRE) */
ESP_ERROR_CHECK(esp_mesh_set_network_duty_cycle(CONFIG_MESH_PS_NWK_DUTY, CONFIG_MESH_PS_NWK_DUTY_DURATION, CONFIG_MESH_PS_NWK_DUTY_RULE));
#endif
ESP_LOGI(MESH_TAG, "mesh starts successfully, heap:%d, %s<%d>%s, ps:%d\n", esp_get_minimum_free_heap_size(),
esp_mesh_is_root_fixed() ? "root fixed" : "root not fixed",
esp_mesh_get_topology(), esp_mesh_get_topology() ? "(chain)":"(tree)", esp_mesh_is_ps_enabled());
#ifdef ROOT_DEVICE
// mesh_status();
// root_loop();
while(1) {
vTaskDelay(100 / portTICK_RATE_MS);
root_task();
}
#endif
#ifdef NODE_DEVICE
// mesh_status();
// node_loop();
while(1) {
vTaskDelay(1000 / portTICK_RATE_MS);
node_task();
}
#endif
}