Skip to content

Add Cellular support for Portenta H7 and Portenta C33 #119

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

Merged
merged 5 commits into from
Jun 10, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- name: MKRGSM
- name: MKRNB
- name: MKRWAN
- name: Arduino_Cellular
ARDUINOCORE_MBED_STAGING_PATH: extras/ArduinoCore-mbed
ARDUINOCORE_API_STAGING_PATH: extras/ArduinoCore-API
SKETCHES_REPORTS_PATH: sketches-reports
Expand Down
2 changes: 2 additions & 0 deletions examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ NBConnectionHandler conMan(SECRET_PIN);
LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY);
#elif defined(BOARD_HAS_CATM1_NBIOT)
CatM1ConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS);
#elif defined(BOARD_HAS_CELLULAR)
CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
#endif

void setup() {
Expand Down
95 changes: 95 additions & 0 deletions src/Arduino_CellularConnectionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
This file is part of the Arduino_ConnectionHandler library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/


/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_CellularConnectionHandler.h"

#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */

/******************************************************************************
CTOR/DTOR
******************************************************************************/

CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::CELL}
, _pin(pin)
, _apn(apn)
, _login(login)
, _pass(pass)
{

}

/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/

unsigned long CellularConnectionHandler::getTime()
{
return _cellular.getCellularTime().getUNIXTimestamp();
}

UDP & CellularConnectionHandler::getUDP()
{
Debug.print(DBG_ERROR, F("CellularConnectionHandler has no UDP support"));
while(1) {};
}

/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/

NetworkConnectionState CellularConnectionHandler::update_handleInit()
{
_cellular.begin();
_cellular.setDebugStream(Serial);
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
{
if (!_cellular.connect(_apn, _login, _pass)) {
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
}
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTED;
}

NetworkConnectionState CellularConnectionHandler::update_handleConnected()
{
if (!_cellular.isConnectedToInternet()) {
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
}

NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting()
{
return NetworkConnectionState::DISCONNECTED;
}

NetworkConnectionState CellularConnectionHandler::update_handleDisconnected()
{
if (_keep_alive) {
return NetworkConnectionState::INIT;
}
return NetworkConnectionState::CLOSED;
}

#endif /* #ifdef BOARD_HAS_CELLULAR */
61 changes: 61 additions & 0 deletions src/Arduino_CellularConnectionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
This file is part of the Arduino_ConnectionHandler library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/


#ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_
#define ARDUINO_CELLULAR_CONNECTION_HANDLER_H_

/******************************************************************************
INCLUDE
******************************************************************************/

#include "Arduino_ConnectionHandler.h"

#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */

/******************************************************************************
CLASS DECLARATION
******************************************************************************/

class CellularConnectionHandler : public ConnectionHandler
{
public:

CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true);


virtual unsigned long getTime() override;
virtual Client & getClient() override { return _gsm_client; };
virtual UDP & getUDP() override;


protected:

virtual NetworkConnectionState update_handleInit () override;
virtual NetworkConnectionState update_handleConnecting () override;
virtual NetworkConnectionState update_handleConnected () override;
virtual NetworkConnectionState update_handleDisconnecting() override;
virtual NetworkConnectionState update_handleDisconnected () override;


private:

const char * _pin;
const char * _apn;
const char * _login;
const char * _pass;

ArduinoCellular _cellular;
TinyGsmClient _gsm_client = _cellular.getNetworkClient();
};

#endif /* #ifdef BOARD_HAS_CELLULAR */

#endif /* #ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ */
19 changes: 13 additions & 6 deletions src/Arduino_ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@
#include <Ethernet.h>
#include <PortentaEthernet.h>
#include <GSM.h>
#include <Arduino_Cellular.h>

#define BOARD_HAS_WIFI
#define BOARD_HAS_ETHERNET
#define BOARD_HAS_CATM1_NBIOT
#define BOARD_HAS_CELLULAR
#define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD
#define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
Expand All @@ -73,9 +75,11 @@
#include <WiFiUdp.h>
#include <EthernetC33.h>
#include <EthernetUdp.h>
#include <Arduino_Cellular.h>

#define BOARD_HAS_WIFI
#define BOARD_HAS_ETHERNET
#define BOARD_HAS_CELLULAR
#define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
Expand Down Expand Up @@ -150,7 +154,7 @@
#if defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WiFiUdp.h>

#define BOARD_HAS_WIFI
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
Expand Down Expand Up @@ -201,7 +205,8 @@ enum class NetworkAdapter {
NB,
GSM,
LORA,
CATM1
CATM1,
CELL
};

typedef void (*OnNetworkEventCallback)();
Expand Down Expand Up @@ -237,13 +242,11 @@ class ConnectionHandler {

NetworkConnectionState check();

#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)
#if !defined(BOARD_HAS_LORA)
virtual unsigned long getTime() = 0;
virtual Client &getClient() = 0;
virtual UDP &getUDP() = 0;
#endif

#if defined(BOARD_HAS_LORA)
#else
virtual int write(const uint8_t *buf, size_t size) = 0;
virtual int read() = 0;
virtual bool available() = 0;
Expand Down Expand Up @@ -304,4 +307,8 @@ class ConnectionHandler {
#include "Arduino_CatM1ConnectionHandler.h"
#endif

#if defined(BOARD_HAS_CELLULAR)
#include "Arduino_CellularConnectionHandler.h"
#endif

#endif /* CONNECTION_HANDLER_H_ */
Loading