diff --git a/Sming/Arch/Esp8266/Components/esp-open-lwip/component.mk b/Sming/Arch/Esp8266/Components/esp-open-lwip/component.mk index 13ba166b61..e99342f5b8 100644 --- a/Sming/Arch/Esp8266/Components/esp-open-lwip/component.mk +++ b/Sming/Arch/Esp8266/Components/esp-open-lwip/component.mk @@ -53,7 +53,8 @@ COMPONENT_SRCFILES := \ lwip/core/ipv4/ip_frag.c \ lwip/netif/etharp.c \ \ - lwip/app/dhcpserver.c + lwip/app/dhcpserver.c \ + lwip/app/ping.c ifeq ($(ENABLE_ESPCONN),1) COMPONENT_SRCFILES += \ diff --git a/samples/Network_Ping/Makefile b/samples/Network_Ping/Makefile new file mode 100644 index 0000000000..ff51b6c3a7 --- /dev/null +++ b/samples/Network_Ping/Makefile @@ -0,0 +1,9 @@ +##################################################################### +#### Please don't change this file. Use component.mk instead #### +##################################################################### + +ifndef SMING_HOME +$(error SMING_HOME is not set: please configure it as an environment variable) +endif + +include $(SMING_HOME)/project.mk diff --git a/samples/Network_Ping/README.rst b/samples/Network_Ping/README.rst new file mode 100644 index 0000000000..db2f004dd8 --- /dev/null +++ b/samples/Network_Ping/README.rst @@ -0,0 +1,4 @@ +Network Ping +============ + +Sample demonstrating the usage of a network ping to check for connectivity issues. diff --git a/samples/Network_Ping/app/application.cpp b/samples/Network_Ping/app/application.cpp new file mode 100644 index 0000000000..1d86662d11 --- /dev/null +++ b/samples/Network_Ping/app/application.cpp @@ -0,0 +1,98 @@ +#include +extern "C" { +#include +} + +// If you want, you can define WiFi settings globally in Eclipse Environment Variables +#ifndef WIFI_SSID +#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and Password here +#define WIFI_PWD "PleaseEnterPass" +#endif + +namespace +{ +ping_option pingOptions; +uint8_t failedAttempts; +constexpr uint8_t PING_COUNT = 3; +constexpr uint8_t MAX_FAILED_ATTEMTPS = 5; +constexpr uint8_t PING_INTERVAL_SECONDS = 10; +constexpr uint8_t RESTART_DELAY_SECONDS = 2; + +Timer procTimer; + +void ping(uint32 ip); + +void pingTask() +{ + ping(IpAddress("8.8.8.8")); // ping Google DNS servers +} + +void onSent(void* arg, void* pdata) +{ + ping_resp* response = reinterpret_cast(pdata); + + if(response == nullptr) { + debug_e("Invalid onSent call"); + return; + } + + Serial.printf("Ping sent. Total failed attempts: %d.\n", failedAttempts); + if(failedAttempts / response->total_count > MAX_FAILED_ATTEMTPS) { + debug_d("Scheduling system restart in %d seconds.", RESTART_DELAY_SECONDS); + // schedule restart + System.restart(RESTART_DELAY_SECONDS * 1000); + return; + } + + debug_d("Scheduling another ping in %d seconds", PING_INTERVAL_SECONDS); + procTimer.initializeMs(PING_INTERVAL_SECONDS * 1000, pingTask).startOnce(); +} + +void onReceived(void* arg, void* pdata) +{ + ping_resp* response = reinterpret_cast(pdata); + if(response == nullptr) { + debug_e("Invalid onReceived call"); + return; + } + + Serial.printf("Ping received. Sequence: %d, Success: %d, Elapsed time: %d", response->seqno, + response->ping_err == 0, response->total_time); + Serial.println(); + + if(response->ping_err) { + failedAttempts++; + } else { + failedAttempts = 0; + } +} + +void ping(uint32_t ip) +{ + debug_d("Ping IP: %s", IpAddress(ip).toString().c_str()); + pingOptions.ip = ip; + pingOptions.count = PING_COUNT; + pingOptions.recv_function = onReceived; + pingOptions.sent_function = onSent; + ping_start(&pingOptions); +} + +void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway) +{ + debug_d("Scheduling initial ping in 1 second."); + procTimer.initializeMs(1000, pingTask).startOnce(); +} + +} // namespace + +void init() +{ + Serial.begin(SERIAL_BAUD_RATE); + Serial.systemDebugOutput(true); + + // Setup the WIFI connection + WifiStation.enable(true); + WifiStation.config(WIFI_SSID, WIFI_PWD); + + WifiEvents.onStationGotIP(connectOk); +} diff --git a/samples/Network_Ping/component.mk b/samples/Network_Ping/component.mk new file mode 100644 index 0000000000..16ad0adfd3 --- /dev/null +++ b/samples/Network_Ping/component.mk @@ -0,0 +1,2 @@ +COMPONENT_DEPENDS := Network +COMPONENT_SOC := esp8266