cmbahadir / opcua-esp32

Embedded OPC UA Server on ESP32 based on open62541 stack
Mozilla Public License 2.0
114 stars 40 forks source link

need help pls #37

Closed leonvita91 closed 3 years ago

leonvita91 commented 3 years ago

hello

thank you for your code it helps me so much and it working just fine, so my question is when i connect with wifi everything works fine and when i connect with Ethernet i got an error on the console and it tells me the esp32 POE has no mac addr !! and normally the esp Ethernet comes without any MAC addr i tried to change some code but my experience with C language not good enough,and after long research i found that i should add this mac addr manually .. may you pls help me in this point .

this my board link >>

https://www.mouser.de/ProductDetail/Olimex-Ltd/ESP32-POE?qs=unwgFEO1A6tUQVMxdOBsBw%3D%3D&mgh=1&vip=1&gclid=Cj0KCQjwsLWDBhCmARIsAPSL3_159IhB8zzZuHKEq0EzSC9NKlC0LUy-kgOdv8CTzJN2d2gppceJyL8aAjfOEALw_wcB

this is what showed me in console >>

I (534) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. E (719) emac_esp32: emac_esp32_init(353): reset timeout E (719) esp_eth: esp_eth_driver_install(204): init mac failed ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x40087ac4 0x40087ac4: _esp_error_check_failed at /home/leon/esp/esp-idf/components/esp_system/esp_err.c:42

file: "IDF/examples/opcua-esp32/components/ethernet/ethernet_connect.c" line 119 func: start expression: esp_eth_driver_install(&config, &eth_handle)

abort() was called at PC 0x40087ac7 on core 0 0x40087ac7: _esp_error_check_failed at /home/leon/esp/esp-idf/components/esp_system/esp_err.c:43

Backtrace:0x40087d57:0x3ffbadc0 0x400883a1:0x3ffbade0 0x4008f37e:0x3ffbae00 0x40087ac7:0x3ffbae70 0x400d76da:0x3ffbae90 0x400d7739:0x3ffbaf00 0x400d70b0:0x3ffbaf30 0x400d74f9:0x3ffbaf50 0x4015de55:0x3ffbaf70 0x4008bab9:0x3ffbaf90 0x40087d57: panic_abort at /home/leon/esp/esp-idf/components/esp_system/panic.c:354

0x400883a1: esp_system_abort at /home/leon/esp/esp-idf/components/esp_system/system_api.c:112

0x4008f37e: abort at /home/leon/esp/esp-idf/components/newlib/abort.c:46

0x40087ac7: _esp_error_check_failed at /home/leon/esp/esp-idf/components/esp_system/esp_err.c:43

0x400d76da: start at /home/leon/esp/esp-idf/examples/opcua-esp32/components/ethernet/ethernet_connect.c:119 (discriminator 1)

0x400d7739: example_connect at /home/leon/esp/esp-idf/examples/opcua-esp32/components/ethernet/ethernet_connect.c:57

0x400d70b0: connection_scan at /home/leon/esp/esp-idf/examples/opcua-esp32/main/opcua_esp32.c:221 (discriminator 2)

0x400d74f9: app_main at /home/leon/esp/esp-idf/examples/opcua-esp32/main/opcua_esp32.c:240 (discriminator 2)

0x4015de55: main_task at /home/leon/esp/esp-idf/components/freertos/port/port_common.c:134 (discriminator 2)

0x4008bab9: vPortTaskWrapper at /home/leon/esp/esp-idf/components/freertos/port/xtensa/port.c:168

ELF file SHA256: cc9012328923cfee

Entering gdb stub now. $T0b#e6xtensa-esp32-elf-gdb: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory --- gdb exited --- Press Ctrl+] to exit monitor. --- Press Ctrl+F to build & flash project.
--- Press Ctrl+A to build & flash app.
--- Press any other key to resume monitor (resets target).

cmbahadir commented 3 years ago

Hello @leonvita91

This is the same issue with #29

E (711) emac_esp32: emac_esp32_init(351): reset timeout E (711) esp_eth: esp_eth_driver_install(203): init mac failed

These two lines show that your ethernet configuration is not correct. You need to configure the ethernet parameters properly. Since ESP32-PoE is a different board with different ethernet configuration, you may need to change pin, clock configuration.

There was a discussion about it in esp-idf issues, i guess following the steps here would solve the issue; espressif/esp-idf#4565

