Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By rocket_man
#41196 I was actually just about to make a post in the Example Sketches forum when I saw your post, Tawhiz20. I asked a similar question a while ago but got no response, so I tried to figure it out. In the end I basically stitched together a few different pieces of code I found that were written for use with the Espressif SDK, made a few changes here and there, and modified it for use with the Arduino IDE.

The following example allows you to ping an IP address on a set interval from the ESP8266 and it prints out some information that you can view on the Serial Monitor. It works on Arduino IDE 1.6.6. Good luck, and I hope it helps you out!

Code: Select allextern "C" {
#include "ping.h"

#include "os_type.h"
#include <ESP8266WiFi.h>
ping_option pingOps; // Struct used by ping_start, see ping.h
static os_timer_t ping_timer;

// Set all your options here:
#define MY_SSID "your_WiFi_SSID_goes_here"
#define PASSWORD "your_password_goes_here"
#define PING_DELAY_S 1 //amount of time between pings and before a ping times out, sec
#define PING_COUNT 4 //count of pings per request
#define DELAY 3000 // Time to wait after completing a full ping request (doing PING_COUNT pings) and before the next one
IPAddress PING_TARGET(192,168,0,1); // The IP address you're trying to ping

// This function is called when ping_timer fires:
static void ICACHE_FLASH_ATTR ping_cb(void *arg)
  Serial.print("Pinging: ");
  Serial.println(" with 32 bytes of data:");

// This function is called when a ping is received or the request times out:
static void ICACHE_FLASH_ATTR ping_recv (void* arg, void *pdata)
  struct ping_resp *pingrsp = (struct ping_resp *)pdata;

  if (pingrsp->bytes > 0)
    Serial.print("Reply from: ");
    Serial.print(": ");
    Serial.print(" time=");
    Serial.println("Request timed out");

// This function is called after the ping request is completed
// (i.e., after PING_COUNT pings are done)
static void ICACHE_FLASH_ATTR ping_sent (void* arg, void *pdata)
  struct ping_resp *pingrsp = (struct ping_resp *)pdata;

  Serial.print("Ping statistics for: ");
  Serial.print("Packets: Sent = ");
  Serial.print(", Recieved = ");
  Serial.print(", Lost = ");
  Serial.print(" (");
  Serial.println("% loss)");

  // Ping request complete, rearm the timer so that a new request is sent in DELAY seconds
  os_timer_arm(&ping_timer, DELAY, 1);

// Function for connecting to the designated WiFi SSID:
void connectToWifi() {
  WiFi.begin(MY_SSID, PASSWORD);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print("Connected to ");
  Serial.print("IP address: ");

void setup() {

  // Setup the ping options:
  pingOps.count = PING_COUNT;
  pingOps.ip = uint32_t(PING_TARGET);
  pingOps.coarse_time = PING_DELAY_S;
  ping_regist_sent(&pingOps, ping_sent);
  ping_regist_recv(&pingOps, ping_recv);

  os_timer_setfn(&ping_timer, (os_timer_func_t *)ping_cb, (void *)0);

  // Timer ping_timer will fire after DELAY seconds elapse, calling function ping_cb
  // The timer is disarmed in the first line of ping_cb, so it doesn't actually fire every DELAY seconds continiously
  os_timer_arm(&ping_timer, DELAY, 1);

void loop() {
  // Anything else you want to do!
User avatar
By Kerosene
#47038 rocket_man's code works great. I was hoping I could figure out how to tweak it for my own use, but I'm really struggling.

I'd like to be able to trigger a ping from within the loop, rather than have the ping just keep firing by itself.
I'd like to simply send a ping from the loop, and do A if it returns true, or B if it fails.
All I need to know is if the device at 10.1.1.x is up or down.
Just one simple ping is all I need.

I'm way out of my depth and could do with a push in the right direction. :oops: