Can't change ESP32S3 WIFI MESH channel to higher then 3

10062244
Posts: 1
Joined: Fri Aug 11, 2023 4:04 pm

Can't change ESP32S3 WIFI MESH channel to higher then 3

Postby 10062244 » Fri Aug 11, 2023 7:05 pm

When ever I change the mesh channel to a value higher then 3, my child nodes won't connect to the root. On channels 1 - 3 everything works fine, but 4 - 14 will not work. My mesh network has no router, is that the reason for this. I'm sure I'm missing something simple. Thanks in advance for any advice.

root code
  1. /* Ethernet Basic 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 <stdio.h>
  10. #include <string.h>
  11. #include <inttypes.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "esp_netif.h"
  15. #include "esp_eth.h"
  16. #include "esp_event.h"
  17. #include "esp_log.h"
  18. #include "ethernet_init.h"
  19. #include "sdkconfig.h"
  20. #include "esp_system.h"
  21. #include <sys/socket.h>
  22. #include <netdb.h>
  23. #include "eip.h"
  24. #include "nvs_flash.h"
  25. #include "esp_mesh_internal.h"
  26. #include "esp_wifi.h"
  27. #include "esp_mac.h"
  28. #include "esp_mesh.h"
  29. #include "driver/gpio.h"
  30.  
  31. #define heart_beat_led 13
  32. #define stop 0
  33. #define static_ip "10.8.180.209"
  34. //#define static_ip "192.168.0.10"
  35. struct {
  36.     char TAG_NAME[10];
  37.     int TAG_DATA;
  38. }typedef tag_t;
  39.  
  40. struct {
  41.     char IP[15];
  42.     tag_t RX_DATA[5]; //data from client
  43.     tag_t TX_DATA[5]; //data to return to client
  44.     int TRANSACTION_NUMBER;
  45. }typedef rx_packet_t;
  46.  
  47. struct {
  48.     int DATA[5];
  49.     int TRANSACTION_NUMBER;
  50. }typedef tx_packet_t;
  51.  
  52. static const char *TAG = "door_uplink";
  53. static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77};
  54.  
  55. static bool is_running = true;
  56. static bool is_mesh_connected = false;
  57. static mesh_addr_t mesh_parent_addr;
  58. static int mesh_layer = -1;
  59. static esp_netif_t *netif_sta = NULL;
  60. static esp_netif_t *eth_netif = NULL;
  61.  
  62. void str_to_bytes(void* ptr_in, unsigned char* bytes, int size){
  63.     unsigned char* ptr = ptr_in;
  64.     for(int i = 0; i < size; i++){
  65.         *(bytes + i) = *(ptr + i);
  66.     }
  67. }
  68.  
  69. static void set_static_ip(esp_netif_t *netif)
  70. {
  71.     if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
  72.         ESP_LOGE(TAG, "Failed to stop dhcp client");
  73.         return;
  74.     }
  75.     esp_netif_ip_info_t ip;
  76.     memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
  77.     ip.ip.addr = ipaddr_addr(static_ip);
  78.     ip.netmask.addr = ipaddr_addr("255.255.255.0");
  79.     ip.gw.addr = ipaddr_addr("0.0.0.0");
  80.     if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) {
  81.         ESP_LOGE(TAG, "Failed to set ip info");
  82.         return;
  83.     }
  84.     ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
  85.     ESP_LOGI(TAG, "IP = " IPSTR "\n", IP2STR(&ip.ip));
  86. }
  87.  
  88. /** Event handler for Ethernet events */
  89. static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  90. {
  91.     uint8_t mac_addr[6] = {0};
  92.     /* we can get the ethernet driver handle from event data */
  93.     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  94.  
  95.     switch (event_id) {
  96.     case ETHERNET_EVENT_CONNECTED:
  97.        
  98.         esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  99.         ESP_LOGI(TAG, "Ethernet Link Up");
  100.         ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  101.                  mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  102.         break;
  103.     case ETHERNET_EVENT_DISCONNECTED:
  104.         ESP_LOGI(TAG, "Ethernet Link Down");
  105.         break;
  106.     case ETHERNET_EVENT_START:
  107.         ESP_LOGI(TAG, "Ethernet Started");
  108.         set_static_ip(arg);
  109.         break;
  110.     case ETHERNET_EVENT_STOP:
  111.         ESP_LOGI(TAG, "Ethernet Stopped");
  112.         break;
  113.     default:
  114.         break;
  115.     }
  116. }
  117.  
  118. void esp_mesh_p2p_rx_main(void *arg)
  119. {
  120.  
  121.     mesh_addr_t from;
  122.  
  123.     mesh_data_t rx_data;
  124.     rx_packet_t rx_packet = {};
  125.     uint8_t rx_buf[sizeof(rx_packet)];
  126.     ESP_LOGI(TAG, "size of rx packet = %d", sizeof(rx_packet));
  127.     rx_data.data = rx_buf;
  128.     rx_data.size = sizeof(rx_packet);
  129.  
  130.     mesh_data_t tx_data;
  131.     tx_packet_t tx_packet = {};
  132.     uint8_t tx_buf[sizeof(tx_packet)];
  133.     ESP_LOGI(TAG, "size of tx packet = %d", sizeof(tx_packet));
  134.     tx_data.data = tx_buf;
  135.     tx_data.size = sizeof(tx_packet);
  136.     tx_data.proto = MESH_PROTO_BIN;
  137.     tx_data.tos = MESH_TOS_P2P;
  138.  
  139.     is_running = true;
  140.     mesh_rx_pending_t pending;
  141.  
  142.     int flag = 0;
  143.     int flip = 0;
  144.     vTaskDelay(3000 / portTICK_PERIOD_MS);
  145.     while (is_running) {
  146.         esp_mesh_get_rx_pending(&pending);
  147.         if(pending.toSelf > 0){
  148.             esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
  149.             memcpy(&rx_packet, rx_data.data, sizeof(rx_packet));
  150.             if(eip_connect(rx_packet.IP, eth_netif) == 0){
  151.                 for(int i = 0; i < 5; i++){
  152.                     if(strlen(rx_packet.RX_DATA[i].TAG_NAME) > 0){
  153.                         write_int(rx_packet.RX_DATA[i].TAG_NAME, rx_packet.RX_DATA[i].TAG_DATA, "sint");
  154.                     }
  155.                     if(strlen(rx_packet.TX_DATA[i].TAG_NAME) > 0){
  156.                         tx_packet.DATA[i] = read_int(rx_packet.TX_DATA[i].TAG_NAME);
  157.                     }
  158.                 }
  159.                 tx_packet.TRANSACTION_NUMBER = rx_packet.TRANSACTION_NUMBER;
  160.                 eip_disconnect();
  161.             }else{
  162.                 ESP_LOGE(TAG, "could not connect to plc %s", rx_packet.IP);
  163.                 tx_packet.TRANSACTION_NUMBER = -1;
  164.             }
  165.             memcpy(tx_data.data, &tx_packet, sizeof(tx_packet));
  166.             esp_mesh_send(&from, &tx_data, MESH_DATA_P2P, NULL, 0);
  167.         }else{
  168.             vTaskDelay(50 / portTICK_PERIOD_MS);
  169.         }
  170.         if(flip == 1){
  171.             flip = 0;
  172.         }else{
  173.             flip = 1;
  174.         }
  175.         gpio_set_level(heart_beat_led, flip);
  176.     }
  177. }
  178.  
  179. esp_err_t esp_mesh_comm_p2p_start(void)
  180. {
  181.     static bool is_comm_p2p_started = false;
  182.     if (!is_comm_p2p_started) {
  183.         is_comm_p2p_started = true;
  184.        
  185.         xTaskCreate(esp_mesh_p2p_rx_main, "MPRX", 3072, NULL, 5, NULL);
  186.     }
  187.     return ESP_OK;
  188. }
  189.  
  190. void mesh_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  191. {
  192.     ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  193.     ESP_LOGI(TAG, "<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip));
  194.  
  195. }
  196.  
  197. void mesh_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  198. {
  199.     mesh_addr_t id = {0,};
  200.     static uint16_t last_layer = 0;
  201.  
  202.     switch (event_id) {
  203.     case MESH_EVENT_STARTED: {
  204.         esp_mesh_get_id(&id);
  205.         ESP_LOGI(TAG, "<MESH_EVENT_MESH_STARTED>ID:"MACSTR"", MAC2STR(id.addr));
  206.         is_mesh_connected = false;
  207.         mesh_layer = esp_mesh_get_layer();
  208.         esp_mesh_comm_p2p_start();
  209.     }
  210.     break;
  211.     case MESH_EVENT_STOPPED: {
  212.         ESP_LOGI(TAG, "<MESH_EVENT_STOPPED>");
  213.         is_mesh_connected = false;
  214.         mesh_layer = esp_mesh_get_layer();
  215.     }
  216.     break;
  217.     case MESH_EVENT_CHILD_CONNECTED: {
  218.         mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)event_data;
  219.         ESP_LOGI(TAG, "<MESH_EVENT_CHILD_CONNECTED>aid:%d, "MACSTR"",
  220.                  child_connected->aid,
  221.                  MAC2STR(child_connected->mac));
  222.     }
  223.     break;
  224.     case MESH_EVENT_CHILD_DISCONNECTED: {
  225.         mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)event_data;
  226.         ESP_LOGI(TAG, "<MESH_EVENT_CHILD_DISCONNECTED>aid:%d, "MACSTR"",
  227.                  child_disconnected->aid,
  228.                  MAC2STR(child_disconnected->mac));
  229.     }
  230.     break;
  231.     case MESH_EVENT_ROUTING_TABLE_ADD: {
  232.         mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
  233.         ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_ADD>add %d, new:%d, layer:%d",
  234.                  routing_table->rt_size_change,
  235.                  routing_table->rt_size_new, mesh_layer);
  236.     }
  237.     break;
  238.     case MESH_EVENT_ROUTING_TABLE_REMOVE: {
  239.         mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
  240.         ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_REMOVE>remove %d, new:%d, layer:%d",
  241.                  routing_table->rt_size_change,
  242.                  routing_table->rt_size_new, mesh_layer);
  243.     }
  244.     break;
  245.     case MESH_EVENT_NO_PARENT_FOUND: {
  246.         mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)event_data;
  247.         ESP_LOGI(TAG, "<MESH_EVENT_NO_PARENT_FOUND>scan times:%d",
  248.                  no_parent->scan_times);
  249.     }
  250.     /* TODO handler for the failure */
  251.     break;
  252.     case MESH_EVENT_PARENT_CONNECTED: {
  253.         mesh_event_connected_t *connected = (mesh_event_connected_t *)event_data;
  254.         esp_mesh_get_id(&id);
  255.         mesh_layer = connected->self_layer;
  256.         memcpy(&mesh_parent_addr.addr, connected->connected.bssid, 6);
  257.         ESP_LOGI(TAG,
  258.                  "<MESH_EVENT_PARENT_CONNECTED>layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR", duty:%d",
  259.                  last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr),
  260.                  esp_mesh_is_root() ? "<ROOT>" :
  261.                  (mesh_layer == 2) ? "<layer2>" : "", MAC2STR(id.addr), connected->duty);
  262.         last_layer = mesh_layer;
  263.         //mesh_connected_indicator(mesh_layer);
  264.         is_mesh_connected = true;
  265.         if (esp_mesh_is_root()) {
  266.             esp_netif_dhcpc_stop(netif_sta);
  267.             esp_netif_dhcpc_start(netif_sta);
  268.         }
  269.        
  270.     }
  271.     break;
  272.     case MESH_EVENT_PARENT_DISCONNECTED: {
  273.         mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)event_data;
  274.         ESP_LOGI(TAG,
  275.                  "<MESH_EVENT_PARENT_DISCONNECTED>reason:%d",
  276.                  disconnected->reason);
  277.         is_mesh_connected = false;
  278.         //mesh_disconnected_indicator();
  279.         mesh_layer = esp_mesh_get_layer();
  280.     }
  281.     break;
  282.     case MESH_EVENT_LAYER_CHANGE: {
  283.         mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)event_data;
  284.         mesh_layer = layer_change->new_layer;
  285.         ESP_LOGI(TAG, "<MESH_EVENT_LAYER_CHANGE>layer:%d-->%d%s",
  286.                  last_layer, mesh_layer,
  287.                  esp_mesh_is_root() ? "<ROOT>" :
  288.                  (mesh_layer == 2) ? "<layer2>" : "");
  289.         last_layer = mesh_layer;
  290.         //mesh_connected_indicator(mesh_layer);
  291.     }
  292.     break;
  293.     case MESH_EVENT_ROOT_ADDRESS: {
  294.         mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)event_data;
  295.         ESP_LOGI(TAG, "<MESH_EVENT_ROOT_ADDRESS>root address:"MACSTR"",
  296.                  MAC2STR(root_addr->addr));
  297.     }
  298.     break;
  299.     case MESH_EVENT_VOTE_STARTED: {
  300.         mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)event_data;
  301.         ESP_LOGI(TAG,
  302.                  "<MESH_EVENT_VOTE_STARTED>attempts:%d, reason:%d, rc_addr:"MACSTR"",
  303.                  vote_started->attempts,
  304.                  vote_started->reason,
  305.                  MAC2STR(vote_started->rc_addr.addr));
  306.     }
  307.     break;
  308.     case MESH_EVENT_VOTE_STOPPED: {
  309.         ESP_LOGI(TAG, "<MESH_EVENT_VOTE_STOPPED>");
  310.         break;
  311.     }
  312.     case MESH_EVENT_ROOT_SWITCH_REQ: {
  313.         mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)event_data;
  314.         ESP_LOGI(TAG,
  315.                  "<MESH_EVENT_ROOT_SWITCH_REQ>reason:%d, rc_addr:"MACSTR"",
  316.                  switch_req->reason,
  317.                  MAC2STR( switch_req->rc_addr.addr));
  318.     }
  319.     break;
  320.     case MESH_EVENT_ROOT_SWITCH_ACK: {
  321.         /* new root */
  322.         mesh_layer = esp_mesh_get_layer();
  323.         esp_mesh_get_parent_bssid(&mesh_parent_addr);
  324.         ESP_LOGI(TAG, "<MESH_EVENT_ROOT_SWITCH_ACK>layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr));
  325.     }
  326.     break;
  327.     case MESH_EVENT_TODS_STATE: {
  328.         mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)event_data;
  329.         ESP_LOGI(TAG, "<MESH_EVENT_TODS_REACHABLE>state:%d", *toDs_state);
  330.     }
  331.     break;
  332.     case MESH_EVENT_ROOT_FIXED: {
  333.         mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)event_data;
  334.         ESP_LOGI(TAG, "<MESH_EVENT_ROOT_FIXED>%s",
  335.                  root_fixed->is_fixed ? "fixed" : "not fixed");
  336.     }
  337.     break;
  338.     case MESH_EVENT_ROOT_ASKED_YIELD: {
  339.         mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)event_data;
  340.         ESP_LOGI(TAG,
  341.                  "<MESH_EVENT_ROOT_ASKED_YIELD>"MACSTR", rssi:%d, capacity:%d",
  342.                  MAC2STR(root_conflict->addr),
  343.                  root_conflict->rssi,
  344.                  root_conflict->capacity);
  345.     }
  346.     break;
  347.     case MESH_EVENT_CHANNEL_SWITCH: {
  348.         mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)event_data;
  349.         ESP_LOGI(TAG, "<MESH_EVENT_CHANNEL_SWITCH>new channel:%d", channel_switch->channel);
  350.     }
  351.     break;
  352.     case MESH_EVENT_SCAN_DONE: {
  353.         mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)event_data;
  354.         ESP_LOGI(TAG, "<MESH_EVENT_SCAN_DONE>number:%d",
  355.                  scan_done->number);
  356.     }
  357.     break;
  358.     case MESH_EVENT_NETWORK_STATE: {
  359.         mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)event_data;
  360.         ESP_LOGI(TAG, "<MESH_EVENT_NETWORK_STATE>is_rootless:%d",
  361.                  network_state->is_rootless);
  362.     }
  363.     break;
  364.     case MESH_EVENT_STOP_RECONNECTION: {
  365.         ESP_LOGI(TAG, "<MESH_EVENT_STOP_RECONNECTION>");
  366.     }
  367.     break;
  368.     case MESH_EVENT_FIND_NETWORK: {
  369.         mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)event_data;
  370.         ESP_LOGI(TAG, "<MESH_EVENT_FIND_NETWORK>new channel:%d, router BSSID:"MACSTR"",
  371.                  find_network->channel, MAC2STR(find_network->router_bssid));
  372.     }
  373.     break;
  374.     case MESH_EVENT_ROUTER_SWITCH: {
  375.         mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)event_data;
  376.         ESP_LOGI(TAG, "<MESH_EVENT_ROUTER_SWITCH>new router:%s, channel:%d, "MACSTR"",
  377.                  router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid));
  378.     }
  379.     break;
  380.     case MESH_EVENT_PS_PARENT_DUTY: {
  381.         mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
  382.         ESP_LOGI(TAG, "<MESH_EVENT_PS_PARENT_DUTY>duty:%d", ps_duty->duty);
  383.     }
  384.     break;
  385.     case MESH_EVENT_PS_CHILD_DUTY: {
  386.         mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
  387.         ESP_LOGI(TAG, "<MESH_EVENT_PS_CHILD_DUTY>cidx:%d, "MACSTR", duty:%d", ps_duty->child_connected.aid-1,
  388.                 MAC2STR(ps_duty->child_connected.mac), ps_duty->duty);
  389.     }
  390.     break;
  391.     default:
  392.         ESP_LOGI(TAG, "unknown id:%" PRId32 "", event_id);
  393.         break;
  394.     }
  395. }
  396.  
  397. void init_gpio(){
  398.  
  399.     gpio_reset_pin(heart_beat_led);
  400.     gpio_set_direction(heart_beat_led, GPIO_MODE_OUTPUT);
  401.  
  402.     gpio_set_direction(stop, GPIO_MODE_INPUT);
  403.     gpio_set_pull_mode(stop, GPIO_PULLUP_ONLY);
  404.  
  405.     gpio_set_level(heart_beat_led, 0);
  406.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  407.     gpio_set_level(heart_beat_led, 1);
  408. }
  409.  
  410. void init_eth()
  411. {
  412.     // Initialize Ethernet driver #####################################################################################
  413.     uint8_t eth_port_cnt = 0;
  414.     esp_eth_handle_t *eth_handles;
  415.     ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt));
  416.     // Create instance(s) of esp-netif for Ethernet(s)
  417.  
  418.     // Use ESP_NETIF_DEFAULT_ETH when just one Ethernet interface is used and you don't need to modify
  419.     // default esp-netif configuration parameters.
  420.     esp_netif_config_t eth_cfg = ESP_NETIF_DEFAULT_ETH();
  421.     eth_netif = esp_netif_new(&eth_cfg);
  422.     // Attach Ethernet driver to TCP/IP stack
  423.     ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0])));
  424.  
  425.     // Register user defined event handers
  426.     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_netif));
  427.  
  428.  
  429.     // Start Ethernet driver state machine
  430.     for (int i = 0; i < eth_port_cnt; i++) {
  431.         ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
  432.     }
  433. }
  434.  
  435. void init_mesh()
  436. {
  437.     // Initialize MESH ################################################################################################
  438.    
  439.     /*  create network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */
  440.     ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL));
  441.     /*  wifi initialization */
  442.     wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  443.  
  444.     ESP_ERROR_CHECK(esp_wifi_init(&config));
  445.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &mesh_ip_event_handler, NULL));
  446.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  447.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
  448.     ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
  449.     ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR));
  450.     ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR));
  451.     ESP_ERROR_CHECK(esp_wifi_start());
  452.     /*  mesh initialization */
  453.     ESP_ERROR_CHECK(esp_mesh_init());
  454.     ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL));
  455.     // /*  set mesh topology */
  456.     ESP_ERROR_CHECK(esp_mesh_set_topology(0));
  457.     // /*  set mesh max layer according to the topology */
  458.     ESP_ERROR_CHECK(esp_mesh_set_max_layer(6));
  459.     ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
  460.     ESP_ERROR_CHECK(esp_mesh_set_xon_qsize(128));
  461.     // /* Disable mesh PS function */
  462.     ESP_ERROR_CHECK(esp_mesh_disable_ps());
  463.     ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
  464.    
  465.     ESP_ERROR_CHECK(esp_mesh_fix_root(true));
  466.     mesh_cfg_t mesh_cfg = MESH_INIT_CONFIG_DEFAULT();
  467.     /* mesh ID */
  468.     memcpy((uint8_t *) &mesh_cfg.mesh_id, MESH_ID, 6);
  469.     // /* router */
  470.     // mesh_cfg.channel = 0;
  471.     // mesh_cfg.router.ssid_len = strlen('ssid');
  472.     // memcpy((uint8_t *) &mesh_cfg.router.ssid, 'ssid', mesh_cfg.router.ssid_len);
  473.     // memcpy((uint8_t *) &mesh_cfg.router.password, '1234', strlen('1234'));
  474.     /* mesh softAP */
  475.     ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(3));
  476.     mesh_cfg.mesh_ap.max_connection = 10;
  477.     mesh_cfg.mesh_ap.nonmesh_max_connection = 0;
  478.     memcpy((uint8_t *) &mesh_cfg.mesh_ap.password, "MAP_PASSWD", strlen("MAP_PASSWD"));
  479.     mesh_cfg.channel = 3;
  480.     ESP_ERROR_CHECK(esp_mesh_set_config(&mesh_cfg));
  481.     // /* mesh start */
  482.     ESP_ERROR_CHECK(esp_mesh_start());
  483.     //ESP_ERROR_CHECK(esp_mesh_set_self_organized(true, false));
  484.     ESP_ERROR_CHECK(esp_mesh_set_type(MESH_ROOT));
  485.     //esp_mesh_fix_root(true);
  486. }
  487.  
  488. void app_main(void)
  489. {
  490.     ESP_ERROR_CHECK(nvs_flash_init());
  491.    
  492.     // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
  493.     ESP_ERROR_CHECK(esp_netif_init());
  494.     // Create default event loop that running in background
  495.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  496.  
  497.     init_gpio();
  498.     init_eth();
  499.     init_mesh();
  500.    
  501.    
  502. }
  503.    
