Ping from ESP32

bobo1974
Posts: 26
Joined: Fri Feb 08, 2019 2:14 pm

Re: Ping from ESP32

Postby bobo1974 » Tue Jul 02, 2019 7:20 am

Ok, I ended up solving this rookie mistake.
Had to do with my local implementation of the source files with the arduino ide that I am using.
I found out the real directory that was used, included the file properly and it worked.

thobias
Posts: 1
Joined: Thu Jun 13, 2019 8:01 pm

Re: Ping from ESP32

Postby thobias » Tue Jul 23, 2019 5:22 pm

I'm trying to use ping API of ESP8266_RTOS_SDK but there is a problem with ping_init() function, i'm including two libs ping.h and esp_ping.h. The functions of esp_ping are working fine, but the linker do not recognize ping_init of ping.h calling: " undefined reference to `ping_init' " when i try to make the project.

Someone could help me?

fruityTomatoe
Posts: 1
Joined: Thu Aug 08, 2019 7:42 am

Re: Ping from ESP32

Postby fruityTomatoe » Thu Aug 08, 2019 7:45 am

thobias wrote:
Tue Jul 23, 2019 5:22 pm
I'm trying to use ping API of ESP8266_RTOS_SDK but there is a problem with ping_init() function, i'm including two libs ping.h and esp_ping.h. The functions of esp_ping are working fine, but the linker do not recognize ping_init of ping.h calling: " undefined reference to `ping_init' " when i try to make the project.

Someone could help me?
I am also seeing the undefined reference to `ping_init' " issue. Did you have a solution?

mz_autec
Posts: 1
Joined: Tue Sep 24, 2019 7:44 am

Re: Ping from ESP32

Postby mz_autec » Tue Sep 24, 2019 7:46 am

Hi,
try to include "ping/ping.h".
The ping.h path is not in the compiler paths configuration.

Mirko

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Ping from ESP32

Postby nopnop2002 » Tue Jan 21, 2020 12:43 am

Finally I succeeded in pinging using ESP-IDE.
It is published here.
https://github.com/nopnop2002/esp-idf-ping

glrtheil
Posts: 61
Joined: Tue Dec 07, 2021 2:48 pm

Re: Ping from ESP32

Postby glrtheil » Tue Apr 26, 2022 7:13 pm

Frustrating how esp-idf examples are so overcomplicated. Examples provided in this thread from years ago do not work, as ping_init() is deprecated. Here's a nice simple method to ping 8.8.8.8 with latest IDF, and utilizing the overcomplicated example from the idf documentation here: https://docs.espressif.com/projects/esp ... _echo.html

Don't forget to connect to wifi/ethernet FIRST!

Code: Select all

static void on_ping_success(esp_ping_handle_t hdl, void *args)
{
    // optionally, get callback arguments
    // const char* str = (const char*) args;
    // printf("%s\r\n", str); // "foo"
    uint8_t ttl;
    uint16_t seqno;
    uint32_t elapsed_time, recv_len;
    ip_addr_t target_addr;
    esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
    esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
    esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
    printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
           recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
    ESP_LOGW("pingSuccess", "");
}

static void on_ping_timeout(esp_ping_handle_t hdl, void *args)
{
    uint16_t seqno;
    ip_addr_t target_addr;
    esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
    ESP_LOGW("pingTimeout", "");
}

static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
{
    uint32_t transmitted;
    uint32_t received;
    uint32_t total_time_ms;

    esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
    esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
    esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
    printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms);
}

void app_main(void)
{
    //CONNECT TO WIFI FIRST!
    
    //destination
    ip4_addr_t gw;
    gw.addr = ipaddr_addr("8.8.8.8");
    esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
    ip_addr_t ping_target;
    ping_target.u_addr.ip4 = gw;
    ping_config.target_addr = ping_target;
    ping_config.count = 5;
    ping_config.interval_ms = 5000;
    ping_config.timeout_ms = 1000;

    /* set callback functions */
    esp_ping_callbacks_t cbs;
    cbs.on_ping_success = on_ping_success;
    cbs.on_ping_timeout = on_ping_timeout;
    cbs.on_ping_end = on_ping_end;
    //cbs.cb_args = "foo"; // arguments that will feed to all callback functions, can be NULL
    //cbs.cb_args = eth_event_group;

    esp_ping_handle_t ping;
    esp_ping_new_session(&ping_config, &cbs, &ping);
    esp_ping_start(ping);
}

maddogmaycry
Posts: 11
Joined: Sun Apr 17, 2022 6:58 am

Re: Ping from ESP32

Postby maddogmaycry » Wed Apr 27, 2022 12:26 pm

Code: Select all

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>
#include "lwip/api.h"
#include "ping/ping_sock.h"

static void test_on_ping_success(esp_ping_handle_t hdl, void *args){
    // optionally, get callback arguments
    // const char* str = (const char*) args;
    // printf("%s\r\n", str); // "foo"
    uint8_t ttl;
    uint16_t seqno;
    uint32_t elapsed_time, recv_len;
    ip_addr_t target_addr;
    esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
    esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
    esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
    printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
           recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
    esp_ping_stop(hdl);
    esp_ping_delete_session(hdl);
}

static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args){
    uint16_t seqno;
    ip_addr_t target_addr;
    esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
    esp_ping_stop(hdl);
    esp_ping_delete_session(hdl);
}

static void test_on_ping_end(esp_ping_handle_t hdl, void *args){
    uint32_t transmitted;
    uint32_t received;
    uint32_t total_time_ms;

    esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
    esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
    esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
    printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms);
    esp_ping_stop(hdl);
    esp_ping_delete_session(hdl);
}

void ping(const char * host){
    ip_addr_t target_addr;
    struct hostent *he;
    he = netconn_gethostbyname(host, &target_addr);
    if(he == 0){
        esp_ping_config_t ping_config = {
            .interval_ms = 1000,
            .timeout_ms = 1000,
            .data_size = 64,
            .tos = 0,
            .task_stack_size = 2048,
            .task_prio = 2,
            .interface = 0,
            .target_addr = target_addr,
            .count = 5
        };

        esp_ping_callbacks_t cbs;
        cbs.on_ping_success = test_on_ping_success;
        cbs.on_ping_timeout = test_on_ping_timeout;
        cbs.on_ping_end = test_on_ping_end;
        cbs.cb_args = "foo";

        esp_ping_handle_t ping;
        esp_ping_new_session(&ping_config, &cbs, &ping);
        esp_ping_start(ping);
    }
}
usage:

Code: Select all

ping("any_host_you_want_or_ip");
//If dns server not response ping newer been happend
ping("www.google.com");
//Ping regular ip v4 ip
ping("192.168.1.1");
//If you want ip4_addr_t struct, then convert it to char[]
const char * ip = (char*)ip4addr_ntoa(&ip_info->ip);
ping(ip);

sara_patel
Posts: 5
Joined: Fri Apr 28, 2023 5:42 am

Re: Ping from ESP32

Postby sara_patel » Fri Apr 28, 2023 6:03 am

Hi can u provide a code for sending ping on all interfaces?
I tried adding interface index but that gives be error on Ping handle being NUll

Who is online

Users browsing this forum: Baidu [Spider], Baldhead, brushlow18, NoTan2 and 57 guests