diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index 46a0c9d4..edc9c11d 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/library.properties b/library.properties index 9ad0dc11..d8c527c9 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Originally part of ArduinoIoTCloud category=Communication url=https://github.com/arduino-libraries/Arduino_ConnectionHandler architectures=samd,esp32,esp8266,mbed,megaavr,mbed_nano,mbed_portenta,mbed_nicla,mbed_opta,mbed_giga,renesas_portenta,renesas_uno,mbed_edge,stm32,rp2040 -depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN, Blues Wireless Notecard (>=1.6.3) +depends=Arduino_OptaCellular, Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN, Blues Wireless Notecard (>=1.6.3) diff --git a/src/CellularConnectionHandler.cpp b/src/CellularConnectionHandler.cpp index b73f5866..020fc330 100644 --- a/src/CellularConnectionHandler.cpp +++ b/src/CellularConnectionHandler.cpp @@ -54,10 +54,56 @@ 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 (strlen(_settings.cell.pin) > 0 && !_cellular.unlockSIM(_settings.cell.pin)) { DEBUG_ERROR(F("SIM not present or wrong PIN")); return NetworkConnectionState::ERROR; @@ -67,6 +113,7 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit() DEBUG_ERROR(F("The board was not able to register to the network...")); return NetworkConnectionState::ERROR; } +#endif DEBUG_INFO(F("Connected to Network")); return NetworkConnectionState::CONNECTING; } diff --git a/src/CellularConnectionHandler.h b/src/CellularConnectionHandler.h index 7a5f00b1..2594c8a2 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 @@ -53,7 +55,16 @@ class CellularConnectionHandler : public ConnectionHandler private: + const char * _pin; + 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 1534e473..588bbd4f 100644 --- a/src/ConnectionHandlerDefinitions.h +++ b/src/ConnectionHandlerDefinitions.h @@ -74,6 +74,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