Chapter 2. API Reference
Create a new ping session
structure firstly, specifying target IP address, interval times, and etc. Optionally, you can also register some callback
functions with the esp_ping_callbacks_t` structure.
Example method to create a new ping session and register callbacks:
static
void
test_on_ping_success(esp_ping_handle_t hdl,
{
// 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
recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
}
static
void
test_on_ping_timeout(esp_ping_handle_t hdl,
{
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
seqno);
→
}
static
void
test_on_ping_end(esp_ping_handle_t hdl,
{
uint32_t
transmitted;
uint32_t
received;
uint32_t
total_time_ms;
esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST,
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
received, total_time_ms);
→
}
void
initialize_ping()
{
/* convert URL to IP address */
ip_addr_t target_addr;
struct
addrinfo
struct
addrinfo
memset(&hint, 0, sizeof(hint));
memset(&target_addr, 0, sizeof(target_addr));
getaddrinfo("www.espressif.com", NULL, &hint, &res);
struct
in_addr
addr4
inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
Espressif Systems
To create a ping session, you need to fill in the esp_ping_config_t configuration
timeout\n",
hint;
*res
=
NULL;
=
((struct
sockaddr_in
Submit Document Feedback
void
ms\n",
void
inet_ntoa(target_addr.u_addr.ip4),␣
void
*args)
&transmitted,␣
%dms\n",
*) (res->ai_addr))->sin_addr;
596
*args)
*args)
transmitted,␣
(continues on next page)
Release v4.4
Need help?
Do you have a question about the ESP32-S2 and is the answer not in the manual?