Skip to content

Commit 85f4374

Browse files
committed
use ping for all connectivity
1 parent 379adfb commit 85f4374

4 files changed

+31
-81
lines changed

Diff for: src/CatM1ConnectionHandler.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
8787
{
8888
return NetworkConnectionState::INIT;
8989
}
90-
91-
if(getTime() == 0){
90+
int ping_result = GSM.ping("time.arduino.cc");
91+
Debug.print(DBG_INFO, F("GSM.ping(): %d"), ping_result);
92+
if (ping_result < 0)
93+
{
9294
Debug.print(DBG_ERROR, F("Internet check failed"));
9395
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
9496
return NetworkConnectionState::CONNECTING;
9597
}
96-
97-
Debug.print(DBG_INFO, F("Connected to Internet"));
98-
return NetworkConnectionState::CONNECTED;
98+
else
99+
{
100+
Debug.print(DBG_INFO, F("Connected to Internet"));
101+
return NetworkConnectionState::CONNECTED;
102+
}
99103
}
100104

101105
NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()

Diff for: src/EthernetConnectionHandler.cpp

+10-31
Original file line numberDiff line numberDiff line change
@@ -108,42 +108,21 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
108108
if (Ethernet.linkStatus() == LinkOFF) {
109109
return NetworkConnectionState::INIT;
110110
}
111-
// Request time from NTP server for testing internet connection
112-
UDP &udp = getUDP();
113-
udp.begin(4001);
114-
uint8_t ntp_packet_buf[48] = {0};
115-
116-
ntp_packet_buf[0] = 0b11100011;
117-
ntp_packet_buf[1] = 0;
118-
ntp_packet_buf[2] = 6;
119-
ntp_packet_buf[3] = 0xEC;
120-
ntp_packet_buf[12] = 49;
121-
ntp_packet_buf[13] = 0x4E;
122-
ntp_packet_buf[14] = 49;
123-
ntp_packet_buf[15] = 52;
124-
125-
udp.beginPacket("time.arduino.cc", 123);
126-
udp.write(ntp_packet_buf, 48);
127-
udp.endPacket();
128-
129-
bool is_timeout = false;
130-
unsigned long const start = millis();
131-
do
132-
{
133-
is_timeout = (millis() - start) >= 1000;
134-
} while(!is_timeout && !udp.parsePacket());
135111

136-
if(is_timeout) {
137-
udp.stop();
112+
int ping_result = Ethernet.ping("time.arduino.cc");
113+
Debug.print(DBG_INFO, F("Ethernet.ping(): %d"), ping_result);
114+
if (ping_result < 0)
115+
{
138116
Debug.print(DBG_ERROR, F("Internet check failed"));
139117
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
140118
return NetworkConnectionState::CONNECTING;
141119
}
142-
143-
udp.read(ntp_packet_buf, 48);
144-
udp.stop();
145-
Debug.print(DBG_INFO, F("Connected to Internet"));
146-
return NetworkConnectionState::CONNECTED;
120+
else
121+
{
122+
Debug.print(DBG_INFO, F("Connected to Internet"));
123+
return NetworkConnectionState::CONNECTED;
124+
}
125+
147126
}
148127

149128
NetworkConnectionState EthernetConnectionHandler::update_handleConnected()

Diff for: src/NBConnectionHandler.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,19 @@ NetworkConnectionState NBConnectionHandler::update_handleConnecting()
123123
return NetworkConnectionState::INIT;
124124
}
125125

126-
if(getTime() == 0){
126+
int ping_result = _nb_gprs.ping("time.arduino.cc");
127+
Debug.print(DBG_INFO, F("GPRS.ping(): %d"), ping_result);
128+
if (ping_result < 0)
129+
{
127130
Debug.print(DBG_ERROR, F("Internet check failed"));
128131
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
129132
return NetworkConnectionState::CONNECTING;
130133
}
131-
132-
return NetworkConnectionState::CONNECTED;
134+
else
135+
{
136+
Debug.print(DBG_INFO, F("Connected to Internet"));
137+
return NetworkConnectionState::CONNECTED;
138+
}
133139
}
134140

135141
NetworkConnectionState NBConnectionHandler::update_handleConnected()

Diff for: src/WiFiConnectionHandler.cpp

+3-42
Original file line numberDiff line numberDiff line change
@@ -132,53 +132,14 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
132132

133133
NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
134134
{
135-
if (WiFi.status() != NETWORK_CONNECTED){
135+
if (WiFi.status() != WL_CONNECTED){
136136
return NetworkConnectionState::INIT;
137137
}
138-
bool checkSuccess = false;
139-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M7) || \
140-
defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
141-
// Request time from NTP server for testing internet connection
142-
UDP &udp = getUDP();
143-
udp.begin(4001);
144-
uint8_t ntp_packet_buf[48] = {0};
145138

146-
ntp_packet_buf[0] = 0b11100011;
147-
ntp_packet_buf[1] = 0;
148-
ntp_packet_buf[2] = 6;
149-
ntp_packet_buf[3] = 0xEC;
150-
ntp_packet_buf[12] = 49;
151-
ntp_packet_buf[13] = 0x4E;
152-
ntp_packet_buf[14] = 49;
153-
ntp_packet_buf[15] = 52;
154-
udp.beginPacket("time.arduino.cc", 123);
155-
udp.write(ntp_packet_buf, 48);
156-
udp.endPacket();
157-
158-
bool is_timeout = false;
159-
unsigned long const start = millis();
160-
do
161-
{
162-
is_timeout = (millis() - start) >= 1000;
163-
} while(!is_timeout && !udp.parsePacket());
164-
if(is_timeout) {
165-
udp.stop();
166-
}
167-
else
168-
{
169-
udp.read(ntp_packet_buf, 48);
170-
udp.stop();
171-
checkSuccess = true;
172-
}
173-
#else
174-
int const ping_result = WiFi.ping("time.arduino.cc");
139+
int ping_result = WiFi.ping("time.arduino.cc");
175140
Debug.print(DBG_INFO, F("WiFi.ping(): %d"), ping_result);
176-
if (ping_result > 0)
141+
if (ping_result < 0)
177142
{
178-
checkSuccess = true;
179-
}
180-
#endif
181-
if(!checkSuccess){
182143
Debug.print(DBG_ERROR, F("Internet check failed"));
183144
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
184145
return NetworkConnectionState::CONNECTING;

0 commit comments

Comments
 (0)