Ping from ESP32

chaitz
Posts: 7
Joined: Wed Apr 26, 2017 2:24 pm

Ping from ESP32

Postby chaitz » Tue Sep 05, 2017 2:05 pm

Hi,

What APIs can be used to ping to an address from ESP32? I can see that there is ping_send in lwip/apps/ping but this function header is not available. The header file only provides ping_init().

perryc
Posts: 23
Joined: Fri Feb 17, 2017 1:29 am

Re: Ping from ESP32

Postby perryc » Mon Jan 22, 2018 4:38 pm

This is a old question but just in case it helps the next guy....

It took me a while to figure out how to use the ping api but just create a pingResults function and make some calls to esp_ping_set_target to config things and call ping_init();


Code: Select all

esp_err_t pingResults(ping_target_id_t msgType, esp_ping_found * pf){
...
}

app_main() {
....
 esp_ping_set_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_IP_ADDRESS, &gw.addr, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_RES_FN, &pingResults, sizeof(pingResults));
 ping_init();
 ...
 }


also in eps_ping.c I got rid of the "x 1000" multipliers and pass ping_timeout and ping_delay in mS

Andy_Odessa
Posts: 1
Joined: Fri Jan 26, 2018 8:59 am

Re: Ping from ESP32

Postby Andy_Odessa » Fri Jan 26, 2018 9:07 am

Hi!
Thanks for this code example! Is it possible to give some explanation or example of function pingResults? It will be wery helpful for understanding.

ugurerkan
Posts: 1
Joined: Wed Jan 31, 2018 10:19 am

Re: Ping from ESP32

Postby ugurerkan » Wed Jan 31, 2018 10:21 am

perryc wrote:This is a old question but just in case it helps the next guy....

It took me a while to figure out how to use the ping api but just create a pingResults function and make some calls to esp_ping_set_target to config things and call ping_init();


Code: Select all

esp_err_t pingResults(ping_target_id_t msgType, esp_ping_found * pf){
...
}

app_main() {
....
 esp_ping_set_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_IP_ADDRESS, &gw.addr, sizeof(uint32_t));
 esp_ping_set_target(PING_TARGET_RES_FN, &pingResults, sizeof(pingResults));
 ping_init();
 ...
 }


also in eps_ping.c I got rid of the "x 1000" multipliers and pass ping_timeout and ping_delay in mS


Hello,

I have also try to understand ping with ESP32, can you provide full code example perryc ?

Thank you.

perryc
Posts: 23
Joined: Fri Feb 17, 2017 1:29 am

Re: Ping from ESP32

Postby perryc » Thu Feb 01, 2018 1:41 am

All your ping info is in the pf struct

Code: Select all

esp_err_t pingResults(ping_target_id_t msgType, esp_ping_found * pf){
   printf("AvgTime:%.1fmS Sent:%d Rec:%d Err:%d min(mS):%d max(mS):%d ", (float)pf->total_time/pf->recv_count,
      pf->send_count, pf->recv_count, pf->err_count, pf->min_time, pf->max_time );
   return ESP_OK;
}

perryc
Posts: 23
Joined: Fri Feb 17, 2017 1:29 am

Re: Ping from ESP32

Postby perryc » Sat Feb 03, 2018 4:21 pm

Here's a simple ping app that's based on esp-idf wifi ap example code

Code: Select all


#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "lwip/inet.h"
#include "lwip/ip4_addr.h"
#include "lwip/dns.h"
#include "ping.h"
#include "esp_ping.h"


/*Set the SSID and Password via "make menuconfig"*/
#define DEFAULT_SSID CONFIG_WIFI_SSID
#define DEFAULT_PWD CONFIG_WIFI_PASSWORD

#define TRUE 1
#define FALSE 0

#if CONFIG_WIFI_ALL_CHANNEL_SCAN
#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
#elif CONFIG_WIFI_FAST_SCAN
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
#else
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
#endif /*CONFIG_SCAN_METHOD*/

#if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
#else
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#endif /*CONFIG_SORT_METHOD*/

#if CONFIG_FAST_SCAN_THRESHOLD
#define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
#if CONFIG_EXAMPLE_OPEN
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
#elif CONFIG_EXAMPLE_WEP
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
#elif CONFIG_EXAMPLE_WPA
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
#elif CONFIG_EXAMPLE_WPA2
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
#else
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
#endif
#else
#define DEFAULT_RSSI -127
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN //WIFI_AUTH_WPA2_PSK
#endif /*CONFIG_FAST_SCAN_THRESHOLD*/

static const char *TAG = "scan";
int gotConn = FALSE;
int first = TRUE;

ip_addr_t ip_Addr;

ip4_addr_t ip;
ip4_addr_t gw;
ip4_addr_t msk;
uint32_t ping_count = 25;  //how many pings per report
uint32_t ping_timeout = 1000; //mS till we consider it timed out
uint32_t ping_delay = 500; //mS between pings
uint32_t waiting_results = 0;

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch (event->event_id) {
        case SYSTEM_EVENT_STA_START:
            ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
            ESP_ERROR_CHECK(esp_wifi_connect());
            break;
        case SYSTEM_EVENT_STA_GOT_IP:
            ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
            ip = event->event_info.got_ip.ip_info.ip;
            gw = event->event_info.got_ip.ip_info.gw;
            msk = event->event_info.got_ip.ip_info.netmask;
            ESP_LOGI(TAG, "Got IP: %s\n",
                     ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
            gotConn=TRUE;
           
            break;
        case SYSTEM_EVENT_STA_DISCONNECTED:
            ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
            gotConn=FALSE;
            ping_deinit();
            ESP_ERROR_CHECK(esp_wifi_connect());
            break;
        default:
            break;
    }
    return ESP_OK;
}

/* Initialize Wi-Fi as sta and set scan method */
static void wifi_scan(void)
{
    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = DEFAULT_SSID,
            .password = DEFAULT_PWD,
            .scan_method = DEFAULT_SCAN_METHOD,
            .sort_method = DEFAULT_SORT_METHOD,
            .threshold.rssi = DEFAULT_RSSI,
            .threshold.authmode = DEFAULT_AUTHMODE,
        },
    };
    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());
}

int getStrength(int points){

   wifi_ap_record_t wifidata;
   long rssi = 0;
    long averageRSSI=0;

    for (int i=0;i < points;i++){
       if (esp_wifi_sta_get_ap_info(&wifidata)==0){

       rssi += wifidata.rssi;
       vTaskDelay(25 / portTICK_PERIOD_MS);
       }
    }

   averageRSSI=rssi/points;
   return averageRSSI;
}

esp_err_t pingResults(ping_target_id_t msgType, esp_ping_found * pf){
   printf("AvgTime:%.1fmS Sent:%d Rec:%d Err:%d min(mS):%d max(mS):%d ", (float)pf->total_time/pf->recv_count, pf->send_count, pf->recv_count, pf->err_count, pf->min_time, pf->max_time );
   printf("Resp(mS):%d Timeouts:%d Total Time:%d\n",pf->resp_time, pf->timeout_count, pf->total_time);
   waiting_results = 0;
   return ESP_OK;
}


void app_main()
{
    printf("esp32 Latency and Packdrop test!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
            chip_info.cores,
            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

    printf("silicon revision %d, ", chip_info.revision);

    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");


    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );

    wifi_scan();

    vTaskDelay(5000 / portTICK_PERIOD_MS);
    while(TRUE) {
       vTaskDelay(1000 / portTICK_PERIOD_MS);
       if ((gotConn)&&(!waiting_results)) {
           esp_ping_set_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count, sizeof(uint32_t));
              esp_ping_set_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(uint32_t));
              esp_ping_set_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(uint32_t));
           esp_ping_set_target(PING_TARGET_IP_ADDRESS, &gw.addr, sizeof(uint32_t));
           esp_ping_set_target(PING_TARGET_RES_FN, &pingResults, sizeof(pingResults));

           printf("\nPinging Gateway IP %s WiFi rssi %d\n",inet_ntoa(gw),getStrength(10));
           if (first){
           first = FALSE;
           }
           printf("%s, %d, ",inet_ntoa(gw),getStrength(10));
           ping_init();
           waiting_results = 1;
        }

    }
}

cmorgan
Posts: 80
Joined: Thu Aug 24, 2017 12:52 am

Re: Ping from ESP32

Postby cmorgan » Sun Feb 18, 2018 10:12 pm

Hi @perryc.

I'm using this code here and was running into cases where the pinging was taking forever to timeout or complete.

The delay and timeout values are in seconds, see esp_ping_set_target():

Code: Select all

    case PING_TARGET_RCV_TIMEO:
        ESP_PING_CHECK_OPTLEN(opt_len, uint32_t);
        ping_option_info->ping_rcv_timeout = (*(uint32_t *)opt_val) * 1000;
        break;
    case PING_TARGET_DELAY_TIME:
        ESP_PING_CHECK_OPTLEN(opt_len, uint32_t);
        ping_option_info->ping_delay = (*(uint32_t *)opt_val) * 1000;
       


The ping thread in ping.c uses sys_msleep() which is documented as:

Code: Select all

/* Time functions. */
#ifndef sys_msleep
void sys_msleep(u32_t ms); /* only has a (close to) 1 ms resolution. */
#endif

perryc
Posts: 23
Joined: Fri Feb 17, 2017 1:29 am

Re: Ping from ESP32

Postby perryc » Tue Mar 06, 2018 6:03 pm

There is a feb 25/18 bug fix for the esp-idf that gets rid of the of the "* 1000" in esp_ping.c so grab the latest esp-idf and you can pass normal milliseconds time params.

Who is online

Users browsing this forum: ginodecock and 1 guest