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

CatM1: allow retry if connection fails #131

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 47 additions & 1 deletion examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,53 @@ void loop() {
* which might not guarantee the correct functioning of the ConnectionHandler
* object.
*/
conMan.check();
if (conMan.check() != NetworkConnectionState::CONNECTED) {
return;
}

#if !defined(BOARD_HAS_LORA)
Client &client = conMan.getClient();
/* arduino.tips */
IPAddress ip = IPAddress(104, 21, 62, 246);
int port = 80;

Serial.println("\nStarting connection to server...");
/* if you get a connection, report back via serial: */
if (!client.connect(ip, port)) {
Serial.println("unable to connect to server");
return;
}

Serial.println("connected to server");
/* Make a HTTP request: */
size_t w = client.println("GET /asciilogo.txt HTTP/1.1");
w += client.println("Host: arduino.tips");
w += client.println("User-Agent: Arduino");
w += client.println("Connection: close");
w += client.println();
Serial.print("Write size is ");
Serial.println(w);

/* if there are incoming bytes available from the server,
* read them and print them:
*/
while (client.connected()) {
size_t len = client.available();
if (len) {
uint8_t buff[len];
client.read(buff, len);
Serial.write(buff, len);
}
delay(0);
}

/* if the server's disconnected, stop the client: */
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
delay(1000);
#endif

}

void onNetworkConnect() {
Expand Down
41 changes: 35 additions & 6 deletions src/CatM1ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(const char * pin, const char * ap
, _pass(pass)
, _rat(rat)
, _band(band)
, _reset(false)
{

}
Expand All @@ -46,7 +47,10 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(const char * pin, const char * ap

unsigned long CatM1ConnectionHandler::getTime()
{
return GSM.getTime();
/* It is not safe to call GSM.getTime() since we don't know if modem internal
* RTC is in sync with current time.
*/
return 0;
}

/******************************************************************************
Expand All @@ -56,28 +60,52 @@ unsigned long CatM1ConnectionHandler::getTime()
NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
{
#if defined (ARDUINO_EDGE_CONTROL)
/* Power on module */
pinMode(ON_MKR2, OUTPUT);
digitalWrite(ON_MKR2, HIGH);
#endif

if(!GSM.begin(_pin, _apn, _login, _pass, _rat, _band, _reset))
{
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
_reset = true;
return NetworkConnectionState::DISCONNECTED;
}
_reset = false;
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
{
if(!GSM.begin(_pin, _apn, _login, _pass, _rat, _band))
int const is_gsm_access_alive = GSM.isConnected();
if (is_gsm_access_alive != 1)
{
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
Debug.print(DBG_ERROR, F("GSM connection not alive... disconnecting"));
return NetworkConnectionState::DISCONNECTED;
}

Debug.print(DBG_INFO, F("Sending PING to outer space..."));
int const ping_result = GSM.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("GSM.ping(): %d"), ping_result);
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("PING failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), 2 * CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
return NetworkConnectionState::CONNECTING;
}
else
{
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTED;
}
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTED;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()
{
int const is_gsm_access_alive = GSM.isConnected();
if (is_gsm_access_alive != 1)
{
Debug.print(DBG_ERROR, F("GSM connection not alive... disconnecting"));
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
Expand All @@ -91,6 +119,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnecting()

NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected()
{
GSM.end();
if (_keep_alive)
{
return NetworkConnectionState::INIT;
Expand Down
1 change: 1 addition & 0 deletions src/CatM1ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CatM1ConnectionHandler : public ConnectionHandler

RadioAccessTechnologyType _rat;
uint32_t _band;
bool _reset;

GSMUDP _gsm_udp;
GSMClient _gsm_client;
Expand Down
Loading