Skip to content

Commit de8c34c

Browse files
committed
set connected state after checking internet availability
use ping for all connectivity compile fix for esp8266 and esp32 remove check ping for mkrnb
1 parent 9dc0fe5 commit de8c34c

4 files changed

+87
-24
lines changed

src/CatM1ConnectionHandler.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
6464
pinMode(ON_MKR2, OUTPUT);
6565
digitalWrite(ON_MKR2, HIGH);
6666
#endif
67-
return NetworkConnectionState::CONNECTING;
68-
}
6967

70-
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
71-
{
7268
if(!GSM.begin(
7369
_settings.catm1.pin,
7470
_settings.catm1.apn,
@@ -80,8 +76,30 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
8076
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
8177
return NetworkConnectionState::ERROR;
8278
}
83-
Debug.print(DBG_INFO, F("Connected to Network"));
84-
return NetworkConnectionState::CONNECTED;
79+
80+
81+
return NetworkConnectionState::CONNECTING;
82+
}
83+
84+
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
85+
{
86+
if (!GSM.isConnected())
87+
{
88+
return NetworkConnectionState::INIT;
89+
}
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+
{
94+
Debug.print(DBG_ERROR, F("Internet check failed"));
95+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
96+
return NetworkConnectionState::CONNECTING;
97+
}
98+
else
99+
{
100+
Debug.print(DBG_INFO, F("Connected to Internet"));
101+
return NetworkConnectionState::CONNECTED;
102+
}
85103
}
86104

87105
NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()

src/CellularConnectionHandler.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,27 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit()
6262
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
6363
return NetworkConnectionState::ERROR;
6464
}
65-
return NetworkConnectionState::CONNECTING;
66-
}
6765

68-
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
69-
{
7066
if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.pass))) {
7167
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
7268
return NetworkConnectionState::ERROR;
7369
}
7470
Debug.print(DBG_INFO, F("Connected to Network"));
71+
return NetworkConnectionState::CONNECTING;
72+
}
73+
74+
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
75+
{
76+
if (!_cellular.isConnectedToInternet()) {
77+
return NetworkConnectionState::INIT;
78+
}
79+
80+
if(getTime() == 0){
81+
Debug.print(DBG_ERROR, F("Internet check failed"));
82+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
83+
return NetworkConnectionState::CONNECTING;
84+
}
85+
7586
return NetworkConnectionState::CONNECTED;
7687
}
7788

src/EthernetConnectionHandler.cpp

+26-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
2222
#include "EthernetConnectionHandler.h"
23+
#include <Udp.h>
2324

2425
/******************************************************************************
2526
CTOR/DTOR
@@ -72,11 +73,6 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
7273
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found."));
7374
return NetworkConnectionState::ERROR;
7475
}
75-
return NetworkConnectionState::CONNECTING;
76-
}
77-
78-
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
79-
{
8076
IPAddress ip(_settings.eth.ip.type, _settings.eth.ip.bytes);
8177

8278
// An ip address is provided -> static ip configuration
@@ -91,7 +87,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
9187
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
9288
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
9389
_settings.eth.timeout, _settings.eth.response_timeout);
94-
return NetworkConnectionState::CONNECTING;
90+
return NetworkConnectionState::INIT;
9591
}
9692
// An ip address is not provided -> dhcp configuration
9793
} else {
@@ -100,11 +96,33 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
10096
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
10197
_settings.eth.timeout, _settings.eth.response_timeout);
10298

103-
return NetworkConnectionState::CONNECTING;
99+
return NetworkConnectionState::INIT;
104100
}
105101
}
106102

107-
return NetworkConnectionState::CONNECTED;
103+
return NetworkConnectionState::CONNECTING;
104+
}
105+
106+
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
107+
{
108+
if (Ethernet.linkStatus() == LinkOFF) {
109+
return NetworkConnectionState::INIT;
110+
}
111+
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+
{
116+
Debug.print(DBG_ERROR, F("Internet check failed"));
117+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
118+
return NetworkConnectionState::CONNECTING;
119+
}
120+
else
121+
{
122+
Debug.print(DBG_INFO, F("Connected to Internet"));
123+
return NetworkConnectionState::CONNECTED;
124+
}
125+
108126
}
109127

110128
NetworkConnectionState EthernetConnectionHandler::update_handleConnected()

src/WiFiConnectionHandler.cpp

+22-6
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
9696
#else
9797
WiFi.mode(WIFI_STA);
9898
#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
99-
return NetworkConnectionState::CONNECTING;
100-
}
10199

102-
NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
103-
{
104100
if (WiFi.status() != WL_CONNECTED)
105101
{
106102
WiFi.begin(_settings.wifi.ssid, _settings.wifi.pwd);
@@ -120,7 +116,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
120116
Debug.print(DBG_ERROR, F("Connection to \"%s\" failed"), _settings.wifi.ssid);
121117
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
122118
#endif
123-
return NetworkConnectionState::CONNECTING;
119+
return NetworkConnectionState::INIT;
124120
}
125121
else
126122
{
@@ -130,8 +126,28 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
130126
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
131127
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
132128
#endif
133-
return NetworkConnectionState::CONNECTED;
129+
return NetworkConnectionState::CONNECTING;
130+
}
131+
}
132+
133+
NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
134+
{
135+
if (WiFi.status() != WL_CONNECTED){
136+
return NetworkConnectionState::INIT;
134137
}
138+
#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
139+
int ping_result = WiFi.ping("time.arduino.cc");
140+
Debug.print(DBG_INFO, F("WiFi.ping(): %d"), ping_result);
141+
if (ping_result < 0)
142+
{
143+
Debug.print(DBG_ERROR, F("Internet check failed"));
144+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
145+
return NetworkConnectionState::CONNECTING;
146+
}
147+
#endif
148+
Debug.print(DBG_INFO, F("Connected to Internet"));
149+
return NetworkConnectionState::CONNECTED;
150+
135151
}
136152

137153
NetworkConnectionState WiFiConnectionHandler::update_handleConnected()

0 commit comments

Comments
 (0)