Cheers, Mete

leonvita91 commented 3 years ago

hello again

i tried to understand the options that i possible to solve this problem, and one says i should make the rest time to 500ms,so i started to search about where i can change the rest time but couldn't found it may you pls help i will be happy for an answer . init(351): reset timeout

this is what im searching for 351ms to 500ms

greetings leon :))

leonvita91 commented 3 years ago

hello thank you again for this nice project and i would close the issue because i solve it.

so the solution that i changed the driver with the one worked for my board and if anyone had this board

20210414_093954 20210414_094013

so what i did, i just added block by block the driver for this board ESP32-POE in ethernet_connect.c that you can find it in this DIR >>> /…/examples/opcua-esp32/components/ethernet so this file is now customized and now looks like this and it work just fine for me :) and in here you just need this code and go to the idf.py menuconfig >> component config >> ethernet >> support ESP32 internal EMAC controller >> RMII clock mode >>> and choose Output RMII clock >> go back and chnage RMII clock GPIO number to 17 . and flash it :)) thats it !!

/////////////////////////////////////////////////////////////////

include

include "sdkconfig.h"

include "esp_event.h"

include "esp_wifi.h"

include "esp_wifi_default.h"

if CONFIG_EXAMPLE_CONNECT_ETHERNET

include "esp_eth.h"

endif

include "esp_log.h"

include "driver/gpio.h"

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "freertos/event_groups.h"

include "lwip/err.h"

include "lwip/sys.h"

include "tcpip_adapter.h"

include "ethernet_connect.h"

define GOT_IPV4_BIT BIT(0)

define GOT_IPV6_BIT BIT(1)

define CONNECTED_BITS (GOT_IPV4_BIT)

static const char *TAG = "online_connection";

