Skip to content

Commit b2f7245

Browse files
committed
Added Ping functionality with ability to resolve names to IPs using m2m_ping_req() and hostByName().
Ping Reply Status constants: #define WL_PING_SUCCESS 0 #define WL_PING_DEST_UNREACHABLE 1 #define WL_PING_TIMEOUT 2 #define WL_PING_UNKNOWN_HOST 3 #define WL_PING_ERROR 4 Functions: uint8_t ping(const char* hostname, uint8_t ttl = 128); uint8_t ping(const String &hostname, uint8_t ttl = 128); uint8_t ping(IPAddress host, uint8_t ttl = 128); uint8_t pingGateway();
1 parent f9805e2 commit b2f7245

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

examples/WiFiPing/WiFiPing.ino

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
3+
This example connects to a encrypted Wifi network (WPA/WPA2).
4+
Then it prints the MAC address of the Wifi shield,
5+
the IP address obtained, and other network details.
6+
Then it continuously pings given IP Address.
7+
8+
Circuit:
9+
* WiFi shield attached / MKR1000
10+
11+
created 13 July 2010
12+
by dlf (Metodo2 srl)
13+
modified 13 May 2016
14+
by Petar Georgiev
15+
*/
16+
#include <SPI.h>
17+
#include <WiFi101.h>
18+
19+
char ssid[] = "yourNetwork"; // your network SSID (name)
20+
char pass[] = "secretPassword"; // your network password
21+
int status = WL_IDLE_STATUS; // the Wifi radio's status
22+
23+
String hostName = "www.google.com";
24+
byte pingResult;
25+
26+
void setup() {
27+
//Initialize serial and wait for port to open:
28+
Serial.begin(9600);
29+
while (!Serial) {
30+
; // wait for serial port to connect. Needed for native USB port only
31+
}
32+
33+
// check for the presence of the shield:
34+
if (WiFi.status() == WL_NO_SHIELD) {
35+
Serial.println("WiFi shield not present");
36+
// don't continue:
37+
while (true);
38+
}
39+
40+
// attempt to connect to Wifi network:
41+
while ( status != WL_CONNECTED) {
42+
Serial.print("Attempting to connect to WPA SSID: ");
43+
Serial.println(ssid);
44+
// Connect to WPA/WPA2 network:
45+
status = WiFi.begin(ssid, pass);
46+
47+
// wait 5 seconds for connection:
48+
delay(5000);
49+
}
50+
51+
// you're connected now, so print out the data:
52+
Serial.print("You're connected to the network");
53+
printCurrentNet();
54+
printWifiData();
55+
56+
}
57+
58+
void loop() {
59+
60+
Serial.print("Pinging ");
61+
Serial.print(hostName);
62+
Serial.print(": ");
63+
64+
pingResult = WiFi.ping(hostName);
65+
66+
if (pingResult == WL_PING_SUCCESS){
67+
Serial.println("SUCCESS");
68+
} else {
69+
switch (pingResult){
70+
case WL_PING_DEST_UNREACHABLE: { Serial.println("Destination host unreachable"); }; break;
71+
case WL_PING_TIMEOUT: { Serial.println("Request Timed Out"); }; break;
72+
case WL_PING_UNKNOWN_HOST: { Serial.println("Unable to resolve hostname to IP Address"); }; break;
73+
case WL_PING_ERROR: { Serial.println("Internal error"); }; break;
74+
};
75+
};
76+
77+
delay(3000);
78+
}
79+
80+
void printWifiData() {
81+
// print your WiFi shield's IP address:
82+
IPAddress ip = WiFi.localIP();
83+
Serial.print("IP Address: ");
84+
Serial.println(ip);
85+
Serial.println(ip);
86+
87+
// print your MAC address:
88+
byte mac[6];
89+
WiFi.macAddress(mac);
90+
Serial.print("MAC address: ");
91+
Serial.print(mac[5], HEX);
92+
Serial.print(":");
93+
Serial.print(mac[4], HEX);
94+
Serial.print(":");
95+
Serial.print(mac[3], HEX);
96+
Serial.print(":");
97+
Serial.print(mac[2], HEX);
98+
Serial.print(":");
99+
Serial.print(mac[1], HEX);
100+
Serial.print(":");
101+
Serial.println(mac[0], HEX);
102+
Serial.println();
103+
}
104+
105+
void printCurrentNet() {
106+
// print the SSID of the network you're attached to:
107+
Serial.print("SSID: ");
108+
Serial.println(WiFi.SSID());
109+
110+
// print the MAC address of the router you're attached to:
111+
byte bssid[6];
112+
WiFi.BSSID(bssid);
113+
Serial.print("BSSID: ");
114+
Serial.print(bssid[5], HEX);
115+
Serial.print(":");
116+
Serial.print(bssid[4], HEX);
117+
Serial.print(":");
118+
Serial.print(bssid[3], HEX);
119+
Serial.print(":");
120+
Serial.print(bssid[2], HEX);
121+
Serial.print(":");
122+
Serial.print(bssid[1], HEX);
123+
Serial.print(":");
124+
Serial.println(bssid[0], HEX);
125+
126+
// print the received signal strength:
127+
long rssi = WiFi.RSSI();
128+
Serial.print("signal strength (RSSI):");
129+
Serial.println(rssi);
130+
131+
// print the encryption type:
132+
byte encryption = WiFi.encryptionType();
133+
Serial.print("Encryption Type:");
134+
Serial.println(encryption, HEX);
135+
Serial.println();
136+
}

