diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index 46a0c9d..2f0971c 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -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() { diff --git a/src/CatM1ConnectionHandler.cpp b/src/CatM1ConnectionHandler.cpp index 0263a39..c64f3a3 100644 --- a/src/CatM1ConnectionHandler.cpp +++ b/src/CatM1ConnectionHandler.cpp @@ -36,6 +36,7 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(const char * pin, const char * ap , _pass(pass) , _rat(rat) , _band(band) +, _reset(false) { } @@ -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; } /****************************************************************************** @@ -56,21 +60,44 @@ 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(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() @@ -78,6 +105,7 @@ 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; @@ -91,6 +119,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnecting() NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected() { + GSM.end(); if (_keep_alive) { return NetworkConnectionState::INIT; diff --git a/src/CatM1ConnectionHandler.h b/src/CatM1ConnectionHandler.h index 33ac9fe..d25ebd1 100644 --- a/src/CatM1ConnectionHandler.h +++ b/src/CatM1ConnectionHandler.h @@ -66,6 +66,7 @@ class CatM1ConnectionHandler : public ConnectionHandler RadioAccessTechnologyType _rat; uint32_t _band; + bool _reset; GSMUDP _gsm_udp; GSMClient _gsm_client;