root code
- /* Ethernet Basic Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <string.h>
- #include <inttypes.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_netif.h"
- #include "esp_eth.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "ethernet_init.h"
- #include "sdkconfig.h"
- #include "esp_system.h"
- #include <sys/socket.h>
- #include <netdb.h>
- #include "eip.h"
- #include "nvs_flash.h"
- #include "esp_mesh_internal.h"
- #include "esp_wifi.h"
- #include "esp_mac.h"
- #include "esp_mesh.h"
- #include "driver/gpio.h"
- #define heart_beat_led 13
- #define stop 0
- #define static_ip "10.8.180.209"
- //#define static_ip "192.168.0.10"
- struct {
- char TAG_NAME[10];
- int TAG_DATA;
- }typedef tag_t;
- struct {
- char IP[15];
- tag_t RX_DATA[5]; //data from client
- tag_t TX_DATA[5]; //data to return to client
- int TRANSACTION_NUMBER;
- }typedef rx_packet_t;
- struct {
- int DATA[5];
- int TRANSACTION_NUMBER;
- }typedef tx_packet_t;
- static const char *TAG = "door_uplink";
- static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77};
- static bool is_running = true;
- static bool is_mesh_connected = false;
- static mesh_addr_t mesh_parent_addr;
- static int mesh_layer = -1;
- static esp_netif_t *netif_sta = NULL;
- static esp_netif_t *eth_netif = NULL;
- void str_to_bytes(void* ptr_in, unsigned char* bytes, int size){
- unsigned char* ptr = ptr_in;
- for(int i = 0; i < size; i++){
- *(bytes + i) = *(ptr + i);
- }
- }
- static void set_static_ip(esp_netif_t *netif)
- {
- if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
- ESP_LOGE(TAG, "Failed to stop dhcp client");
- return;
- }
- esp_netif_ip_info_t ip;
- memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
- ip.ip.addr = ipaddr_addr(static_ip);
- ip.netmask.addr = ipaddr_addr("255.255.255.0");
- ip.gw.addr = ipaddr_addr("0.0.0.0");
- if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) {
- ESP_LOGE(TAG, "Failed to set ip info");
- return;
- }
- ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
- ESP_LOGI(TAG, "IP = " IPSTR "\n", IP2STR(&ip.ip));
- }
- /** Event handler for Ethernet events */
- static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
- {
- uint8_t mac_addr[6] = {0};
- /* we can get the ethernet driver handle from event data */
- esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
- switch (event_id) {
- case ETHERNET_EVENT_CONNECTED:
- esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
- ESP_LOGI(TAG, "Ethernet Link Up");
- ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
- mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
- break;
- case ETHERNET_EVENT_DISCONNECTED:
- ESP_LOGI(TAG, "Ethernet Link Down");
- break;
- case ETHERNET_EVENT_START:
- ESP_LOGI(TAG, "Ethernet Started");
- set_static_ip(arg);
- break;
- case ETHERNET_EVENT_STOP:
- ESP_LOGI(TAG, "Ethernet Stopped");
- break;
- default:
- break;
- }
- }
- void esp_mesh_p2p_rx_main(void *arg)
- {
- mesh_addr_t from;
- mesh_data_t rx_data;
- rx_packet_t rx_packet = {};
- uint8_t rx_buf[sizeof(rx_packet)];
- ESP_LOGI(TAG, "size of rx packet = %d", sizeof(rx_packet));
- rx_data.data = rx_buf;
- rx_data.size = sizeof(rx_packet);
- mesh_data_t tx_data;
- tx_packet_t tx_packet = {};
- uint8_t tx_buf[sizeof(tx_packet)];
- ESP_LOGI(TAG, "size of tx packet = %d", sizeof(tx_packet));
- tx_data.data = tx_buf;
- tx_data.size = sizeof(tx_packet);
- tx_data.proto = MESH_PROTO_BIN;
- tx_data.tos = MESH_TOS_P2P;
- is_running = true;
- mesh_rx_pending_t pending;
- int flag = 0;
- int flip = 0;
- vTaskDelay(3000 / portTICK_PERIOD_MS);
- while (is_running) {
- esp_mesh_get_rx_pending(&pending);
- if(pending.toSelf > 0){
- esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
- memcpy(&rx_packet, rx_data.data, sizeof(rx_packet));
- if(eip_connect(rx_packet.IP, eth_netif) == 0){
- for(int i = 0; i < 5; i++){
- if(strlen(rx_packet.RX_DATA[i].TAG_NAME) > 0){
- write_int(rx_packet.RX_DATA[i].TAG_NAME, rx_packet.RX_DATA[i].TAG_DATA, "sint");
- }
- if(strlen(rx_packet.TX_DATA[i].TAG_NAME) > 0){
- tx_packet.DATA[i] = read_int(rx_packet.TX_DATA[i].TAG_NAME);
- }
- }
- tx_packet.TRANSACTION_NUMBER = rx_packet.TRANSACTION_NUMBER;
- eip_disconnect();
- }else{
- ESP_LOGE(TAG, "could not connect to plc %s", rx_packet.IP);
- tx_packet.TRANSACTION_NUMBER = -1;
- }
- memcpy(tx_data.data, &tx_packet, sizeof(tx_packet));
- esp_mesh_send(&from, &tx_data, MESH_DATA_P2P, NULL, 0);
- }else{
- vTaskDelay(50 / portTICK_PERIOD_MS);
- }
- if(flip == 1){
- flip = 0;
- }else{
- flip = 1;
- }
- gpio_set_level(heart_beat_led, flip);
- }
- }
- esp_err_t esp_mesh_comm_p2p_start(void)
- {
- static bool is_comm_p2p_started = false;
- if (!is_comm_p2p_started) {
- is_comm_p2p_started = true;
- xTaskCreate(esp_mesh_p2p_rx_main, "MPRX", 3072, NULL, 5, NULL);
- }
- return ESP_OK;
- }
- void mesh_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
- {
- ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
- ESP_LOGI(TAG, "<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip));
- }
- 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 uint16_t last_layer = 0;
- switch (event_id) {
- case MESH_EVENT_STARTED: {
- esp_mesh_get_id(&id);
- ESP_LOGI(TAG, "<MESH_EVENT_MESH_STARTED>ID:"MACSTR"", MAC2STR(id.addr));
- is_mesh_connected = false;
- mesh_layer = esp_mesh_get_layer();
- esp_mesh_comm_p2p_start();
- }
- break;
- case MESH_EVENT_STOPPED: {
- ESP_LOGI(TAG, "<MESH_EVENT_STOPPED>");
- is_mesh_connected = false;
- mesh_layer = esp_mesh_get_layer();
- }
- break;
- case MESH_EVENT_CHILD_CONNECTED: {
- mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_CHILD_CONNECTED>aid:%d, "MACSTR"",
- child_connected->aid,
- MAC2STR(child_connected->mac));
- }
- break;
- case MESH_EVENT_CHILD_DISCONNECTED: {
- mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_CHILD_DISCONNECTED>aid:%d, "MACSTR"",
- child_disconnected->aid,
- MAC2STR(child_disconnected->mac));
- }
- break;
- case MESH_EVENT_ROUTING_TABLE_ADD: {
- mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
- ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_ADD>add %d, new:%d, layer:%d",
- routing_table->rt_size_change,
- routing_table->rt_size_new, mesh_layer);
- }
- break;
- case MESH_EVENT_ROUTING_TABLE_REMOVE: {
- mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
- ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_REMOVE>remove %d, new:%d, layer:%d",
- routing_table->rt_size_change,
- routing_table->rt_size_new, mesh_layer);
- }
- break;
- case MESH_EVENT_NO_PARENT_FOUND: {
- mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_NO_PARENT_FOUND>scan times:%d",
- no_parent->scan_times);
- }
- /* TODO handler for the failure */
- break;
- case MESH_EVENT_PARENT_CONNECTED: {
- mesh_event_connected_t *connected = (mesh_event_connected_t *)event_data;
- esp_mesh_get_id(&id);
- mesh_layer = connected->self_layer;
- memcpy(&mesh_parent_addr.addr, connected->connected.bssid, 6);
- ESP_LOGI(TAG,
- "<MESH_EVENT_PARENT_CONNECTED>layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR", duty:%d",
- last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr),
- esp_mesh_is_root() ? "<ROOT>" :
- (mesh_layer == 2) ? "<layer2>" : "", MAC2STR(id.addr), connected->duty);
- last_layer = mesh_layer;
- //mesh_connected_indicator(mesh_layer);
- is_mesh_connected = true;
- if (esp_mesh_is_root()) {
- esp_netif_dhcpc_stop(netif_sta);
- esp_netif_dhcpc_start(netif_sta);
- }
- }
- break;
- case MESH_EVENT_PARENT_DISCONNECTED: {
- mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_PARENT_DISCONNECTED>reason:%d",
- disconnected->reason);
- is_mesh_connected = false;
- //mesh_disconnected_indicator();
- mesh_layer = esp_mesh_get_layer();
- }
- break;
- case MESH_EVENT_LAYER_CHANGE: {
- mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)event_data;
- mesh_layer = layer_change->new_layer;
- ESP_LOGI(TAG, "<MESH_EVENT_LAYER_CHANGE>layer:%d-->%d%s",
- last_layer, mesh_layer,
- esp_mesh_is_root() ? "<ROOT>" :
- (mesh_layer == 2) ? "<layer2>" : "");
- last_layer = mesh_layer;
- //mesh_connected_indicator(mesh_layer);
- }
- break;
- case MESH_EVENT_ROOT_ADDRESS: {
- mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_ROOT_ADDRESS>root address:"MACSTR"",
- MAC2STR(root_addr->addr));
- }
- break;
- case MESH_EVENT_VOTE_STARTED: {
- mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_VOTE_STARTED>attempts:%d, reason:%d, rc_addr:"MACSTR"",
- vote_started->attempts,
- vote_started->reason,
- MAC2STR(vote_started->rc_addr.addr));
- }
- break;
- case MESH_EVENT_VOTE_STOPPED: {
- ESP_LOGI(TAG, "<MESH_EVENT_VOTE_STOPPED>");
- break;
- }
- case MESH_EVENT_ROOT_SWITCH_REQ: {
- mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_ROOT_SWITCH_REQ>reason:%d, rc_addr:"MACSTR"",
- switch_req->reason,
- MAC2STR( switch_req->rc_addr.addr));
- }
- break;
- case MESH_EVENT_ROOT_SWITCH_ACK: {
- /* new root */
- mesh_layer = esp_mesh_get_layer();
- esp_mesh_get_parent_bssid(&mesh_parent_addr);
- ESP_LOGI(TAG, "<MESH_EVENT_ROOT_SWITCH_ACK>layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr));
- }
- break;
- case MESH_EVENT_TODS_STATE: {
- mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_TODS_REACHABLE>state:%d", *toDs_state);
- }
- break;
- case MESH_EVENT_ROOT_FIXED: {
- mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_ROOT_FIXED>%s",
- root_fixed->is_fixed ? "fixed" : "not fixed");
- }
- break;
- case MESH_EVENT_ROOT_ASKED_YIELD: {
- mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_ROOT_ASKED_YIELD>"MACSTR", rssi:%d, capacity:%d",
- MAC2STR(root_conflict->addr),
- root_conflict->rssi,
- root_conflict->capacity);
- }
- break;
- case MESH_EVENT_CHANNEL_SWITCH: {
- mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_CHANNEL_SWITCH>new channel:%d", channel_switch->channel);
- }
- break;
- case MESH_EVENT_SCAN_DONE: {
- mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_SCAN_DONE>number:%d",
- scan_done->number);
- }
- break;
- case MESH_EVENT_NETWORK_STATE: {
- mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_NETWORK_STATE>is_rootless:%d",
- network_state->is_rootless);
- }
- break;
- case MESH_EVENT_STOP_RECONNECTION: {
- ESP_LOGI(TAG, "<MESH_EVENT_STOP_RECONNECTION>");
- }
- break;
- case MESH_EVENT_FIND_NETWORK: {
- mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_FIND_NETWORK>new channel:%d, router BSSID:"MACSTR"",
- find_network->channel, MAC2STR(find_network->router_bssid));
- }
- break;
- case MESH_EVENT_ROUTER_SWITCH: {
- mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_ROUTER_SWITCH>new router:%s, channel:%d, "MACSTR"",
- router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid));
- }
- break;
- case MESH_EVENT_PS_PARENT_DUTY: {
- mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_PS_PARENT_DUTY>duty:%d", ps_duty->duty);
- }
- break;
- case MESH_EVENT_PS_CHILD_DUTY: {
- mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_PS_CHILD_DUTY>cidx:%d, "MACSTR", duty:%d", ps_duty->child_connected.aid-1,
- MAC2STR(ps_duty->child_connected.mac), ps_duty->duty);
- }
- break;
- default:
- ESP_LOGI(TAG, "unknown id:%" PRId32 "", event_id);
- break;
- }
- }
- void init_gpio(){
- gpio_reset_pin(heart_beat_led);
- gpio_set_direction(heart_beat_led, GPIO_MODE_OUTPUT);
- gpio_set_direction(stop, GPIO_MODE_INPUT);
- gpio_set_pull_mode(stop, GPIO_PULLUP_ONLY);
- gpio_set_level(heart_beat_led, 0);
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- gpio_set_level(heart_beat_led, 1);
- }
- void init_eth()
- {
- // Initialize Ethernet driver #####################################################################################
- uint8_t eth_port_cnt = 0;
- esp_eth_handle_t *eth_handles;
- ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt));
- // Create instance(s) of esp-netif for Ethernet(s)
- // Use ESP_NETIF_DEFAULT_ETH when just one Ethernet interface is used and you don't need to modify
- // default esp-netif configuration parameters.
- esp_netif_config_t eth_cfg = ESP_NETIF_DEFAULT_ETH();
- eth_netif = esp_netif_new(ð_cfg);
- // Attach Ethernet driver to TCP/IP stack
- ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0])));
- // Register user defined event handers
- ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_netif));
- // Start Ethernet driver state machine
- for (int i = 0; i < eth_port_cnt; i++) {
- ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
- }
- }
- void init_mesh()
- {
- // Initialize MESH ################################################################################################
- /* 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, &mesh_ip_event_handler, NULL));
- ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
- ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
- ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR));
- ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR));
- 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));
- // /* set mesh topology */
- ESP_ERROR_CHECK(esp_mesh_set_topology(0));
- // /* set mesh max layer according to the topology */
- ESP_ERROR_CHECK(esp_mesh_set_max_layer(6));
- ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
- ESP_ERROR_CHECK(esp_mesh_set_xon_qsize(128));
- // /* Disable mesh PS function */
- ESP_ERROR_CHECK(esp_mesh_disable_ps());
- ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
- ESP_ERROR_CHECK(esp_mesh_fix_root(true));
- mesh_cfg_t mesh_cfg = MESH_INIT_CONFIG_DEFAULT();
- /* mesh ID */
- memcpy((uint8_t *) &mesh_cfg.mesh_id, MESH_ID, 6);
- // /* router */
- // mesh_cfg.channel = 0;
- // mesh_cfg.router.ssid_len = strlen('ssid');
- // memcpy((uint8_t *) &mesh_cfg.router.ssid, 'ssid', mesh_cfg.router.ssid_len);
- // memcpy((uint8_t *) &mesh_cfg.router.password, '1234', strlen('1234'));
- /* mesh softAP */
- ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(3));
- mesh_cfg.mesh_ap.max_connection = 10;
- mesh_cfg.mesh_ap.nonmesh_max_connection = 0;
- memcpy((uint8_t *) &mesh_cfg.mesh_ap.password, "MAP_PASSWD", strlen("MAP_PASSWD"));
- mesh_cfg.channel = 3;
- ESP_ERROR_CHECK(esp_mesh_set_config(&mesh_cfg));
- // /* mesh start */
- ESP_ERROR_CHECK(esp_mesh_start());
- //ESP_ERROR_CHECK(esp_mesh_set_self_organized(true, false));
- ESP_ERROR_CHECK(esp_mesh_set_type(MESH_ROOT));
- //esp_mesh_fix_root(true);
- }
- void app_main(void)
- {
- ESP_ERROR_CHECK(nvs_flash_init());
- // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
- ESP_ERROR_CHECK(esp_netif_init());
- // Create default event loop that running in background
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- init_gpio();
- init_eth();
- init_mesh();
- }
- #include <stdio.h>
- #include <string.h>
- #include <inttypes.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_netif.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "sdkconfig.h"
- #include "esp_system.h"
- #include <sys/socket.h>
- #include <netdb.h>
- #include "nvs_flash.h"
- #include "esp_mesh_internal.h"
- #include "esp_wifi.h"
- #include "esp_mac.h"
- #include "esp_mesh.h"
- #include "driver/gpio.h"
- #define node_name "D5"
- //esp32s2
- #define heart_beat_led 13
- #define door_status 5
- #define open_door 18
- #define stop 0
- #define plc_ip "10.8.180.223"
- //#define plc_ip "192.168.0.11"
- #define door_cmd_tag "D5CMD"
- #define door_status_tag "D5STATE"
- #define heartbeat_tag "D5HB"
- struct {
- char TAG_NAME[10];
- int TAG_DATA;
- }typedef tag_t;
- struct {
- char IP[15];
- tag_t TX_DATA[5]; //data to send to server
- tag_t RX_DATA[5]; //tags the server should retrun data from
- int TRANSACTION_NUMBER;
- }typedef tx_packet_t;
- struct {
- int DATA[5];
- int TRANSACTION_NUMBER;
- }typedef rx_packet_t;
- static const char *TAG = node_name;
- static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77};
- static bool is_mesh_connected = false;
- static mesh_addr_t mesh_parent_addr;
- static int mesh_layer = -1;
- static esp_netif_t *netif_sta = NULL;
- static TaskHandle_t interface_main_handle = NULL;
- void str_to_bytes(void* ptr_in, char* bytes, int size){
- char* ptr = ptr_in;
- for(int i = 0; i < size; i++){
- *(bytes + i) = *(ptr + i);
- }
- }
- void interface_main(void *arg)
- {
- mesh_data_t tx_data;
- tx_packet_t tx_packet = {
- .IP = plc_ip,
- .TX_DATA[0] = {.TAG_NAME = door_status_tag, .TAG_DATA = 0},
- .TX_DATA[1] = {.TAG_NAME = heartbeat_tag, .TAG_DATA = 0},
- .TX_DATA[2] = {.TAG_NAME = "", .TAG_DATA = 0},
- .TX_DATA[3] = {.TAG_NAME = "", .TAG_DATA = 0},
- .TX_DATA[4] = {.TAG_NAME = "", .TAG_DATA = 0},
- .RX_DATA[0] = {.TAG_NAME = door_cmd_tag, .TAG_DATA = 0},
- .RX_DATA[1] = {.TAG_NAME = "", .TAG_DATA = 0},
- .RX_DATA[2] = {.TAG_NAME = "", .TAG_DATA = 0},
- .RX_DATA[3] = {.TAG_NAME = "", .TAG_DATA = 0},
- .RX_DATA[4] = {.TAG_NAME = "", .TAG_DATA = 0},
- .TRANSACTION_NUMBER = 0
- };
- uint8_t tx_buf[sizeof(tx_packet)];
- tx_data.data = tx_buf;
- tx_data.size = sizeof(tx_packet);
- tx_data.proto = MESH_PROTO_BIN;
- tx_data.tos = MESH_TOS_P2P;
- mesh_data_t rx_data;
- rx_packet_t rx_packet = {
- .DATA[0] = 0,
- .DATA[1] = 0,
- .DATA[2] = 0,
- .DATA[3] = 0,
- .DATA[4] = 0,
- .TRANSACTION_NUMBER = 0
- };
- uint8_t rx_buf[sizeof(rx_packet)];
- rx_data.data = rx_buf;
- rx_data.size = sizeof(rx_packet);
- mesh_addr_t from;
- int flag = 0;
- int heart_beat_count = 0;
- int flip = 0;
- mesh_rx_pending_t pending;
- esp_mesh_get_rx_pending(&pending);
- //clear out buffer on startup
- for(int i = 0; i < pending.toSelf; i++){
- esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
- }
- while (true) {
- tx_packet.TX_DATA[0].TAG_DATA = gpio_get_level(door_status);
- tx_packet.TX_DATA[1].TAG_DATA = heart_beat_count;
- memcpy(tx_data.data, &tx_packet, sizeof(tx_packet));
- esp_mesh_send(NULL, &tx_data, MESH_DATA_P2P, NULL, 0);
- vTaskDelay(500 / portTICK_PERIOD_MS);
- esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
- memcpy(&rx_packet, rx_data.data, sizeof(rx_packet));
- if(tx_packet.TRANSACTION_NUMBER == rx_packet.TRANSACTION_NUMBER){
- gpio_set_level(open_door, rx_packet.DATA[0]);
- if(flip == 1){
- flip = 0;
- }
- else{
- flip = 1;
- }
- gpio_set_level(heart_beat_led, flip);
- if(heart_beat_count < 100){
- heart_beat_count += 1;
- }else{
- heart_beat_count = 0;
- }
- if(tx_packet.TRANSACTION_NUMBER < 30000){
- tx_packet.TRANSACTION_NUMBER = tx_packet.TRANSACTION_NUMBER + 1;
- }else{
- ESP_LOGI(TAG, "reset trans number");
- tx_packet.TRANSACTION_NUMBER = 0;
- }
- }else{
- ESP_LOGE(TAG, "Transaction number mismatch tx = %i\trx = %i", tx_packet.TRANSACTION_NUMBER, rx_packet.TRANSACTION_NUMBER);
- esp_mesh_get_rx_pending(&pending);
- //clear out buffer on startup
- for(int i = 0; i < pending.toSelf; i++){
- esp_mesh_recv(&from, &rx_data, portMAX_DELAY, &flag, NULL, 0);
- }
- vTaskDelay(10000 / portTICK_PERIOD_MS);
- }
- }
- }
- void mesh_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
- {
- ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
- ESP_LOGI(TAG, "<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip));
- }
- 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 uint16_t last_layer = 0;
- switch (event_id) {
- case MESH_EVENT_STARTED: {
- esp_mesh_get_id(&id);
- ESP_LOGI(TAG, "<MESH_EVENT_MESH_STARTED>ID:"MACSTR"", MAC2STR(id.addr));
- is_mesh_connected = false;
- mesh_layer = esp_mesh_get_layer();
- }
- break;
- case MESH_EVENT_STOPPED: {
- ESP_LOGI(TAG, "<MESH_EVENT_STOPPED>");
- is_mesh_connected = false;
- mesh_layer = esp_mesh_get_layer();
- }
- break;
- case MESH_EVENT_CHILD_CONNECTED: {
- mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_CHILD_CONNECTED>aid:%d, "MACSTR"",
- child_connected->aid,
- MAC2STR(child_connected->mac));
- }
- break;
- case MESH_EVENT_CHILD_DISCONNECTED: {
- mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_CHILD_DISCONNECTED>aid:%d, "MACSTR"",
- child_disconnected->aid,
- MAC2STR(child_disconnected->mac));
- }
- break;
- case MESH_EVENT_ROUTING_TABLE_ADD: {
- mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
- ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_ADD>add %d, new:%d, layer:%d",
- routing_table->rt_size_change,
- routing_table->rt_size_new, mesh_layer);
- }
- break;
- case MESH_EVENT_ROUTING_TABLE_REMOVE: {
- mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)event_data;
- ESP_LOGW(TAG, "<MESH_EVENT_ROUTING_TABLE_REMOVE>remove %d, new:%d, layer:%d",
- routing_table->rt_size_change,
- routing_table->rt_size_new, mesh_layer);
- }
- break;
- case MESH_EVENT_NO_PARENT_FOUND: {
- mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_NO_PARENT_FOUND>scan times:%d",
- no_parent->scan_times);
- esp_restart();
- }
- /* TODO handler for the failure */
- break;
- case MESH_EVENT_PARENT_CONNECTED: {
- mesh_event_connected_t *connected = (mesh_event_connected_t *)event_data;
- esp_mesh_get_id(&id);
- mesh_layer = connected->self_layer;
- memcpy(&mesh_parent_addr.addr, connected->connected.bssid, 6);
- ESP_LOGI(TAG,
- "<MESH_EVENT_PARENT_CONNECTED>layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR", duty:%d",
- last_layer, mesh_layer, MAC2STR(mesh_parent_addr.addr),
- esp_mesh_is_root() ? "<ROOT>" :
- (mesh_layer == 2) ? "<layer2>" : "", MAC2STR(id.addr), connected->duty);
- last_layer = mesh_layer;
- //mesh_connected_indicator(mesh_layer);
- is_mesh_connected = true;
- if (esp_mesh_is_root()) {
- esp_netif_dhcpc_stop(netif_sta);
- esp_netif_dhcpc_start(netif_sta);
- }
- xTaskCreate(interface_main, "main_interface", 3072, NULL, 5, &interface_main_handle);
- }
- break;
- case MESH_EVENT_PARENT_DISCONNECTED: {
- mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_PARENT_DISCONNECTED>reason:%d",
- disconnected->reason);
- is_mesh_connected = false;
- //mesh_disconnected_indicator();
- mesh_layer = esp_mesh_get_layer();
- if(interface_main_handle != NULL){
- vTaskDelete(interface_main_handle);
- interface_main_handle = NULL;
- }
- }
- break;
- case MESH_EVENT_LAYER_CHANGE: {
- mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)event_data;
- mesh_layer = layer_change->new_layer;
- ESP_LOGI(TAG, "<MESH_EVENT_LAYER_CHANGE>layer:%d-->%d%s",
- last_layer, mesh_layer,
- esp_mesh_is_root() ? "<ROOT>" :
- (mesh_layer == 2) ? "<layer2>" : "");
- last_layer = mesh_layer;
- //mesh_connected_indicator(mesh_layer);
- }
- break;
- case MESH_EVENT_ROOT_ADDRESS: {
- mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_ROOT_ADDRESS>root address:"MACSTR"",
- MAC2STR(root_addr->addr));
- }
- break;
- case MESH_EVENT_VOTE_STARTED: {
- mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_VOTE_STARTED>attempts:%d, reason:%d, rc_addr:"MACSTR"",
- vote_started->attempts,
- vote_started->reason,
- MAC2STR(vote_started->rc_addr.addr));
- }
- break;
- case MESH_EVENT_VOTE_STOPPED: {
- ESP_LOGI(TAG, "<MESH_EVENT_VOTE_STOPPED>");
- break;
- }
- case MESH_EVENT_ROOT_SWITCH_REQ: {
- mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_ROOT_SWITCH_REQ>reason:%d, rc_addr:"MACSTR"",
- switch_req->reason,
- MAC2STR( switch_req->rc_addr.addr));
- }
- break;
- case MESH_EVENT_ROOT_SWITCH_ACK: {
- /* new root */
- mesh_layer = esp_mesh_get_layer();
- esp_mesh_get_parent_bssid(&mesh_parent_addr);
- ESP_LOGI(TAG, "<MESH_EVENT_ROOT_SWITCH_ACK>layer:%d, parent:"MACSTR"", mesh_layer, MAC2STR(mesh_parent_addr.addr));
- }
- break;
- case MESH_EVENT_TODS_STATE: {
- mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_TODS_REACHABLE>state:%d", *toDs_state);
- }
- break;
- case MESH_EVENT_ROOT_FIXED: {
- mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_ROOT_FIXED>%s",
- root_fixed->is_fixed ? "fixed" : "not fixed");
- }
- break;
- case MESH_EVENT_ROOT_ASKED_YIELD: {
- mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)event_data;
- ESP_LOGI(TAG,
- "<MESH_EVENT_ROOT_ASKED_YIELD>"MACSTR", rssi:%d, capacity:%d",
- MAC2STR(root_conflict->addr),
- root_conflict->rssi,
- root_conflict->capacity);
- }
- break;
- case MESH_EVENT_CHANNEL_SWITCH: {
- mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_CHANNEL_SWITCH>new channel:%d", channel_switch->channel);
- }
- break;
- case MESH_EVENT_SCAN_DONE: {
- mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_SCAN_DONE>number:%d",
- scan_done->number);
- }
- break;
- case MESH_EVENT_NETWORK_STATE: {
- mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_NETWORK_STATE>is_rootless:%d",
- network_state->is_rootless);
- }
- break;
- case MESH_EVENT_STOP_RECONNECTION: {
- ESP_LOGI(TAG, "<MESH_EVENT_STOP_RECONNECTION>");
- }
- break;
- case MESH_EVENT_FIND_NETWORK: {
- mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_FIND_NETWORK>new channel:%d, router BSSID:"MACSTR"",
- find_network->channel, MAC2STR(find_network->router_bssid));
- }
- break;
- case MESH_EVENT_ROUTER_SWITCH: {
- mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_ROUTER_SWITCH>new router:%s, channel:%d, "MACSTR"",
- router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid));
- }
- break;
- case MESH_EVENT_PS_PARENT_DUTY: {
- mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_PS_PARENT_DUTY>duty:%d", ps_duty->duty);
- }
- break;
- case MESH_EVENT_PS_CHILD_DUTY: {
- mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)event_data;
- ESP_LOGI(TAG, "<MESH_EVENT_PS_CHILD_DUTY>cidx:%d, "MACSTR", duty:%d", ps_duty->child_connected.aid-1,
- MAC2STR(ps_duty->child_connected.mac), ps_duty->duty);
- }
- break;
- default:
- ESP_LOGI(TAG, "unknown id:%" PRId32 "", event_id);
- break;
- }
- }
- void init_gpio(){
- gpio_reset_pin(heart_beat_led);
- gpio_set_direction(heart_beat_led, GPIO_MODE_OUTPUT);
- gpio_reset_pin(door_status);
- gpio_set_direction(door_status, GPIO_MODE_INPUT);
- gpio_set_direction(stop, GPIO_MODE_INPUT);
- gpio_set_pull_mode(stop, GPIO_PULLUP_ONLY);
- gpio_reset_pin(open_door);
- gpio_set_direction(open_door, GPIO_MODE_OUTPUT);
- gpio_set_level(heart_beat_led, 0);
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- gpio_set_level(heart_beat_led, 1);
- }
- void init_mesh(){
- /* 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, &mesh_ip_event_handler, NULL));
- ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
- ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR));
- ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR));
- ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
- ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR));
- ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR));
- 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));
- // /* set mesh topology */
- ESP_ERROR_CHECK(esp_mesh_set_topology(0));
- // /* set mesh max layer according to the topology */
- ESP_ERROR_CHECK(esp_mesh_set_max_layer(6));
- ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
- ESP_ERROR_CHECK(esp_mesh_set_xon_qsize(128));
- // /* Disable mesh PS function */
- ESP_ERROR_CHECK(esp_mesh_disable_ps());
- ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
- ESP_ERROR_CHECK(esp_mesh_fix_root(true));
- mesh_cfg_t mesh_cfg = MESH_INIT_CONFIG_DEFAULT();
- /* mesh ID */
- memcpy((uint8_t *) &mesh_cfg.mesh_id, MESH_ID, 6);
- /* mesh softAP */
- ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(3));
- mesh_cfg.mesh_ap.max_connection = 6;
- mesh_cfg.mesh_ap.nonmesh_max_connection = 0;
- memcpy((uint8_t *) &mesh_cfg.mesh_ap.password, "MAP_PASSWD", strlen("MAP_PASSWD"));
- mesh_cfg.allow_channel_switch = true;
- ESP_ERROR_CHECK(esp_mesh_set_config(&mesh_cfg));
- // /* mesh start */
- ESP_ERROR_CHECK(esp_mesh_start());
- ESP_ERROR_CHECK(esp_mesh_set_self_organized(true, true));
- }
- void app_main(void)
- {
- ESP_ERROR_CHECK(nvs_flash_init());
- // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
- ESP_ERROR_CHECK(esp_netif_init());
- // Create default event loop that running in background
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- init_gpio();
- init_mesh();
- }