|
| 1 | +/* |
| 2 | + Web ICMP Ping |
| 3 | +
|
| 4 | + This sketch pings a device based on the IP address or the hostname |
| 5 | + using the WiFi module. By default the attempt is performed 5 times, but can |
| 6 | + be changed to max. 255 |
| 7 | +
|
| 8 | + It requires at least version 0.5.0 of USB Wifi bridge firmware and WiFiS3 library. |
| 9 | +
|
| 10 | + This example is written for a network using WPA encryption. For |
| 11 | + WEP or WPA, change the WiFi.begin() call accordingly. |
| 12 | +
|
| 13 | + created 14 February 2024 |
| 14 | + by paulvha |
| 15 | +
|
| 16 | + updated 27 February 2025 |
| 17 | + by Fabik111 |
| 18 | +
|
| 19 | + */ |
| 20 | + |
| 21 | +#include "WiFiC3.h" |
| 22 | +#include "arduino_secrets.h" |
| 23 | + |
| 24 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 25 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 26 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 27 | +int keyIndex = 0; // your network key index number (needed only for WEP) |
| 28 | + |
| 29 | +int status = WL_IDLE_STATUS; |
| 30 | + |
| 31 | +/* -------------------------------------------------------------------------- */ |
| 32 | +void setup() { |
| 33 | +/* -------------------------------------------------------------------------- */ |
| 34 | + //Initialize serial and wait for port to open: |
| 35 | + Serial.begin(115200); |
| 36 | + while (!Serial) { |
| 37 | + ; // wait for serial port to connect. Needed for native USB port only |
| 38 | + } |
| 39 | + |
| 40 | + // check for the WiFi module: |
| 41 | + if (WiFi.status() == WL_NO_MODULE) { |
| 42 | + Serial.println("Communication with WiFi module failed."); |
| 43 | + // don't continue |
| 44 | + while (true); |
| 45 | + } |
| 46 | + |
| 47 | + // attempt to connect to WiFi network: |
| 48 | + while (status != WL_CONNECTED) { |
| 49 | + Serial.print("Attempting to connect to SSID: "); |
| 50 | + Serial.println(ssid); |
| 51 | + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: |
| 52 | + status = WiFi.begin(ssid, pass); |
| 53 | + |
| 54 | + // wait 10 seconds for connection: |
| 55 | + delay(10000); |
| 56 | + } |
| 57 | + |
| 58 | + printWifiStatus(); |
| 59 | +} |
| 60 | + |
| 61 | +/* -------------------------------------------------------------------------- */ |
| 62 | +void loop() { |
| 63 | +/* -------------------------------------------------------------------------- */ |
| 64 | + |
| 65 | + // Ping IP |
| 66 | + const IPAddress remote_ip(140,82,121,4); |
| 67 | + Serial.print("Trying to ping github.com on IP: "); |
| 68 | + Serial.println(remote_ip); |
| 69 | + |
| 70 | + // using default ping count of 1 |
| 71 | + int res = WiFi.ping(remote_ip); |
| 72 | + |
| 73 | + if (res > 0) { |
| 74 | + Serial.print("Ping response time: "); |
| 75 | + Serial.print(res); |
| 76 | + Serial.println(" ms"); |
| 77 | + } |
| 78 | + else { |
| 79 | + Serial.println("Timeout on IP!"); |
| 80 | + } |
| 81 | + |
| 82 | + // Ping Host |
| 83 | + const char* remote_host = "www.google.com"; |
| 84 | + Serial.print("Trying to ping host: "); |
| 85 | + Serial.println(remote_host); |
| 86 | + |
| 87 | + // setting ttl to 128 and ping count to 10 |
| 88 | + int res1 = WiFi.ping(remote_host); |
| 89 | + |
| 90 | + if (res1 > 0) { |
| 91 | + Serial.print("Ping average response time: "); |
| 92 | + Serial.print(res1); |
| 93 | + Serial.println(" ms"); |
| 94 | + } |
| 95 | + else { |
| 96 | + Serial.println("Timeout on host!"); |
| 97 | + } |
| 98 | + |
| 99 | + Serial.println(); |
| 100 | + delay(1000); |
| 101 | +} |
| 102 | + |
| 103 | +/* -------------------------------------------------------------------------- */ |
| 104 | +void printWifiStatus() { |
| 105 | +/* -------------------------------------------------------------------------- */ |
| 106 | + // print the SSID of the network you're attached to: |
| 107 | + Serial.print("SSID: "); |
| 108 | + Serial.println(WiFi.SSID()); |
| 109 | + |
| 110 | + // print your board's IP address: |
| 111 | + IPAddress ip = WiFi.localIP(); |
| 112 | + Serial.print("IP Address: "); |
| 113 | + Serial.println(ip); |
| 114 | + |
| 115 | + // print the received signal strength: |
| 116 | + long rssi = WiFi.RSSI(); |
| 117 | + Serial.print("signal strength (RSSI):"); |
| 118 | + Serial.print(rssi); |
| 119 | + Serial.println(" dBm"); |
| 120 | +} |
0 commit comments