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 diff --git a/src/CellularConnectionHandler.cpp b/src/CellularConnectionHandler.cpp index 2e4499a..7d27833 100644 --- a/src/CellularConnectionHandler.cpp +++ b/src/CellularConnectionHandler.cpp @@ -51,14 +51,61 @@ UDP & CellularConnectionHandler::getUDP() PROTECTED MEMBER FUNCTIONS ******************************************************************************/ +#if defined(ARDUINO_OPTA) +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) + 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; } 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