Skip to content

Commit 6676cfd

Browse files
committed
Merge pull request #68 from PKGeorgiev/master
Added Ping functionality with ability to resolve names to IPs using m2m_ping_req() and hostByName().
2 parents 95a8a26 + ec51ae9 commit 6676cfd

File tree

3 files changed

+212
-1
lines changed

3 files changed

+212
-1
lines changed

examples/WiFiPing/WiFiPing.ino

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 host specified by IP Address or name.
7+
8+
Circuit:
9+
* WiFi shield attached / MKR1000
10+
11+
created 13 July 2010
12+
by dlf (Metodo2 srl)
13+
modified 09 June 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+
// Specify IP address or hostname
24+
String hostName = "www.google.com";
25+
byte pingResult;
26+
27+
void setup() {
28+
// Initialize serial and wait for port to open:
29+
Serial.begin(9600);
30+
while (!Serial) {
31+
; // wait for serial port to connect. Needed for native USB port only
32+
}
33+
34+
// check for the presence of the shield:
35+
if (WiFi.status() == WL_NO_SHIELD) {
36+
Serial.println("WiFi shield not present");
37+
// don't continue:
38+
while (true);
39+
}
40+
41+
// attempt to connect to Wifi network:
42+
while ( status != WL_CONNECTED) {
43+
Serial.print("Attempting to connect to WPA SSID: ");
44+
Serial.println(ssid);
45+
// Connect to WPA/WPA2 network:
46+
status = WiFi.begin(ssid, pass);
47+
48+
// wait 5 seconds for connection:
49+
delay(5000);
50+
}
51+
52+
// you're connected now, so print out the data:
53+
Serial.print("You're connected to the network");
54+
printCurrentNet();
55+
printWifiData();
56+
57+
}
58+
59+
void loop() {
60+
61+
Serial.print("Pinging ");
62+
Serial.print(hostName);
63+
Serial.print(": ");
64+
65+
pingResult = WiFi.ping(hostName);
66+
67+
if (pingResult == WL_PING_SUCCESS) {
68+
Serial.println("SUCCESS!");
69+
} else {
70+
Serial.print("FAILED! Error code: ");
71+
Serial.println(pingResult);
72+
};
73+
74+
delay(3000);
75+
}
76+
77+
void printWifiData() {
78+
// print your WiFi shield's IP address:
79+
IPAddress ip = WiFi.localIP();
80+
Serial.print("IP Address: ");
81+
Serial.println(ip);
82+
Serial.println(ip);
83+
84+
// print your MAC address:
85+
byte mac[6];
86+
WiFi.macAddress(mac);
87+
Serial.print("MAC address: ");
88+
Serial.print(mac[5], HEX);
89+
Serial.print(":");
90+
Serial.print(mac[4], HEX);
91+
Serial.print(":");
92+
Serial.print(mac[3], HEX);
93+
Serial.print(":");
94+
Serial.print(mac[2], HEX);
95+
Serial.print(":");
96+
Serial.print(mac[1], HEX);
97+
Serial.print(":");
98+
Serial.println(mac[0], HEX);
99+
Serial.println();
100+
}
101+
102+
void printCurrentNet() {
103+
// print the SSID of the network you're attached to:
104+
Serial.print("SSID: ");
105+
Serial.println(WiFi.SSID());
106+
107+
// print the MAC address of the router you're attached to:
108+
byte bssid[6];
109+
WiFi.BSSID(bssid);
110+
Serial.print("BSSID: ");
111+
Serial.print(bssid[5], HEX);
112+
Serial.print(":");
113+
Serial.print(bssid[4], HEX);
114+
Serial.print(":");
115+
Serial.print(bssid[3], HEX);
116+
Serial.print(":");
117+
Serial.print(bssid[2], HEX);
118+
Serial.print(":");
119+
Serial.print(bssid[1], HEX);
120+
Serial.print(":");
121+
Serial.println(bssid[0], HEX);
122+
123+
// print the received signal strength:
124+
long rssi = WiFi.RSSI();
125+
Serial.print("signal strength (RSSI):");
126+
Serial.println(rssi);
127+
128+
// print the encryption type:
129+
byte encryption = WiFi.encryptionType();
130+
Serial.print("Encryption Type:");
131+
Serial.println(encryption, HEX);
132+
Serial.println();
133+
}