src/WiFi.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ static void resolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
147147
WiFi._resolve = hostIp;
148148
}
149149

150+
static void ping_cb(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode) {
151+
if (u8ErrorCode == 0)
152+
if (WiFi._resolve == u32IPAddr)
153+
WiFi._resolve = WL_PING_SUCCESS;
154+
else
155+
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
156+
else
157+
WiFi._resolve = u8ErrorCode;
158+
};
159+
150160
WiFiClass::WiFiClass()
151161
{
152162
_mode = WL_RESET_MODE;
@@ -723,4 +733,54 @@ void WiFiClass::refresh(void)
723733
m2m_wifi_handle_events(NULL);
724734
}
725735

736+
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl){
737+
IPAddress ip;
738+
if (hostByName(hostname, ip) > 0)
739+
return ping(ip, ttl);
740+
else
741+
return WL_PING_ERROR;
742+
};
743+
744+
uint8_t WiFiClass::ping(const String &hostname, uint8_t ttl){
745+
return ping(hostname.c_str(), ttl);
746+
};
747+
748+
uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
749+
750+
// Network led ON (rev A then rev B).
751+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);
752+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0);
753+
754+
uint32_t dstHost = (uint32_t)host;
755+
_resolve = dstHost;
756+
757+
if (m2m_ping_req((uint32_t)host, ttl, &ping_cb) < 0) {
758+
// Network led OFF (rev A then rev B).
759+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
760+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
761+
// Error sending ping request
762+
return WL_PING_ERROR;
763+
};
764+
765+
// Wait for connection or timeout:
766+
unsigned long start = millis();
767+
while (_resolve == dstHost && millis() - start < 5000) {
768+
m2m_wifi_handle_events(NULL);
769+
};
770+
771+
// Network led OFF (rev A then rev B).
772+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
773+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
774+
775+
if (_resolve == dstHost)
776+
return WL_PING_TIMEOUT;
777+
else
778+
return _resolve;
779+
};
780+
781+
uint8_t WiFiClass::pingGateway(){
782+
return ping(_gateway);
783+
784+
};
785+
726786
WiFiClass WiFi;

src/WiFi101.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ typedef enum {
6161
WL_AP_MODE
6262
} wl_mode_t;
6363

64+
#define WL_PING_SUCCESS 0
65+
#define WL_PING_DEST_UNREACHABLE 1
66+
#define WL_PING_TIMEOUT 2
67+
#define WL_PING_UNKNOWN_HOST 3
68+
#define WL_PING_ERROR 4
69+
6470
class WiFiClass
6571
{
6672
public:
@@ -138,6 +144,11 @@ class WiFiClass
138144
int hostByName(const char* hostname, IPAddress& result);
139145
int hostByName(const String &hostname, IPAddress& result) { return hostByName(hostname.c_str(), result); }
140146

147+
uint8_t ping(const char* hostname, uint8_t ttl = 128);
148+
uint8_t ping(const String &hostname, uint8_t ttl = 128);
149+
uint8_t ping(IPAddress host, uint8_t ttl = 128);
150+
uint8_t pingGateway();
151+
141152
void refresh(void);
142153

143154
private:

0 commit comments

Comments
 (0)