Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial network ping sample (ESP8266 only). #2513

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sming/Arch/Esp8266/Components/esp-open-lwip/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 += \
Expand Down
9 changes: 9 additions & 0 deletions samples/Network_Ping/Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions samples/Network_Ping/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Network Ping
============

Sample demonstrating the usage of a network ping to check for connectivity issues.
98 changes: 98 additions & 0 deletions samples/Network_Ping/app/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <SmingCore.h>
extern "C" {
#include <lwip/app/ping.h>
}

// 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<ping_resp*>(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<ping_resp*>(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);
}
2 changes: 2 additions & 0 deletions samples/Network_Ping/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPONENT_DEPENDS := Network
COMPONENT_SOC := esp8266