src/WiFi.cpp

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
extern "C" {
2323
#include "bsp/include/nm_bsp.h"
2424
#include "socket/include/socket_buffer.h"
25+
#include "socket/include/m2m_socket_host_if.h"
2526
#include "driver/source/nmasic.h"
26-
#include "driver/include/m2m_periph.h"
27+
#include "driver/include/m2m_periph.h"
2728
}
2829

2930
static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
@@ -147,6 +148,26 @@ static void resolve_cb(uint8_t * /* hostName */, uint32_t hostIp)
147148
WiFi._resolve = hostIp;
148149
}
149150

151+
static void ping_cb(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode) {
152+
if (PING_ERR_SUCCESS == u8ErrorCode){
153+
// Ensure this ICMP reply comes from requested IP address
154+
if (WiFi._resolve == u32IPAddr)
155+
WiFi._resolve = WL_PING_SUCCESS;
156+
else
157+
// Another network device replied to the our ICMP request
158+
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
159+
}
160+
else if (PING_ERR_DEST_UNREACH == u8ErrorCode) {
161+
WiFi._resolve = WL_PING_DEST_UNREACHABLE;
162+
}
163+
else if (PING_ERR_TIMEOUT == u8ErrorCode) {
164+
WiFi._resolve = WL_PING_TIMEOUT;
165+
}
166+
else {
167+
WiFi._resolve = WL_PING_ERROR;
168+
};
169+
};
170+
150171
WiFiClass::WiFiClass()
151172
{
152173
_mode = WL_RESET_MODE;
@@ -740,4 +761,49 @@ void WiFiClass::refresh(void)
740761
m2m_wifi_handle_events(NULL);
741762
}
742763

764+
uint8_t WiFiClass::ping(const char* hostname, uint8_t ttl){
765+
IPAddress ip;
766+
if (hostByName(hostname, ip) > 0)
767+
return ping(ip, ttl);
768+
else
769+
return WL_PING_UNKNOWN_HOST;
770+
};
771+
772+
uint8_t WiFiClass::ping(const String &hostname, uint8_t ttl){
773+
return ping(hostname.c_str(), ttl);
774+
};
775+
776+
uint8_t WiFiClass::ping(IPAddress host, uint8_t ttl) {
777+
778+
// Network led ON (rev A then rev B).
779+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);
780+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0);
781+
782+
uint32_t dstHost = (uint32_t)host;
783+
_resolve = dstHost;
784+
785+
if (m2m_ping_req((uint32_t)host, ttl, &ping_cb) < 0) {
786+
// Network led OFF (rev A then rev B).
787+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
788+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
789+
// Error sending ping request
790+
return WL_PING_ERROR;
791+
};
792+
793+
// Wait for connection or timeout:
794+
unsigned long start = millis();
795+
while (_resolve == dstHost && millis() - start < 5000) {
796+
m2m_wifi_handle_events(NULL);
797+
};
798+
799+
// Network led OFF (rev A then rev B).
800+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
801+
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
802+
803+
if (_resolve == dstHost)
804+
return WL_PING_TIMEOUT;
805+
else
806+
return (wl_ping_result_t)_resolve;
807+
};
808+
743809
WiFiClass WiFi;

src/WiFi101.h

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

64+
typedef enum {
65+
WL_PING_SUCCESS = 0,
66+
WL_PING_DEST_UNREACHABLE,
67+
WL_PING_TIMEOUT,
68+
WL_PING_UNKNOWN_HOST,
69+
WL_PING_ERROR
70+
} wl_ping_result_t;
71+
6472
class WiFiClass
6573
{
6674
public:
@@ -139,6 +147,10 @@ class WiFiClass
139147
int hostByName(const char* hostname, IPAddress& result);
140148
int hostByName(const String &hostname, IPAddress& result) { return hostByName(hostname.c_str(), result); }
141149

150+
uint8_t ping(const char* hostname, uint8_t ttl = 128);
151+
uint8_t ping(const String &hostname, uint8_t ttl = 128);
152+
uint8_t ping(IPAddress host, uint8_t ttl = 128);
153+
142154
void refresh(void);
143155

144156
private:

0 commit comments

Comments
 (0)