forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It was linked as a dependency in PlatformIO.ini, but apparently will add complexity to Arduino IDE users.
- Loading branch information
Showing
9 changed files
with
789 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# ESP8266Ping | ||
Let the ESP8266 ping a remote machine. | ||
|
||
With this library an ESP8266 can ping a remote machine and know if it's reachable. | ||
It provide some basic measurements on ping messages (avg response time). | ||
|
||
## Usage | ||
|
||
First, include the library in your sketch along with WiFi library: | ||
|
||
```Arduino | ||
#include <ESP8266WiFi.h> | ||
#include <ESP8266Ping.h> | ||
``` | ||
|
||
Next, simply call the `Ping.ping()` function | ||
|
||
```Arduino | ||
IPAddress ip (192, 168, 0, 1); // The remote ip to ping | ||
bool ret = Ping.ping(ip); | ||
``` | ||
|
||
`ret` will be true if the remote responded to pings, false if not reachable. | ||
The library supports hostname too, just pass a string instead of the ip address: | ||
|
||
```Arduino | ||
bool ret = Ping.ping("www.google.com"); | ||
``` | ||
|
||
Additionally, the function accept a second integer parameter `count` that specify how many pings has to be sent: | ||
|
||
```Arduino | ||
bool ret = Ping.ping(ip_or_host, 10); | ||
``` | ||
|
||
After `Ping.ping()` has been called, the average response time (in milliseconds) can be retrieved with | ||
|
||
```Arduino | ||
int avg_time_ms = Ping.averageTime(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* This example show how to ping a remote machine using it's hostname | ||
*/ | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <ESP8266Ping.h> | ||
|
||
const char* ssid = "ssid"; | ||
const char* password = "passphrase"; | ||
|
||
const char* remote_host = "www.google.com"; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
delay(10); | ||
|
||
// We start by connecting to a WiFi network | ||
|
||
Serial.println(); | ||
Serial.println("Connecting to WiFi"); | ||
|
||
WiFi.begin(ssid, password); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(100); | ||
Serial.print("."); | ||
} | ||
|
||
Serial.println(); | ||
Serial.print("WiFi connected with ip "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
Serial.print("Pinging host "); | ||
Serial.println(remote_host); | ||
|
||
if(Ping.ping(remote_host)) { | ||
Serial.println("Success!!"); | ||
} else { | ||
Serial.println("Error :("); | ||
} | ||
} | ||
|
||
void loop() { } |
44 changes: 44 additions & 0 deletions
44
lib/ESPEasy_ESP8266Ping/examples/SimplePing/SimplePing.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* With this library an ESP8266 can ping a remote machine and know if it's reachable. | ||
* It provides some basic measurements on ping messages (avg response time). | ||
*/ | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <ESP8266Ping.h> | ||
|
||
const char* ssid = "ssid"; | ||
const char* password = "passphrase"; | ||
|
||
const IPAddress remote_ip(192, 168, 0, 1); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
delay(10); | ||
|
||
// We start by connecting to a WiFi network | ||
|
||
Serial.println(); | ||
Serial.println("Connecting to WiFi"); | ||
|
||
WiFi.begin(ssid, password); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(100); | ||
Serial.print("."); | ||
} | ||
|
||
Serial.println(); | ||
Serial.print("WiFi connected with ip "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
Serial.print("Pinging ip "); | ||
Serial.println(remote_ip); | ||
|
||
if(Ping.ping(remote_ip)) { | ||
Serial.println("Success!!"); | ||
} else { | ||
Serial.println("Error :("); | ||
} | ||
} | ||
|
||
void loop() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
####################################### | ||
# Syntax Coloring Map For ESP8266Ping | ||
####################################### | ||
|
||
####################################### | ||
# Library (KEYWORD3) | ||
####################################### | ||
|
||
ESP8266Ping KEYWORD3 | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
Ping KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
ping KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=ESPEasy_ESP8266Ping | ||
version=1.0 | ||
author=Daniele Colanardi | ||
maintainer=Daniele Colanardi <[email protected]> | ||
sentence=Let the ESP8266 ping a remote machine. | ||
paragraph=With this library an ESP8266 can ping a remote machine and know if it's reachable. It provide some basic measurements on ping messages (avg response time). Lib name renamed to avoid conflicts in Arduino IDE. | ||
category=Communication | ||
url= | ||
architectures=esp8266 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
ESP8266Ping - Ping library for ESP8266 | ||
Copyright (c) 2015 Daniele Colanardi. All rights reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef ESP8266Ping_H | ||
#define ESP8266Ping_H | ||
|
||
#include <Arduino.h> | ||
#include <ESP8266WiFi.h> | ||
|
||
extern "C" { | ||
#include <ping.h> | ||
} | ||
|
||
#ifdef ENABLE_DEBUG_PING | ||
#define DEBUG_PING(...) Serial.printf(__VA_ARGS__) | ||
#else | ||
#define DEBUG_PING(...) | ||
#endif | ||
|
||
class PingClass { | ||
public: | ||
PingClass(); | ||
|
||
bool ping(IPAddress dest, byte count = 5); | ||
bool ping(const char* host, byte count = 5); | ||
|
||
int averageTime(); | ||
|
||
protected: | ||
static void _ping_sent_cb(void *opt, void *pdata); | ||
static void _ping_recv_cb(void *opt, void *pdata); | ||
|
||
IPAddress _dest; | ||
ping_option _options; | ||
|
||
static byte _expected_count, _errors, _success; | ||
static int _avg_time; | ||
}; | ||
|
||
#include "ESP8266Ping.impl.h" | ||
PingClass Ping; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
ESP8266Ping - Ping library for ESP8266 | ||
Copyright (c) 2015 Daniele Colanardi. All rights reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
extern "C" void esp_schedule(); | ||
extern "C" void esp_yield(); | ||
|
||
PingClass::PingClass() {} | ||
|
||
bool PingClass::ping(IPAddress dest, byte count) { | ||
_expected_count = count; | ||
_errors = 0; | ||
_success = 0; | ||
|
||
_avg_time = 0; | ||
|
||
memset(&_options, 0, sizeof(struct ping_option)); | ||
|
||
// Repeat count (how many time send a ping message to destination) | ||
_options.count = count; | ||
// Time interval between two ping (seconds??) | ||
_options.coarse_time = 1; | ||
// Destination machine | ||
_options.ip = dest; | ||
|
||
// Callbacks | ||
_options.recv_function = reinterpret_cast<ping_recv_function>(&PingClass::_ping_recv_cb); | ||
_options.sent_function = NULL; //reinterpret_cast<ping_sent_function>(&_ping_sent_cb); | ||
|
||
// Let's go! | ||
if(ping_start(&_options)) { | ||
// Suspend till the process end | ||
esp_yield(); | ||
} | ||
|
||
return (_success > 0); | ||
} | ||
|
||
bool PingClass::ping(const char* host, byte count) { | ||
IPAddress remote_addr; | ||
|
||
if (WiFi.hostByName(host, remote_addr)) | ||
return ping(remote_addr, count); | ||
|
||
return false; | ||
} | ||
|
||
int PingClass::averageTime() { | ||
return _avg_time; | ||
} | ||
|
||
void PingClass::_ping_recv_cb(void *opt, void *resp) { | ||
// Cast the parameters to get some usable info | ||
ping_resp* ping_resp = reinterpret_cast<struct ping_resp*>(resp); | ||
//ping_option* ping_opt = reinterpret_cast<struct ping_option*>(opt); | ||
|
||
// Error or success? | ||
if (ping_resp->ping_err == -1) | ||
_errors++; | ||
else { | ||
_success++; | ||
_avg_time += ping_resp->resp_time; | ||
} | ||
|
||
// Some debug info | ||
DEBUG_PING( | ||
"DEBUG: ping reply\n" | ||
"\ttotal_count = %d \n" | ||
"\tresp_time = %d \n" | ||
"\tseqno = %d \n" | ||
"\ttimeout_count = %d \n" | ||
"\tbytes = %d \n" | ||
"\ttotal_bytes = %d \n" | ||
"\ttotal_time = %d \n" | ||
"\tping_err = %d \n", | ||
ping_resp->total_count, ping_resp->resp_time, ping_resp->seqno, | ||
ping_resp->timeout_count, ping_resp->bytes, ping_resp->total_bytes, | ||
ping_resp->total_time, ping_resp->ping_err | ||
); | ||
|
||
// Is it time to end? | ||
// Don't using seqno because it does not increase on error | ||
if (_success + _errors == _expected_count) { | ||
_avg_time = _avg_time / _expected_count; | ||
|
||
DEBUG_PING("Avg resp time %d ms\n", _avg_time); | ||
|
||
// Done, return to main functiom | ||
esp_schedule(); | ||
} | ||
} | ||
|
||
byte PingClass::_expected_count = 0; | ||
byte PingClass::_errors = 0; | ||
byte PingClass::_success = 0; | ||
int PingClass::_avg_time = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters