Advertisement
Guest User

Untitled

a guest
May 31st, 2018
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.46 KB | None | 0 0
  1. /* Mesh Internal Transceiver Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <string.h>
  10. #include "esp_wifi.h"
  11. #include "esp_system.h"
  12. #include "esp_event_loop.h"
  13. #include "esp_log.h"
  14. #include "esp_mesh.h"
  15. #include "esp_mesh_internal.h"
  16. #include "mesh_light.h"
  17. #include "nvs_flash.h"
  18.  
  19. #include "mdf_wifi_mesh.h"
  20.  
  21. /*******************************************************
  22.  *                Macros
  23.  *******************************************************/
  24. //#define MESH_P2P_TOS_OFF
  25. //#define MESH_ROOT_TO_GROUP
  26.  
  27. /*******************************************************
  28.  *                Constants
  29.  *******************************************************/
  30. #define RX_SIZE          (1500)
  31. #define TX_SIZE          (1460)
  32.  
  33. /*******************************************************
  34.  *                Variable Definitions
  35.  *******************************************************/
  36. static const char *MESH_TAG = "mesh_main";
  37. static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77};
  38. static uint8_t tx_buf[TX_SIZE] = { 0, };
  39. static uint8_t slave_buf[TX_SIZE] = { 0x3E,0x3E, 0x48,0x65,0x6c,0x6c,0x6f,0x5f,0x4d,0x65,0x73,0x68,0x21, 0x00}; // 0x480x650x6c0x6c0x6f0x5f0x4d0x650x730x680x21
  40. static uint8_t rx_buf[RX_SIZE] = { 0, };
  41. static bool is_running = true;
  42. static bool is_mesh_connected = false;
  43. static mesh_addr_t mesh_parent_addr;
  44. static int mesh_layer = -1;
  45. static bool gotIP = false;
  46.  
  47.  
  48. mesh_light_ctl_t light_on = {
  49.     .cmd = MESH_CONTROL_CMD,
  50.     .on = 1,
  51.     .token_id = MESH_TOKEN_ID,
  52.     .token_value = MESH_TOKEN_VALUE,
  53. };
  54.  
  55. mesh_light_ctl_t light_off = {
  56.     .cmd = MESH_CONTROL_CMD,
  57.     .on = 0,
  58.     .token_id = MESH_TOKEN_ID,
  59.     .token_value = MESH_TOKEN_VALUE,
  60. };
  61.  
  62. /*******************************************************
  63.  *                Function Declarations
  64.  *******************************************************/
  65.  
  66. /*******************************************************
  67.  *                Function Definitions
  68.  *******************************************************/
  69. void mesh_send_to_group(const mesh_data_t *data, const mesh_addr_t *group, int seqno)
  70. {
  71.  
  72.     esp_err_t err = esp_mesh_send(group, data,
  73.                                   MESH_DATA_P2P | MESH_DATA_GROUP,
  74.                                   NULL, 0);
  75.     if (err) {
  76.         ESP_LOGE(MESH_TAG,
  77.                  "[GROUP:%d][L:%d][rtableSize:%d]parent:"MACSTR" to "MACSTR", heap:%d[err:0x%x, proto:%d, tos:%d]",
  78.                  seqno, mesh_layer, esp_mesh_get_routing_table_size(),
  79.                  MAC2STR(mesh_parent_addr.addr), MAC2STR(group->addr),
  80.                  esp_get_free_heap_size(), err, data->proto, data->tos);
  81.     } else {
  82.         ESP_LOGW(MESH_TAG,
  83.                  "[GROUP:%d][L:%d][rtableSize:%d]parent:"MACSTR" to "MACSTR", heap:%d[err:0x%x, proto:%d, tos:%d]",
  84.                  seqno, mesh_layer, esp_mesh_get_routing_table_size(),
  85.                  MAC2STR(mesh_parent_addr.addr), MAC2STR(group->addr),
  86.                  esp_get_free_heap_size(), err, data->proto, data->tos);
  87.     }
  88. }
  89.  
  90. void send2Server(mesh_addr_t* s, mesh_data_t *d){
  91.     if(!gotIP) return;
  92.  
  93.     ESP_LOGE(MESH_TAG, "Send to server");
  94.     esp_mesh_send(s, d, MESH_DATA_TODS, NULL , 0);
  95. }
  96.  
  97. void esp_mesh_p2p_tx_main(void *arg)
  98. {
  99.     int i;
  100.     esp_err_t err;
  101.     int send_count = 0;
  102.     mesh_addr_t route_table[CONFIG_MESH_ROUTE_TABLE_SIZE];
  103.     int route_table_size = 0;
  104.     mesh_data_t data;
  105.     data.data = tx_buf;
  106.     data.size = sizeof(tx_buf);
  107.     data.proto = MESH_PROTO_BIN;
  108.     mesh_addr_t serverAddr;
  109.     IP4_ADDR(&serverAddr.mip.ip4, 192,168,0,132);
  110.     serverAddr.mip.port = 8089;
  111. #ifdef MESH_P2P_TOS_OFF
  112.     data.tos = MESH_TOS_DEF;
  113. #endif /* MESH_P2P_TOS_OFF */
  114.  
  115.     is_running = true;
  116.     while (is_running) {
  117.         /* non-root do nothing but print */
  118.  
  119.         send2Server(&serverAddr, &data);
  120.  
  121.         ESP_LOGE(MESH_TAG, "Routing table");
  122.         for (i = 0; i < route_table_size; i++) {
  123.             ESP_LOGE(MESH_TAG,MACSTR,MAC2STR(route_table[i].addr));
  124.         }
  125.  
  126.         if (!esp_mesh_is_root()) {
  127.             ESP_LOGI(MESH_TAG, "layer:%d, rtableSize:%d, %s", mesh_layer,
  128.                      esp_mesh_get_routing_table_size(),
  129.                      (is_mesh_connected && esp_mesh_is_root()) ? "ROOT" : is_mesh_connected ? "NODE" : "DISCONNECT");
  130.             vTaskDelay(10 * 1000 / portTICK_RATE_MS);
  131.             mesh_addr_t parent;
  132.             err = esp_mesh_get_parent_bssid(&parent);
  133.             if(err) continue;
  134.             ESP_LOGI(MESH_TAG, "got parent");
  135.            
  136.  
  137.             // warning. begin of copypaste
  138.             /*static int packet_id = 0;
  139.             esp_err_t ret        = ESP_OK;
  140.             int mesh_flag        = MESH_DATA_TODS;
  141.  
  142.             wifi_mesh_data_type_t data_type    = {
  143.                 .proto = MESH_PROTO_HTTP,
  144.             };
  145.  
  146.             mesh_data_t mesh_data = {
  147.                 .proto = MESH_PROTO_HTTP,
  148.                 .tos   = MESH_TOS_DEF,
  149.                 .data = slave_buf,
  150.                 .size = sizeof(slave_buf),
  151.             };
  152.  
  153.             wifi_mesh_head_data_t mesh_head_data = {
  154.                 .size = sizeof(slave_buf),
  155.                 .seq  = 0,
  156.                 .id   = packet_id++,
  157.             };
  158.  
  159.             mesh_opt_t mesh_opt = {
  160.                 .len  = sizeof(wifi_mesh_head_data_t),
  161.                 .val  = (void *) &mesh_head_data,
  162.                 .type = MESH_OPT_RECV_DS_ADDR,
  163.             };
  164.  
  165.             ret = esp_mesh_send(&serverAddr, &mesh_data, mesh_flag, &mesh_opt, 1);*/
  166.  
  167.         // end of copypaste
  168.            
  169.            
  170.             continue;
  171.         }
  172.  
  173.         esp_mesh_get_routing_table((mesh_addr_t *) &route_table,
  174.                                    CONFIG_MESH_ROUTE_TABLE_SIZE * 6, &route_table_size);
  175.         if (send_count && !(send_count % 100)) {
  176.             ESP_LOGI(MESH_TAG, "size:%d/%d,send_count:%d", route_table_size,
  177.                      esp_mesh_get_routing_table_size(), send_count);
  178.         }
  179.         send_count++;
  180.         tx_buf[25] = (send_count >> 24) & 0xff;
  181.         tx_buf[24] = (send_count >> 16) & 0xff;
  182.         tx_buf[23] = (send_count >> 8) & 0xff;
  183.         tx_buf[22] = (send_count >> 0) & 0xff;
  184.         if (send_count % 2) {
  185.             memcpy(tx_buf, (uint8_t *)&light_on, sizeof(light_on));
  186.         } else {
  187.             memcpy(tx_buf, (uint8_t *)&light_off, sizeof(light_off));
  188.         }
  189.  
  190. #ifdef MESH_ROOT_TO_GROUP
  191.         mesh_send_to_group(&data, (mesh_addr_t *)MESH_GROUP_ID, send_count);
  192.         vTaskDelay(1 * 1000 / portTICK_RATE_MS);
  193.         continue;
  194. #endif
  195.         for (i = 0; i < route_table_size; i++) {
  196.             err = esp_mesh_send(&route_table[i], &data, MESH_DATA_P2P, NULL, 0);
  197.             if (err) {
  198.                 ESP_LOGE(MESH_TAG,
  199.                          "[ROOT-2-UNICAST:%d][L:%d]parent:"MACSTR" to "MACSTR", heap:%d[err:0x%x, proto:%d, tos:%d]",
  200.                          send_count, mesh_layer, MAC2STR(mesh_parent_addr.addr),
  201.                          MAC2STR(route_table[i].addr), esp_get_free_heap_size(),
  202.                          err, data.proto, data.tos);
  203.             } else if (!(send_count % 100)) {
  204.                 ESP_LOGW(MESH_TAG,
  205.                          "[ROOT-2-UNICAST:%d][L:%d][rtableSize:%d]parent:"MACSTR" to "MACSTR", heap:%d[err:0x%x, proto:%d, tos:%d]",
  206.                          send_count, mesh_layer,
  207.                          esp_mesh_get_routing_table_size(),
  208.                          MAC2STR(mesh_parent_addr.addr),
  209.                          MAC2STR(route_table[i].addr), esp_get_free_heap_size(),
  210.                          err, data.proto, data.tos);
  211.             }
  212.         }
  213.         /* if route_table_size is less than 10, add delay to avoid watchdog in this task. */
  214.         if (route_table_size < 10) {
  215.             vTaskDelay(1 * 1000 / portTICK_RATE_MS);
  216.         }
  217.     }
  218.     vTaskDelete(NULL);
  219. }
  220.  
  221. void esp_mesh_p2p_rx_main(void *arg)
  222. {
  223.     int recv_count = 0;
  224.     esp_err_t err;
  225.     mesh_addr_t from;
  226.     int send_count = 0;
  227.     mesh_data_t data;
  228.     int flag = 0;
  229.     data.data = rx_buf;
  230.     data.size = RX_SIZE;
  231.  
  232.     is_running = true;
  233.     while (is_running) {
  234.         data.size = RX_SIZE;
  235.         err = esp_mesh_recv(&from, &data, portMAX_DELAY, &flag, NULL, 0);
  236.         if (err != ESP_OK || !data.size) {
  237.             ESP_LOGE(MESH_TAG, "err:0x%x, size:%d", err, data.size);
  238.             continue;
  239.         }
  240.         /* extract send count */
  241.         if (data.size >= sizeof(send_count)) {
  242.             send_count = (data.data[25] << 24) | (data.data[24] << 16)
  243.                          | (data.data[23] << 8) | data.data[22];
  244.         }
  245.         recv_count++;
  246.         /* process light control */
  247.         if(data.data[0] == 62){
  248.             ESP_LOGW(MESH_TAG, "GOT MESSAGE FROM NODE:%s", data.data);
  249.         }
  250.         mesh_light_process(&from, data.data, data.size);
  251.         if (!(recv_count % 1)) {
  252.             ESP_LOGW(MESH_TAG,
  253.                      "[#RX:%d/%d][L:%d] parent:"MACSTR", receive from "MACSTR", size:%d, heap:%d, flag:%d[err:0x%x, proto:%d, tos:%d]",
  254.                      recv_count, send_count, mesh_layer,
  255.                      MAC2STR(mesh_parent_addr.addr), MAC2STR(from.addr),
  256.                      data.size, esp_get_free_heap_size(), flag, err, data.proto,
  257.                      data.tos);
  258.         }
  259.     }
  260.     vTaskDelete(NULL);
  261. }
  262.  
  263. esp_err_t esp_mesh_comm_p2p_start(void)
  264. {
  265.     static bool is_comm_p2p_started = false;
  266.     if (!is_comm_p2p_started) {
  267.         is_comm_p2p_started = true;
  268.         xTaskCreate(esp_mesh_p2p_tx_main, "MPTX", 3072, NULL, 5, NULL);
  269.         xTaskCreate(esp_mesh_p2p_rx_main, "MPRX", 3072, NULL, 5, NULL);
  270.     }
  271.     return ESP_OK;
  272. }
  273.  
  274. void esp_mesh_event_handler(mesh_event_t event)
  275. {
  276. #ifdef MESH_ROOT_TO_GROUP
  277.     mesh_addr_t group;
  278. #endif
  279.     static uint8_t last_layer = 0;
  280.     ESP_LOGD(MESH_TAG, "esp_event_handler:%d", event.id);
  281.  
  282.     switch (event.id) {
  283.     case MESH_EVENT_STARTED:
  284.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_STARTED>heap:%d", esp_get_free_heap_size());
  285.         is_mesh_connected = false;
  286.         mesh_layer = esp_mesh_get_layer();
  287.         break;
  288.     case MESH_EVENT_STOPPED:
  289.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_STOPPED>heap:%d", esp_get_free_heap_size());
  290.         is_mesh_connected = false;
  291.         mesh_layer = esp_mesh_get_layer();
  292.         break;
  293.     case MESH_EVENT_CHILD_CONNECTED:
  294.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_CHILD_CONNECTED>aid:%d, "MACSTR"",
  295.                  event.info.child_connected.aid,
  296.                  MAC2STR(event.info.child_connected.mac));
  297.         break;
  298.     case MESH_EVENT_CHILD_DISCONNECTED:
  299.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_CHILD_DISCONNECTED>aid:%d, "MACSTR"",
  300.                  event.info.child_disconnected.aid,
  301.                  MAC2STR(event.info.child_disconnected.mac));
  302.         break;
  303.     case MESH_EVENT_ROUTING_TABLE_ADD:
  304.         ESP_LOGW(MESH_TAG, "<MESH_EVENT_ROUTING_TABLE_ADD>add %d, new:%d",
  305.                  event.info.routing_table.rt_size_change,
  306.                  event.info.routing_table.rt_size_new);
  307.         break;
  308.     case MESH_EVENT_ROUTING_TABLE_REMOVE:
  309.         ESP_LOGW(MESH_TAG, "<MESH_EVENT_ROUTING_TABLE_REMOVE>remove %d, new:%d",
  310.                  event.info.routing_table.rt_size_change,
  311.                  event.info.routing_table.rt_size_new);
  312.         break;
  313.     case MESH_EVENT_NO_PARNET_FOUND:
  314.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_NO_PARNET_FOUND>scan times:%d",
  315.                  event.info.no_parent.scan_times);
  316.         /* TODO handler for the failure */
  317.         break;
  318.     case MESH_EVENT_PARENT_CONNECTED:
  319.         mesh_layer = event.info.connected.self_layer;
  320.         memcpy(&mesh_parent_addr.addr, event.info.connected.connected.bssid, 6);
  321.         ESP_LOGI(MESH_TAG,
  322.                  "<MESH_EVENT_PARENT_CONNECTED>layer:%d-->%d, parent:"MACSTR"%s",
  323.                  last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr),
  324.                  esp_mesh_is_root() ? "<ROOT>" :
  325.                  (mesh_layer == 2) ? "<layer2>" : "");
  326.         last_layer = mesh_layer;
  327.         mesh_connected_indicator(mesh_layer);
  328.         is_mesh_connected = true;
  329.         if (esp_mesh_is_root()) {
  330.             tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
  331.         }
  332. #ifdef MESH_ROOT_TO_GROUP
  333.         if (mesh_layer == 3) {
  334.             ESP_ERROR_CHECK(
  335.                 esp_mesh_set_group_id((mesh_addr_t * ) MESH_GROUP_ID, 1))
  336.             ESP_ERROR_CHECK(esp_mesh_get_group_list(&group, 1))
  337.             ESP_LOGI(MESH_TAG, "num:%d, group "MACSTR"\n",
  338.                      esp_mesh_get_group_num(), MAC2STR(group.addr));
  339.         }
  340. #endif
  341.         esp_mesh_comm_p2p_start();
  342.         break;
  343.     case MESH_EVENT_PARENT_DISCONNECTED:
  344.         ESP_LOGI(MESH_TAG,
  345.                  "<MESH_EVENT_PARENT_DISCONNECTED>reason:%d",
  346.                  event.info.disconnected.reason);
  347.         is_mesh_connected = false;
  348.         mesh_disconnected_indicator();
  349.         break;
  350.     case MESH_EVENT_LAYER_CHANGE:
  351.         mesh_layer = event.info.layer_change.new_layer;
  352.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_LAYER_CHANGE>layer:%d-->%d%s",
  353.                  last_layer, mesh_layer,
  354.                  esp_mesh_is_root() ? "<ROOT>" :
  355.                  (mesh_layer == 2) ? "<layer2>" : "");
  356.         last_layer = mesh_layer;
  357.         mesh_connected_indicator(mesh_layer);
  358.         break;
  359.     case MESH_EVENT_ROOT_ADDRESS:
  360.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_ROOT_ADDRESS>root address:"MACSTR"",
  361.                  MAC2STR(event.info.root_addr.addr));
  362.         break;
  363.     case MESH_EVENT_ROOT_GOT_IP:
  364.         /* root starts to connect to server */
  365.         ESP_LOGI(MESH_TAG,
  366.                  "<MESH_EVENT_ROOT_GOT_IP>sta ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR,
  367.                  IP2STR(&event.info.got_ip.ip_info.ip),
  368.                  IP2STR(&event.info.got_ip.ip_info.netmask),
  369.                  IP2STR(&event.info.got_ip.ip_info.gw));
  370.         gotIP = true;
  371.         break;
  372.     case MESH_EVENT_ROOT_LOST_IP:
  373.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_ROOT_LOST_IP>");
  374.         break;
  375.     case MESH_EVENT_VOTE_STARTED:
  376.         ESP_LOGI(MESH_TAG,
  377.                  "<MESH_EVENT_VOTE_STARTED>attempts:%d, reason:%d, rc_addr:"MACSTR"",
  378.                  event.info.vote_started.attempts,
  379.                  event.info.vote_started.reason,
  380.                  MAC2STR(event.info.vote_started.rc_addr.addr));
  381.         break;
  382.     case MESH_EVENT_VOTE_STOPPED:
  383.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_VOTE_STOPPED>");
  384.         break;
  385.     case MESH_EVENT_ROOT_SWITCH_REQ:
  386.         ESP_LOGI(MESH_TAG,
  387.                  "<MESH_EVENT_ROOT_SWITCH_REQ>reason:%d, rc_addr:"MACSTR"",
  388.                  event.info.switch_req.reason,
  389.                  MAC2STR( event.info.switch_req.rc_addr.addr));
  390.         break;
  391.     case MESH_EVENT_ROOT_SWITCH_ACK:
  392.         /* new root */
  393.         mesh_layer = esp_mesh_get_layer();
  394.         esp_mesh_get_parent_bssid(&mesh_parent_addr);
  395.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_ROOT_SWITCH_ACK>layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr));
  396.         break;
  397.  
  398.     case MESH_EVENT_TODS_STATE:
  399.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_TODS_REACHABLE>state:%d",
  400.                  event.info.toDS_state);
  401.         break;
  402.     case MESH_EVENT_ROOT_FIXED:
  403.         ESP_LOGI(MESH_TAG, "<MESH_EVENT_ROOT_FIXED>%s",
  404.                  event.info.root_fixed.is_fixed ? "fixed" : "not fixed");
  405.         break;
  406.     default:
  407.         ESP_LOGI(MESH_TAG, "unknown id:%d", event.id);
  408.         break;
  409.     }
  410. }
  411.  
  412. void app_main(void)
  413. {
  414.     ESP_ERROR_CHECK(mesh_light_init());
  415.     ESP_ERROR_CHECK(nvs_flash_init());
  416.     /*  tcpip initialization */
  417.     tcpip_adapter_init();
  418.     /* for mesh
  419.      * stop DHCP server on softAP interface by default
  420.      * stop DHCP client on station interface by default
  421.      * */
  422.     ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  423.     ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
  424. #if 0
  425.     /* static ip settings */
  426.     tcpip_adapter_ip_info_t sta_ip;
  427.     sta_ip.ip.addr = ipaddr_addr("192.168.1.102");
  428.     sta_ip.gw.addr = ipaddr_addr("192.168.1.1");
  429.     sta_ip.netmask.addr = ipaddr_addr("255.255.255.0");
  430.     tcpip_adapter_set_ip_info(WIFI_IF_STA, &sta_ip);
  431. #endif
  432.     /*  wifi initialization */
  433.     ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL));
  434.     wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  435.     ESP_ERROR_CHECK(esp_wifi_init(&config));
  436.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  437.     ESP_ERROR_CHECK(esp_wifi_start());
  438.     /*  mesh initialization */
  439.     ESP_ERROR_CHECK(esp_mesh_init());
  440.     ESP_ERROR_CHECK(esp_mesh_set_max_layer(CONFIG_MESH_MAX_LAYER));
  441.     ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(CONFIG_MESH_AP_AUTHMODE));
  442.     ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
  443.     ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
  444. #ifdef MESH_FIX_ROOT
  445.     ESP_ERROR_CHECK(esp_mesh_fix_root(1));
  446. #endif
  447.     mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
  448.     /* mesh ID */
  449.     memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
  450.     /* mesh event callback */
  451.     cfg.event_cb = &esp_mesh_event_handler;
  452.     /* router */
  453.     cfg.channel = CONFIG_MESH_CHANNEL;
  454.     cfg.router.ssid_len = strlen(CONFIG_MESH_ROUTER_SSID);
  455.     memcpy((uint8_t *) &cfg.router.ssid, CONFIG_MESH_ROUTER_SSID, cfg.router.ssid_len);
  456.     memcpy((uint8_t *) &cfg.router.password, CONFIG_MESH_ROUTER_PASSWD,
  457.            strlen(CONFIG_MESH_ROUTER_PASSWD));
  458.     /* mesh softAP */
  459.     cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS;
  460.     memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD,
  461.            strlen(CONFIG_MESH_AP_PASSWD));
  462.     ESP_ERROR_CHECK(esp_mesh_set_config(&cfg));
  463.     /* set RSSI threshold for connecting to the root */
  464.     mesh_switch_parent_t switch_paras ;
  465.     ESP_ERROR_CHECK(esp_mesh_get_switch_parent_paras(&switch_paras));
  466.     switch_paras.backoff_rssi = -45;
  467.     ESP_ERROR_CHECK(esp_mesh_set_switch_parent_paras(&switch_paras));
  468.  
  469. #ifdef MESH_SET_PARENT
  470.     /* parent */
  471.     wifi_config_t parent = {
  472.         .sta = {
  473.             .ssid = CONFIG_MESH_PARENT_SSID,
  474.             .channel = CONFIG_MESH_CHANNEL,
  475.         },
  476.     };
  477.     ESP_ERROR_CHECK(esp_mesh_set_parent(&parent, MESH_ROOT, 1));
  478. #endif
  479.     /* mesh start */
  480.     ESP_ERROR_CHECK(esp_mesh_start());
  481.     ESP_LOGI(MESH_TAG, "mesh starts successfully, heap:%d, %s\n",  esp_get_free_heap_size(),
  482.              esp_mesh_is_root_fixed() ? "root fixed" : "root not fixed");
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement