Ping from ESP32

chaitz
Posts: 14
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: 25
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: 25
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: 25
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: 89
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: 25
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.

pratik.yadav.455
Posts: 23
Joined: Thu Oct 26, 2017 7:34 am

Re: Ping from ESP32

Postby pratik.yadav.455 » Tue Jul 10, 2018 12:14 pm

Hello

I am using ping functionality with following configuration

Code: Select all

PING_TARGET_IP_ADDRESS_COUNT  5 
PING_TARGET_RCV_TIMEO  10000
PING_TARGET_DELAY_TIME 500
PING_TARGET_IP_ADDRESS "www.google.com"
PING_TARGET_RES_FN pingResults
my call call function is as following

Code: Select all

esp_err_t pingResults(ping_target_id_t msgType, esp_ping_found * pf)
 {
	ESP_LOGI(TAG,"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);
	ESP_LOGI(TAG,"Resp(mS):%d Timeouts:%d Total Time:%d Bytes :%d Total Bytes: %d\n", pf->resp_time, pf->timeout_count, pf->total_time, pf->bytes, pf->total_bytes);
	if(pf->err_count == 0){
		++ping_count_local;
		if(ping_count_local >= ping_count){
			ping_sucess_flag = ping_external_success;
		}
	}
	else{
		ping_sucess_flag = ping_external_failed ;
	}
	return ESP_OK;
}
ISSUE is
I am using my ethernet.
After 2 ping if i remove Ethernet cable i am not able to get callback(As my ping count is 5).
Also when i connect Ethernet cable again. fail ping init with -5 error which shows ping is ongoing.
If ping is fails then why i am not getting call back with increased error count ?
am i missing anything or miss understood?

if anyone have idea about it please help me.

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

Re: Ping from ESP32

Postby bobo1974 » Tue Jun 25, 2019 11:48 am

Hi,
I have been trying for a while to make this ping work without success.
From the code above which I have simplified to the max, I still ahve issues:
Appologies, I have not been doing this for a long time.

"ping_init not declared in this scope"
It seems it can't find this although ping.c is included....


This is the error message.

Code: Select all

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "LOLIN D32, Minimal SPIFFS (Large APPS with OTA), 80MHz, 512000, None"

C:\Users\etawy\OneDrive\Development\Arduino\TestSketches\ping_test\ping_test.ino: In function 'void loop()':

ping_test:95:13: error: 'ping_init' was not declared in this scope

   ping_init();

             ^

Multiple libraries were found for "WiFi.h"
 Used: C:\Users\etawy\OneDrive\Documents Perso\ArduinoData\packages\esp32\hardware\esp32\1.0.2\libraries\WiFi
 Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\WiFi
exit status 1
'ping_init' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

And this is my code:

Code: Select all

/*
  #include <stdio.h>
  #include "freertos/FreeRTOS.h"
  #include "freertos/task.h"

  #include "esp_spi_flash.h"
  #include "freertos/event_groups.h"
  #include "esp_log.h"
  #include "esp_event_loop.h"
  #include "nvs_flash.h"
*/
#include "esp_system.h"
#include "lwip/dns.h"
#include "esp_wifi.h"
#include "lwip/inet.h"
#include "esp_ping.h"
#include "lwip/ip4_addr.h"
#include "lwip/ip_addr.h"
// to make this work for v1.01 just change in file Ping.cpp in your library path:
// inet_addr_from_ipaddr to inet_addr_from_ip4addr
// inet_addr_to_ipaddr to inet_addr_to_ip4addr
#include "ping.h"
#include "esp_ping.h"
#include <WiFi.h>




//ip_addr_t ip_Addr;
ip4_addr_t gw;

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;

/*
  #define PING_TARGET_IP_ADDRESS_COUNT  5
  #define PING_TARGET_RCV_TIMEO  10000
  #define PING_TARGET_DELAY_TIME 500
  #define PING_TARGET_IP_ADDRESS "www.google.com"
  #define PING_TARGET_RES_FN pingResults
*/

// the setup function runs once when you press reset or power the board
const char ssid[] = "SFR-XXXX";  //  your network SSID (name)
const char password[] = "XXXXXXXX";       // your network password

void setup() {

  Serial.begin(115200);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}



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;
}

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;
}

void loop() {
  gw.addr = ipaddr_addr("8.8.8.8");
  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));
  printf("%s, %d, ", inet_ntoa(gw), getStrength(10));
  ping_init();
  waiting_results = 1;




}

Who is online

Users browsing this forum: No registered users and 229 guests