child node code
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <inttypes.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_netif.h"
  7.  
  8. #include "esp_event.h"
  9. #include "esp_log.h"
  10.  
  11. #include "sdkconfig.h"
  12. #include "esp_system.h"
  13. #include <sys/socket.h>
  14. #include <netdb.h>
  15.  
  16. #include "nvs_flash.h"
  17. #include "esp_mesh_internal.h"
  18. #include "esp_wifi.h"
  19. #include "esp_mac.h"
  20. #include "esp_mesh.h"
  21. #include "driver/gpio.h"
  22.  
  23. #define node_name "D5"
  24. //esp32s2
  25. #define heart_beat_led 13
  26. #define door_status 5
  27. #define open_door 18
  28. #define stop 0
  29. #define plc_ip "10.8.180.223"
  30. //#define plc_ip "192.168.0.11"
  31. #define door_cmd_tag "D5CMD"
  32. #define door_status_tag "D5STATE"
  33. #define heartbeat_tag "D5HB"
  34.  
  35. struct {
  36.     char TAG_NAME[10];
  37.     int TAG_DATA;
  38. }typedef tag_t;
  39.  
  40. struct {
  41.     char IP[15];
  42.     tag_t TX_DATA[5]; //data to send to server
  43.     tag_t RX_DATA[5]; //tags the server should retrun data from
  44.     int TRANSACTION_NUMBER;
  45. }typedef tx_packet_t;
  46.  
  47. struct {
  48.     int DATA[5];
  49.     int TRANSACTION_NUMBER;
  50. }typedef rx_packet_t;
  51.  
  52. static const char *TAG = node_name;
  53. static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77};
  54. static bool is_mesh_connected = false;
  55. static mesh_addr_t mesh_parent_addr;
  56. static int mesh_layer = -1;
  57. static esp_netif_t *netif_sta = NULL;
  58. static TaskHandle_t interface_main_handle = NULL;
  59.  
  60. void str_to_bytes(void* ptr_in, char* bytes, int size){
  61.     char* ptr = ptr_in;
  62.     for(int i = 0; i < size; i++){
  63.         *(bytes + i) = *(ptr + i);
  64.     }
  65. }
  66.  
  67. void interface_main(void *arg)
  68. {
  69.     mesh_data_t tx_data;
  70.     tx_packet_t tx_packet = {
  71.         .IP = plc_ip,
  72.        
  73.         .TX_DATA[0] = {.TAG_NAME = door_status_tag, .TAG_DATA = 0},
  74.         .TX_DATA[1] = {.TAG_NAME = heartbeat_tag, .TAG_DATA = 0},
  75.         .TX_DATA[2] = {.TAG_NAME = "", .TAG_DATA = 0},
  76.         .TX_DATA[3] = {.TAG_NAME = "", .TAG_DATA = 0},
  77.         .TX_DATA[4] = {.TAG_NAME = "", .TAG_DATA = 0},
  78.        
  79.         .RX_DATA[0] = {.TAG_NAME = door_cmd_tag, .TAG_DATA = 0},
  80.         .RX_DATA[1] = {.TAG_NAME = "", .TAG_DATA = 0},
  81.         .RX_DATA[2] = {.TAG_NAME = "", .TAG_DATA = 0},
  82.         .RX_DATA[3] = {.TAG_NAME = "", .TAG_DATA = 0},
  83.         .RX_DATA[4] = {.TAG_NAME = "", .TAG_DATA = 0},
  84.        
  85.         .TRANSACTION_NUMBER = 0
  86.     };
  87.    
  88.     uint8_t tx_buf[sizeof(tx_packet)];
  89.     tx_data.data = tx_buf;
  90.     tx_data.size = sizeof(tx_packet);
  91.     tx_data.proto = MESH_PROTO_BIN;
  92.     tx_data.tos = MESH_TOS_P2P;
  93.  
  94.     mesh_data_t rx_data;
  95.     rx_packet_t rx_packet = {
  96.         .DATA[0] = 0,
  97.         .DATA[1] = 0,
  98.         .DATA[2] = 0,
  99.         .DATA[3] = 0,
  100.         .DATA[4] = 0,
  101.        
  102.         .TRANSACTION_NUMBER = 0
  103.     };
  104.  
  105.     uint8_t rx_buf[sizeof(rx_packet)];
  106.     rx_data.data = rx_buf;
  107.     rx_data.size = sizeof(rx_packet);
  108.  
  109.     mesh_addr_t from;
  110.    
  111.     int flag = 0;
  112.     int heart_beat_count = 0;
  113.     int flip = 0;
  114.     mesh_rx_pending_t pending;
  115.     esp_mesh_get_rx_pending(&pending);
  116.     //clear out buffer on startup
  117.     for(int i = 0; i < pending.toSelf; i++){
  118.         esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
  119.     }
  120.     while (true) {
  121.         tx_packet.TX_DATA[0].TAG_DATA = gpio_get_level(door_status);
  122.         tx_packet.TX_DATA[1].TAG_DATA = heart_beat_count;
  123.         memcpy(tx_data.data, &tx_packet, sizeof(tx_packet));
  124.         esp_mesh_send(NULL, &tx_data, MESH_DATA_P2P, NULL, 0);
  125.         vTaskDelay(500 / portTICK_PERIOD_MS);
  126.         esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
  127.         memcpy(&rx_packet, rx_data.data, sizeof(rx_packet));
  128.         if(tx_packet.TRANSACTION_NUMBER == rx_packet.TRANSACTION_NUMBER){
  129.             gpio_set_level(open_door, rx_packet.DATA[0]);
  130.             if(flip == 1){
  131.                 flip = 0;
  132.             }
  133.             else{
  134.                 flip = 1;
  135.             }
  136.             gpio_set_level(heart_beat_led, flip);
  137.             if(heart_beat_count < 100){
  138.                 heart_beat_count += 1;
  139.             }else{
  140.                 heart_beat_count = 0;
  141.             }
  142.             if(tx_packet.TRANSACTION_NUMBER < 30000){
  143.                 tx_packet.TRANSACTION_NUMBER = tx_packet.TRANSACTION_NUMBER + 1;
  144.             }else{
  145.                 ESP_LOGI(TAG, "reset trans number");
  146.                 tx_packet.TRANSACTION_NUMBER = 0;
  147.             }
  148.         }else{
  149.             ESP_LOGE(TAG, "Transaction number mismatch tx = %i\trx = %i", tx_packet.TRANSACTION_NUMBER, rx_packet.TRANSACTION_NUMBER);
  150.             esp_mesh_get_rx_pending(&pending);
  151.             //clear out buffer on startup
  152.             for(int i = 0; i < pending.toSelf; i++){
  153.                 esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
  154.             }
  155.             vTaskDelay(10000 / portTICK_PERIOD_MS);
  156.         }
  157.     }  
  158. }
  159.  
  160. void mesh_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  161. {
  162.     ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  163.     ESP_LOGI(TAG, "<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip));
  164.  
  165. }
  166.  
  167. void mesh_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  168. {
  169.     mesh_addr_t id = {0,};
  170.     static uint16_t last_layer = 0;
  171.  
  172.     switch (event_id) {
  173.     case MESH_EVENT_STARTED: {
  174.         esp_mesh_get_id(&id);
  175.         ESP_LOGI(TAG, "<MESH_EVENT_MESH_STARTED>ID:"MACSTR"", MAC2STR(id.addr));
  176.         is_mesh_connected = false;
  177.         mesh_layer = esp_mesh_get_layer();
  178.     }
  179.     break;
  180.     case MESH_EVENT_STOPPED: {
  181.         ESP_LOGI(TAG, "<MESH_EVENT_STOPPED>");
  182.         is_mesh_connected = false;
  183.         mesh_layer = esp_mesh_get_layer();
  184.     }
  185.     break;
  186.     case MESH_EVENT_CHILD_CONNECTED: {
  187.         mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)event_data;
  188.         ESP_LOGI(TAG, "<MESH_EVENT_CHILD_CONNECTED>aid:%d, "MACSTR"",
  189.                  child_connected->aid,
  190.                  MAC2STR(child_connected->mac));
  191.     }
  192.     break;
  193.     case MESH_EVENT_CHILD_DISCONNECTED: {
  194.         mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)event_data;
  195.         ESP_LOGI(TAG, "<MESH_EVENT_CHILD_DISCONNECTED>aid:%d, "MACSTR"",
  196.                  child_disconnected->aid,
  197.                  MAC2STR(child_disconnected->mac));
  198.     }
  199.     break;
  200.     case MESH_EVENT_ROUTING_TABLE_ADD: {
  201.         mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
  202.         ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_ADD>add %d, new:%d, layer:%d",
  203.                  routing_table->rt_size_change,
  204.                  routing_table->rt_size_new, mesh_layer);
  205.     }
  206.     break;
  207.     case MESH_EVENT_ROUTING_TABLE_REMOVE: {
  208.         mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
  209.         ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_REMOVE>remove %d, new:%d, layer:%d",
  210.                  routing_table->rt_size_change,
  211.                  routing_table->rt_size_new, mesh_layer);
  212.     }
  213.     break;
  214.     case MESH_EVENT_NO_PARENT_FOUND: {
  215.         mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)event_data;
  216.         ESP_LOGI(TAG, "<MESH_EVENT_NO_PARENT_FOUND>scan times:%d",
  217.                  no_parent->scan_times);
  218.         esp_restart();
  219.     }
  220.     /* TODO handler for the failure */
  221.     break;
  222.     case MESH_EVENT_PARENT_CONNECTED: {
  223.         mesh_event_connected_t *connected = (mesh_event_connected_t *)event_data;
  224.         esp_mesh_get_id(&id);
  225.         mesh_layer = connected->self_layer;
  226.         memcpy(&mesh_parent_addr.addr, connected->connected.bssid, 6);
  227.         ESP_LOGI(TAG,
  228.                  "<MESH_EVENT_PARENT_CONNECTED>layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR", duty:%d",
  229.                  last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr),
  230.                  esp_mesh_is_root() ? "<ROOT>" :
  231.                  (mesh_layer == 2) ? "<layer2>" : "", MAC2STR(id.addr), connected->duty);
  232.         last_layer = mesh_layer;
  233.         //mesh_connected_indicator(mesh_layer);
  234.         is_mesh_connected = true;
  235.         if (esp_mesh_is_root()) {
  236.             esp_netif_dhcpc_stop(netif_sta);
  237.             esp_netif_dhcpc_start(netif_sta);
  238.         }
  239.         xTaskCreate(interface_main, "main_interface", 3072, NULL, 5, &interface_main_handle);
  240.     }
  241.     break;
  242.     case MESH_EVENT_PARENT_DISCONNECTED: {
  243.         mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)event_data;
  244.         ESP_LOGI(TAG,
  245.                  "<MESH_EVENT_PARENT_DISCONNECTED>reason:%d",
  246.                  disconnected->reason);
  247.         is_mesh_connected = false;
  248.         //mesh_disconnected_indicator();
  249.         mesh_layer = esp_mesh_get_layer();
  250.         if(interface_main_handle != NULL){
  251.             vTaskDelete(interface_main_handle);
  252.             interface_main_handle = NULL;
  253.         }
  254.     }
  255.     break;
  256.     case MESH_EVENT_LAYER_CHANGE: {
  257.         mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)event_data;
  258.         mesh_layer = layer_change->new_layer;
  259.         ESP_LOGI(TAG, "<MESH_EVENT_LAYER_CHANGE>layer:%d-->%d%s",
  260.                  last_layer, mesh_layer,
  261.                  esp_mesh_is_root() ? "<ROOT>" :
  262.                  (mesh_layer == 2) ? "<layer2>" : "");
  263.         last_layer = mesh_layer;
  264.         //mesh_connected_indicator(mesh_layer);
  265.     }
  266.     break;
  267.     case MESH_EVENT_ROOT_ADDRESS: {
  268.         mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)event_data;
  269.         ESP_LOGI(TAG, "<MESH_EVENT_ROOT_ADDRESS>root address:"MACSTR"",
  270.                  MAC2STR(root_addr->addr));
  271.     }
  272.     break;
  273.     case MESH_EVENT_VOTE_STARTED: {
  274.         mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)event_data;
  275.         ESP_LOGI(TAG,
  276.                  "<MESH_EVENT_VOTE_STARTED>attempts:%d, reason:%d, rc_addr:"MACSTR"",
  277.                  vote_started->attempts,
  278.                  vote_started->reason,
  279.                  MAC2STR(vote_started->rc_addr.addr));
  280.     }
  281.     break;
  282.     case MESH_EVENT_VOTE_STOPPED: {
  283.         ESP_LOGI(TAG, "<MESH_EVENT_VOTE_STOPPED>");
  284.         break;
  285.     }
  286.     case MESH_EVENT_ROOT_SWITCH_REQ: {
  287.         mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)event_data;
  288.         ESP_LOGI(TAG,
  289.                  "<MESH_EVENT_ROOT_SWITCH_REQ>reason:%d, rc_addr:"MACSTR"",
  290.                  switch_req->reason,
  291.                  MAC2STR( switch_req->rc_addr.addr));
  292.     }
  293.     break;
  294.     case MESH_EVENT_ROOT_SWITCH_ACK: {
  295.         /* new root */
  296.         mesh_layer = esp_mesh_get_layer();
  297.         esp_mesh_get_parent_bssid(&mesh_parent_addr);
  298.         ESP_LOGI(TAG, "<MESH_EVENT_ROOT_SWITCH_ACK>layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr));
  299.     }
  300.     break;
  301.     case MESH_EVENT_TODS_STATE: {
  302.         mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)event_data;
  303.         ESP_LOGI(TAG, "<MESH_EVENT_TODS_REACHABLE>state:%d", *toDs_state);
  304.     }
  305.     break;
  306.     case MESH_EVENT_ROOT_FIXED: {
  307.         mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)event_data;
  308.         ESP_LOGI(TAG, "<MESH_EVENT_ROOT_FIXED>%s",
  309.                  root_fixed->is_fixed ? "fixed" : "not fixed");
  310.     }
  311.     break;
  312.     case MESH_EVENT_ROOT_ASKED_YIELD: {
  313.         mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)event_data;
  314.         ESP_LOGI(TAG,
  315.                  "<MESH_EVENT_ROOT_ASKED_YIELD>"MACSTR", rssi:%d, capacity:%d",
  316.                  MAC2STR(root_conflict->addr),
  317.                  root_conflict->rssi,
  318.                  root_conflict->capacity);
  319.     }
  320.     break;
  321.     case MESH_EVENT_CHANNEL_SWITCH: {
  322.         mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)event_data;
  323.         ESP_LOGI(TAG, "<MESH_EVENT_CHANNEL_SWITCH>new channel:%d", channel_switch->channel);
  324.     }
  325.     break;
  326.     case MESH_EVENT_SCAN_DONE: {
  327.         mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)event_data;
  328.         ESP_LOGI(TAG, "<MESH_EVENT_SCAN_DONE>number:%d",
  329.                  scan_done->number);
  330.     }
  331.     break;
  332.     case MESH_EVENT_NETWORK_STATE: {
  333.         mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)event_data;
  334.         ESP_LOGI(TAG, "<MESH_EVENT_NETWORK_STATE>is_rootless:%d",
  335.                  network_state->is_rootless);
  336.     }
  337.     break;
  338.     case MESH_EVENT_STOP_RECONNECTION: {
  339.         ESP_LOGI(TAG, "<MESH_EVENT_STOP_RECONNECTION>");
  340.     }
  341.     break;
  342.     case MESH_EVENT_FIND_NETWORK: {
  343.         mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)event_data;
  344.         ESP_LOGI(TAG, "<MESH_EVENT_FIND_NETWORK>new channel:%d, router BSSID:"MACSTR"",
  345.                  find_network->channel, MAC2STR(find_network->router_bssid));
  346.     }
  347.     break;
  348.     case MESH_EVENT_ROUTER_SWITCH: {
  349.         mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)event_data;
  350.         ESP_LOGI(TAG, "<MESH_EVENT_ROUTER_SWITCH>new router:%s, channel:%d, "MACSTR"",
  351.                  router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid));
  352.     }
  353.     break;
  354.     case MESH_EVENT_PS_PARENT_DUTY: {
  355.         mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
  356.         ESP_LOGI(TAG, "<MESH_EVENT_PS_PARENT_DUTY>duty:%d", ps_duty->duty);
  357.     }
  358.     break;
  359.     case MESH_EVENT_PS_CHILD_DUTY: {
  360.         mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
  361.         ESP_LOGI(TAG, "<MESH_EVENT_PS_CHILD_DUTY>cidx:%d, "MACSTR", duty:%d", ps_duty->child_connected.aid-1,
  362.                 MAC2STR(ps_duty->child_connected.mac), ps_duty->duty);
  363.     }
  364.     break;
  365.     default:
  366.         ESP_LOGI(TAG, "unknown id:%" PRId32 "", event_id);
  367.         break;
  368.     }
  369. }
  370.  
  371. void init_gpio(){
  372.     gpio_reset_pin(heart_beat_led);
  373.     gpio_set_direction(heart_beat_led, GPIO_MODE_OUTPUT);
  374.  
  375.     gpio_reset_pin(door_status);
  376.     gpio_set_direction(door_status, GPIO_MODE_INPUT);
  377.     gpio_set_direction(stop, GPIO_MODE_INPUT);
  378.     gpio_set_pull_mode(stop, GPIO_PULLUP_ONLY);
  379.  
  380.     gpio_reset_pin(open_door);
  381.     gpio_set_direction(open_door, GPIO_MODE_OUTPUT);
  382.  
  383.     gpio_set_level(heart_beat_led, 0);
  384.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  385.     gpio_set_level(heart_beat_led, 1);
  386. }
  387.  
  388. void init_mesh(){
  389.     /*  create network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */
  390.     ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL));
  391.     /*  wifi initialization */
  392.     wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  393.     ESP_ERROR_CHECK(esp_wifi_init(&config));
  394.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &mesh_ip_event_handler, NULL));
  395.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  396.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
  397.     ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR));
  398.     ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR));
  399.     ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
  400.     ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR));
  401.     ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR));
  402.     ESP_ERROR_CHECK(esp_wifi_start());
  403.     /*  mesh initialization */
  404.     ESP_ERROR_CHECK(esp_mesh_init());
  405.     ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL));
  406.     // /*  set mesh topology */
  407.     ESP_ERROR_CHECK(esp_mesh_set_topology(0));
  408.     // /*  set mesh max layer according to the topology */
  409.     ESP_ERROR_CHECK(esp_mesh_set_max_layer(6));
  410.     ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
  411.     ESP_ERROR_CHECK(esp_mesh_set_xon_qsize(128));
  412.     // /* Disable mesh PS function */
  413.     ESP_ERROR_CHECK(esp_mesh_disable_ps());
  414.     ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
  415.    
  416.     ESP_ERROR_CHECK(esp_mesh_fix_root(true));
  417.     mesh_cfg_t mesh_cfg = MESH_INIT_CONFIG_DEFAULT();
  418.     /* mesh ID */
  419.     memcpy((uint8_t *) &mesh_cfg.mesh_id, MESH_ID, 6);
  420.  
  421.     /* mesh softAP */
  422.     ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(3));
  423.     mesh_cfg.mesh_ap.max_connection = 6;
  424.     mesh_cfg.mesh_ap.nonmesh_max_connection = 0;
  425.     memcpy((uint8_t *) &mesh_cfg.mesh_ap.password, "MAP_PASSWD", strlen("MAP_PASSWD"));
  426.     mesh_cfg.allow_channel_switch = true;
  427.     ESP_ERROR_CHECK(esp_mesh_set_config(&mesh_cfg));
  428.     // /* mesh start */
  429.     ESP_ERROR_CHECK(esp_mesh_start());
  430.     ESP_ERROR_CHECK(esp_mesh_set_self_organized(true, true));
  431. }
  432.  
  433.     void app_main(void)
  434. {
  435.     ESP_ERROR_CHECK(nvs_flash_init());
  436.  
  437.     // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
  438.     ESP_ERROR_CHECK(esp_netif_init());
  439.     // Create default event loop that running in background
  440.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  441.    
  442.     init_gpio();
  443.     init_mesh();
  444.  
  445. }

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 122 guests