/* 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");
    break;
case ETHERNET_EVENT_STOP:
    ESP_LOGI(TAG, "Ethernet Stopped");
    break;
default:
    break;
}

}

/* Event handler for IP_EVENT_ETH_GOT_IP / static void got_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; const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;

ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");

}

/////////////////////////////////////////////////////

static EventGroupHandle_t s_connect_event_group; static esp_ip4_addr_t s_ip_addr; static const char s_connection_name; static esp_netif_t s_example_esp_netif = NULL;

//static const char *TAG = "online_connection"; static void start(void); static void stop(void);

static void on_got_ip(void arg, esp_event_base_t event_base, int32_t event_id, void event_data)

{ ESP_LOGI(TAG, "Got IP event!"); ip_event_got_ip_t event = (ip_event_got_ip_t )event_data; memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); }

esp_err_t example_connect(void) { if (s_connect_event_group != NULL) { return ESP_ERR_INVALID_STATE; } s_connect_event_group = xEventGroupCreate(); start(); ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); ESP_LOGI(TAG, "Waiting for IP"); xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); ESP_LOGI(TAG, "Connected to %s", s_connection_name); ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); return ESP_OK; }

esp_err_t example_disconnect(void) { if (s_connect_event_group == NULL) { return ESP_ERR_INVALID_STATE; } vEventGroupDelete(s_connect_event_group); s_connect_event_group = NULL; stop(); ESP_LOGI(TAG, "Disconnected from %s", s_connection_name); s_connection_name = NULL; return ESP_OK; }

ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET

static esp_eth_handle_t eth_handle = NULL; static esp_eth_mac_t s_mac = NULL; static esp_eth_phy_t s_phy = NULL; static void *s_eth_glue = NULL;

define PIN_PHY_POWER 12

static void start(void) {
tcpip_adapter_init(); esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); esp_netif_t *eth_netif = esp_netif_new(&cfg); assert(eth_netif); s_example_esp_netif = eth_netif;

#ifdef CONFIG_USE_STATIC_IP
esp_netif_ip_info_t ipInfo;
ipInfo.ip.addr = esp_ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_IP4_ADDRESS);
ipInfo.gw.addr = esp_ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_GATEWAY);
ipInfo.netmask.addr = esp_ip4addr_aton(CONFIG_ETHERNET_HELPER_STATIC_NETMASK);
if(ipInfo.ip.addr != 0 && ipInfo.netmask.addr != 0 && ipInfo.gw.addr != 0){
    ESP_ERROR_CHECK(esp_netif_dhcpc_stop(get_example_netif()));
    ESP_ERROR_CHECK(esp_netif_set_ip_info(get_example_netif(), &ipInfo));
}
#endif

// Set default handlers to process TCP/IP stuffs
ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));

//Configuration using LAN8720
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;

//phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; gpio_pad_select_gpio(PIN_PHY_POWER); gpio_set_direction(PIN_PHY_POWER,GPIO_MODE_OUTPUT); gpio_set_level(PIN_PHY_POWER, 1); vTaskDelay(pdMS_TO_TICKS(10));

if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET

mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);

if CONFIG_EXAMPLE_ETH_PHY_IP101

esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);

elif CONFIG_EXAMPLE_ETH_PHY_RTL8201

esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);

elif CONFIG_EXAMPLE_ETH_PHY_LAN8720

esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);

elif CONFIG_EXAMPLE_ETH_PHY_DP83848

esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);

endif

elif CONFIG_EXAMPLE_USE_DM9051

gpio_install_isr_service(0);
spi_device_handle_t spi_handle = NULL;
spi_bus_config_t buscfg = {
    .miso_io_num = CONFIG_EXAMPLE_DM9051_MISO_GPIO,
    .mosi_io_num = CONFIG_EXAMPLE_DM9051_MOSI_GPIO,
    .sclk_io_num = CONFIG_EXAMPLE_DM9051_SCLK_GPIO,
    .quadwp_io_num = -1,
    .quadhd_io_num = -1,
};

ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_DM9051_SPI_HOST, &buscfg, 1)); spi_device_interface_config_t devcfg = { .command_bits = 1, .address_bits = 7, .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_DM9051_SPI_CLOCK_MHZ 1000 1000, .spics_io_num = CONFIG_EXAMPLE_DM9051_CS_GPIO, .queue_size = 20 }; ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_DM9051_SPI_HOST, &devcfg, &spi_handle)); / dm9051 ethernet driver is based on spi driver / eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_DM9051_INT_GPIO; esp_eth_mac_t mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); esp_eth_phy_t phy = esp_eth_phy_new_dm9051(&phy_config);

endif

/////////////////////////////////////////////
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
esp_eth_handle_t eth_handle = NULL;

ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
s_connection_name = "ETH";

} static void stop(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip)); ESP_ERROR_CHECK(esp_eth_stop(eth_handle)); ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); ESP_ERROR_CHECK(esp_eth_clear_default_handlers(s_example_esp_netif)); ESP_ERROR_CHECK(esp_eth_driver_uninstall(eth_handle)); ESP_ERROR_CHECK(s_phy->del(s_phy)); ESP_ERROR_CHECK(s_mac->del(s_mac));

esp_netif_destroy(s_example_esp_netif);
s_example_esp_netif = NULL;

}

endif // CONFIG_EXAMPLE_CONNECT_ETHERNET

ifdef CONFIG_EXAMPLE_CONNECT_WIFI

static void on_wifi_disconnect(void arg, esp_event_base_t event_base, int32_t event_id, void event_data) { ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); esp_err_t err = esp_wifi_connect(); if (err == ESP_ERR_WIFI_NOT_STARTED) { return; } ESP_ERROR_CHECK(err); }

static void start(void) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg));

esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA();

esp_netif_t *netif = esp_netif_new(&netif_config);

assert(netif);

esp_netif_attach_wifi_station(netif);
esp_wifi_set_default_wifi_sta_handlers();

s_example_esp_netif = netif;
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));

ifdef CONFIG_EXAMPLE_CONNECT_IPV6

ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));

endif

ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
    .sta = {
        .ssid = CONFIG_WIFI_SSID,
        .password = CONFIG_WIFI_PASSWORD,
    },
};
ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
s_connection_name = CONFIG_WIFI_SSID;

}

static void stop(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect)); ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip));

ifdef CONFIG_EXAMPLE_CONNECT_IPV6

ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));

endif

esp_err_t err = esp_wifi_stop();
if (err == ESP_ERR_WIFI_NOT_INIT) {
    return;
}
ESP_ERROR_CHECK(err);
ESP_ERROR_CHECK(esp_wifi_deinit());
ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_esp_netif));
esp_netif_destroy(s_example_esp_netif);
s_example_esp_netif = NULL;

}

endif //CONFIG_EXAMPLE_CONNECT_WIFI

esp_netif_t *get_example_netif(void) { return s_example_esp_netif; } ` ///////////////////////////////////////////////////////////////////

i hope this solution help someone else :)

greetings leon :+1: