From 0f21423710a599de3be06d61cc085a81208f897f Mon Sep 17 00:00:00 2001 From: maidnl Date: Tue, 28 Jan 2025 17:18:26 +0100 Subject: [PATCH 1/4] introducing Cellular possibility on Connection Handler Demo --- examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index 46a0c9d..edc9c11 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -35,7 +35,8 @@ #define CONN_TOGGLE_MS 60000 #if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ - defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) + defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT) \ + || defined(BOARD_HAS_CELLULAR)) #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" #endif From 502e12dff5b8ff2b865f445ffae064aab2ae24de Mon Sep 17 00:00:00 2001 From: maidnl Date: Tue, 28 Jan 2025 17:19:41 +0100 Subject: [PATCH 2/4] Introducing Arduino Opta Cellular as possible connection source for Arduino Opta --- src/CellularConnectionHandler.cpp | 31 ++++++++++++++++++++++++++++++ src/CellularConnectionHandler.h | 8 +++++++- src/ConnectionHandlerDefinitions.h | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/CellularConnectionHandler.cpp b/src/CellularConnectionHandler.cpp index 2e4499a..9ddf35a 100644 --- a/src/CellularConnectionHandler.cpp +++ b/src/CellularConnectionHandler.cpp @@ -51,9 +51,40 @@ UDP & CellularConnectionHandler::getUDP() PROTECTED MEMBER FUNCTIONS ******************************************************************************/ +#if defined(ARDUINO_OPTA) && defined(BOARD_HAS_CELLULAR) +CellularExpansion ce; +static void beginOptaCellular() { + static bool first_call = true; + + if(first_call) { + first_call = false; + OptaController.registerCustomExpansion(CellularExpansion::getProduct(), + CellularExpansion::makeExpansion, + CellularExpansion::startUp); + OptaController.begin(); + delay(500); + for (int i = 0; i < OptaController.getExpansionNum(); i++) { + ce = OptaController.getExpansion(i); + if(ce) { + ce.ctrlModem(true); + delay(100); + break; + } + } + } + else { + OptaController.update(); + } +} +#endif + NetworkConnectionState CellularConnectionHandler::update_handleInit() { +#if defined(ARDUINO_OPTA) && defined(BOARD_HAS_CELLULAR) + beginOptaCellular(); +#else _cellular.begin(); +#endif _cellular.setDebugStream(Serial); if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) { Debug.print(DBG_ERROR, F("SIM not present or wrong PIN")); diff --git a/src/CellularConnectionHandler.h b/src/CellularConnectionHandler.h index 2addd29..725cd81 100644 --- a/src/CellularConnectionHandler.h +++ b/src/CellularConnectionHandler.h @@ -20,6 +20,8 @@ #if defined(ARDUINO_PORTENTA_C33) || defined(ARDUINO_PORTENTA_H7_M7) #include +#elif defined(ARDUINO_OPTA) +#include #endif #ifndef BOARD_HAS_CELLULAR @@ -57,8 +59,12 @@ class CellularConnectionHandler : public ConnectionHandler const char * _apn; const char * _login; const char * _pass; - + + #if defined(ARDUINO_OPTA) + ArduinoCellular &_cellular = CellularExpansion::getCellular(); + #else ArduinoCellular _cellular; + #endif TinyGsmClient _gsm_client = _cellular.getNetworkClient(); }; diff --git a/src/ConnectionHandlerDefinitions.h b/src/ConnectionHandlerDefinitions.h index 4f29559..8743c81 100644 --- a/src/ConnectionHandlerDefinitions.h +++ b/src/ConnectionHandlerDefinitions.h @@ -81,6 +81,7 @@ #if defined(ARDUINO_OPTA) #define BOARD_HAS_WIFI #define BOARD_HAS_ETHERNET + #define BOARD_HAS_CELLULAR #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD #define NETWORK_IDLE_STATUS WL_IDLE_STATUS #define NETWORK_CONNECTED WL_CONNECTED From 5e782a9375af3c8ce9679c62fe05a120ada42b51 Mon Sep 17 00:00:00 2001 From: Daniele <34984733+maidnl@users.noreply.github.com> Date: Wed, 29 Jan 2025 11:19:11 +0100 Subject: [PATCH 3/4] Update src/CellularConnectionHandler.cpp removed redundant preprocessor #if defined Co-authored-by: Andrea Gilardoni <4046444+andreagilardoni@users.noreply.github.com> --- src/CellularConnectionHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CellularConnectionHandler.cpp b/src/CellularConnectionHandler.cpp index 9ddf35a..3e68fd1 100644 --- a/src/CellularConnectionHandler.cpp +++ b/src/CellularConnectionHandler.cpp @@ -51,7 +51,7 @@ UDP & CellularConnectionHandler::getUDP() PROTECTED MEMBER FUNCTIONS ******************************************************************************/ -#if defined(ARDUINO_OPTA) && defined(BOARD_HAS_CELLULAR) +#if defined(ARDUINO_OPTA) CellularExpansion ce; static void beginOptaCellular() { static bool first_call = true; From a94ac93d8d3e85226905e76019b9c18452673ab5 Mon Sep 17 00:00:00 2001 From: maidnl Date: Fri, 31 Jan 2025 14:08:29 +0100 Subject: [PATCH 4/4] handled the case the Opta Cellular is not present If the Opta Cellular is not present the Opta crashed due to some impossible communication or timeout in the _cellular.unlockSIM() function call With this change if the Cellular is not present no useless call to _cellular.unlockSIM() is performed and this prevent the crash --- src/CellularConnectionHandler.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/CellularConnectionHandler.cpp b/src/CellularConnectionHandler.cpp index 3e68fd1..7d27833 100644 --- a/src/CellularConnectionHandler.cpp +++ b/src/CellularConnectionHandler.cpp @@ -80,16 +80,32 @@ static void beginOptaCellular() { NetworkConnectionState CellularConnectionHandler::update_handleInit() { -#if defined(ARDUINO_OPTA) && defined(BOARD_HAS_CELLULAR) +#if defined(ARDUINO_OPTA) beginOptaCellular(); #else _cellular.begin(); #endif _cellular.setDebugStream(Serial); + +#if defined(ARDUINO_OPTA) + /* in case Opta Cellular is not wired this check on ce prevent the call + to unlockSIM that cause a crash in the Opta (deep due to the missing + communication with the modem) */ + if(ce) { + if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) { + Debug.print(DBG_ERROR, F("SIM not present or wrong PIN")); + return NetworkConnectionState::ERROR; + } + } + else { + return NetworkConnectionState::ERROR; + } +#else if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) { Debug.print(DBG_ERROR, F("SIM not present or wrong PIN")); return NetworkConnectionState::ERROR; } +#endif return NetworkConnectionState::CONNECTING; }