8000 First commit · useful-esp8266-lib/ESP8266Ping@bb6ef83 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb6ef83

Browse files
committed
First commit
0 parents  commit bb6ef83

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

examples/SimplePing/SimplePing.ino

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
3+
*
4+
* You need to get streamId and privateKey at data.sparkfun.com and paste them
5+
* below. Or just customize this script to talk to other HTTP servers.
6+
*
7+
*/
8+
9+
#include <ESP8266WiFi.h>
10+
#include <ESP8266Ping.h>
11+
12+
const char* ssid = "ssid";
13+
const char* password = "passphrase";
14+
15+
const IPAddress ping_dest(192, 168, 0, 1);
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
delay(10);
20+
21+
// We start by connecting to a WiFi network
22+
23+
Serial.println();
24+
Serial.println("Connecting to WiFi");
25+
26+
WiFi.begin(ssid, password);
27+
28+
while (WiFi.status() != WL_CONNECTED) {
29+
delay(100);
30+
Serial.print(".");
31+
}
32+
33+
Serial.println();
34+
Serial.print("WiFi connected with ip ");
35+
Serial.println(WiFi.localIP());
36+
37+
if(Ping.ping(ping_dest)) {
38+
Serial.println("Success!!");
39+
} else {
40+
Serial.println("Error :(");
41+
}
42+
}
43+
44+
void loop() { }

keywords.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#######################################
2+
# Syntax Coloring Map For ESP8266Ping
3+
#######################################
4+
5+
#######################################
6+
# Library (KEYWORD3)
7+
#######################################
8+
9+
ESP8266Ping KEYWORD3
10+
11+
#######################################
12+
# Datatypes (KEYWORD1)
13+
#######################################
14+
15+
Ping KEYWORD1
16+
17+
#######################################
18+
# Methods and Functions (KEYWORD2)
19+
#######################################
20+
21+
ping KEYWORD2
22+
23+
#######################################
24+
# Constants (LITERAL1)
25+
#######################################
26+

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ESP8266Ping
2+
version=1.0
3+
author=Daniele Colanardi
4+
maintainer=Daniele Colanardi <dancol90@gmail.com>
5+
sentence=Let the ESP8266 ping a remote machine.
6+
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).
7+
category=Communication
8+
url=
9+
architectures=esp8266

src/ESP8266Ping.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
#include "ESP8266Ping.h"
3+
4+
extern "C" void esp_schedule();
5+
extern "C" void esp_yield();
6+
7+
PingClass::PingClass() {}
8+
9+
bool PingClass::ping(IPAddress dest, byte count) {
10+
_expected_count = count;
11+
_errors = 0;
12+
_success = 0;
13+
14+
_avg_time = 0;
15+
16+
memset(&_options, 0, sizeof(struct ping_option));
17+
18+
// Repeat count (how many time send a ping message to destination)
19+
_options.count = count;
20+
// Time interval between two ping (seconds??)
21+
_options.coarse_time = 1;
22+
// Destination machine
23+
_options.ip = dest;
24+
25+
// Callbacks
26+
_options.recv_function = reinterpret_cast<ping_recv_function>(&PingClass::_ping_recv_cb);
27+
_options.sent_function = NULL; //reinterpret_cast<ping_sent_function>(&_ping_sent_cb);
28+
29+
// Let's go!
30+
if(ping_start(&_options)) {
31+
// Suspend till the process end
32+
esp_yield();
33+
}
34+
35+
return (_success > 0);
36+
}
37+
38+
void PingClass::_ping_recv_cb(void *opt, void *resp) {
39+
// Cast the parameters to get some usable info
40+
ping_resp* ping_resp = reinterpret_cast<struct ping_resp*>(resp);
41+
ping_option* ping_opt = reinterpret_cast<struct ping_option*>(opt);
42+
43+
// Error or success?
44+
if (ping_resp->ping_err == -1)
45+
_errors++;
46+
else {
47+
_success++;
48+
_avg_time += ping_resp->resp_time;
49+
}
50+
51+
// Some debug info
52+
DEBUG_PING(
53+
"DEBUG: ping reply\n"
54+
"\ttotal_count = %d \n"
55+
"\tresp_time = %d \n"
56+
"\tseqno = %d \n"
57+
"\ttimeout_count = %d \n"
58+
"\tbytes = %d \n"
59+
"\ttotal_bytes = %d \n"
60+
"\ttotal_time = %d \n"
61+
"\tping_err = %d \n",
62+
ping_resp->total_count, ping_resp->resp_time, ping_resp->seqno,
63+
ping_resp->timeout_count, ping_resp->bytes, ping_resp->total_bytes,
64+
ping_resp->total_time, ping_resp->ping_err
65+
);
66+
67+
// Is it time to end?
68+
// Don't using seqno because it does not increase on error
69+
if (_success + _errors == _expected_count) {
70+
_avg_time = _avg_time / _expected_count;
71+
72+
DEBUG_PING("Avg resp time %d ms\n", _avg_time);
73+
74+
// Done, return to main functiom
75+
esp_schedule();
76+
}
77+
}
78+
79+
byte PingClass::_expected_count = 0;
80+
byte PingClass::_errors = 0;
81+
byte PingClass::_success = 0;
82+
int PingClass::_avg_time = 0;
83+
84+
PingClass Ping;

src/ESP8266Ping.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#ifndef ESP826Ping_H
3+
#define ESP826Ping_H
4+
5+
#include <Arduino.h>
6+
#include <ESP8266WiFi.h>
7+
8+
extern "C" {
9+
#include <ping.h>
10+
}
11+
12+
#define DEBUG_PING(...) Serial.printf(__VA_ARGS__)
13+
//#define DEBUG_PING(...)
14+
15+
class PingClass {
16+
public:
17+
PingClass();
18+
19+
bool ping(IPAddress dest, byte count = 5);
20+
21+
protected:
22+
static void _ping_sent_cb(void *opt, void *pdata);
23+
static void _ping_recv_cb(void *opt, void *pdata);
24+
25+
IPAddress _dest;
26+
ping_option _options;
27+
28+
static byte _expected_count, _errors, _success;
29+
static int _avg_time;
30+
};
31+
32+
extern PingClass Ping;
33+
34+
#endif

0 commit comments

Comments
 (0)
0