Skip to content

Commit 8e4216f

Browse files
authored
Merge pull request #119 from pennam/cellular
Add Cellular support for Portenta H7 and Portenta C33
2 parents 5022993 + 075c689 commit 8e4216f

File tree

5 files changed

+172
-6
lines changed

5 files changed

+172
-6
lines changed

Diff for: .github/workflows/compile-examples.yml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- name: MKRGSM
3535
- name: MKRNB
3636
- name: MKRWAN
37+
- name: Arduino_Cellular
3738
ARDUINOCORE_MBED_STAGING_PATH: extras/ArduinoCore-mbed
3839
ARDUINOCORE_API_STAGING_PATH: extras/ArduinoCore-API
3940
SKETCHES_REPORTS_PATH: sketches-reports

Diff for: examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ NBConnectionHandler conMan(SECRET_PIN);
4343
LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY);
4444
#elif defined(BOARD_HAS_CATM1_NBIOT)
4545
CatM1ConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS);
46+
#elif defined(BOARD_HAS_CELLULAR)
47+
CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
4648
#endif
4749

4850
void setup() {

Diff for: src/Arduino_CellularConnectionHandler.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
This file is part of the Arduino_ConnectionHandler library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
12+
/******************************************************************************
13+
INCLUDE
14+
******************************************************************************/
15+
16+
#include "Arduino_CellularConnectionHandler.h"
17+
18+
#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */
19+
20+
/******************************************************************************
21+
CTOR/DTOR
22+
******************************************************************************/
23+
24+
CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
25+
: ConnectionHandler{keep_alive, NetworkAdapter::CELL}
26+
, _pin(pin)
27+
, _apn(apn)
28+
, _login(login)
29+
, _pass(pass)
30+
{
31+
32+
}
33+
34+
/******************************************************************************
35+
PUBLIC MEMBER FUNCTIONS
36+
******************************************************************************/
37+
38+
unsigned long CellularConnectionHandler::getTime()
39+
{
40+
return _cellular.getCellularTime().getUNIXTimestamp();
41+
}
42+
43+
UDP & CellularConnectionHandler::getUDP()
44+
{
45+
Debug.print(DBG_ERROR, F("CellularConnectionHandler has no UDP support"));
46+
while(1) {};
47+
}
48+
49+
/******************************************************************************
50+
PROTECTED MEMBER FUNCTIONS
51+
******************************************************************************/
52+
53+
NetworkConnectionState CellularConnectionHandler::update_handleInit()
54+
{
55+
_cellular.begin();
56+
_cellular.setDebugStream(Serial);
57+
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
58+
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
59+
return NetworkConnectionState::ERROR;
60+
}
61+
return NetworkConnectionState::CONNECTING;
62+
}
63+
64+
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
65+
{
66+
if (!_cellular.connect(_apn, _login, _pass)) {
67+
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
68+
return NetworkConnectionState::ERROR;
69+
}
70+
Debug.print(DBG_INFO, F("Connected to Network"));
71+
return NetworkConnectionState::CONNECTED;
72+
}
73+
74+
NetworkConnectionState CellularConnectionHandler::update_handleConnected()
75+
{
76+
if (!_cellular.isConnectedToInternet()) {
77+
return NetworkConnectionState::DISCONNECTED;
78+
}
79+
return NetworkConnectionState::CONNECTED;
80+
}
81+
82+
NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting()
83+
{
84+
return NetworkConnectionState::DISCONNECTED;
85+
}
86+
87+
NetworkConnectionState CellularConnectionHandler::update_handleDisconnected()
88+
{
89+
if (_keep_alive) {
90+
return NetworkConnectionState::INIT;
91+
}
92+
return NetworkConnectionState::CLOSED;
93+
}
94+
95+
#endif /* #ifdef BOARD_HAS_CELLULAR */

Diff for: src/Arduino_CellularConnectionHandler.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This file is part of the Arduino_ConnectionHandler library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
12+
#ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_
13+
#define ARDUINO_CELLULAR_CONNECTION_HANDLER_H_
14+
15+
/******************************************************************************
16+
INCLUDE
17+
******************************************************************************/
18+
19+
#include "Arduino_ConnectionHandler.h"
20+
21+
#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */
22+
23+
/******************************************************************************
24+
CLASS DECLARATION
25+
******************************************************************************/
26+
27+
class CellularConnectionHandler : public ConnectionHandler
28+
{
29+
public:
30+
31+
CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true);
32+
33+
34+
virtual unsigned long getTime() override;
35+
virtual Client & getClient() override { return _gsm_client; };
36+
virtual UDP & getUDP() override;
37+
38+
39+
protected:
40+
41+
virtual NetworkConnectionState update_handleInit () override;
42+
virtual NetworkConnectionState update_handleConnecting () override;
43+
virtual NetworkConnectionState update_handleConnected () override;
44+
virtual NetworkConnectionState update_handleDisconnecting() override;
45+
virtual NetworkConnectionState update_handleDisconnected () override;
46+
47+
48+
private:
49+
50+
const char * _pin;
51+
const char * _apn;
52+
const char * _login;
53+
const char * _pass;
54+
55+
ArduinoCellular _cellular;
56+
TinyGsmClient _gsm_client = _cellular.getNetworkClient();
57+
};
58+
59+
#endif /* #ifdef BOARD_HAS_CELLULAR */
60+
61+
#endif /* #ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ */

Diff for: src/Arduino_ConnectionHandler.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@
5757
#include <Ethernet.h>
5858
#include <PortentaEthernet.h>
5959
#include <GSM.h>
60+
#include <Arduino_Cellular.h>
6061

6162
#define BOARD_HAS_WIFI
6263
#define BOARD_HAS_ETHERNET
6364
#define BOARD_HAS_CATM1_NBIOT
65+
#define BOARD_HAS_CELLULAR
6466
#define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD
6567
#define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET
6668
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
@@ -73,9 +75,11 @@
7375
#include <WiFiUdp.h>
7476
#include <EthernetC33.h>
7577
#include <EthernetUdp.h>
78+
#include <Arduino_Cellular.h>
7679

7780
#define BOARD_HAS_WIFI
7881
#define BOARD_HAS_ETHERNET
82+
#define BOARD_HAS_CELLULAR
7983
#define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET
8084
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
8185
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
@@ -150,7 +154,7 @@
150154
#if defined(ARDUINO_ARCH_ESP32)
151155
#include <WiFi.h>
152156
#include <WiFiUdp.h>
153-
157+
154158
#define BOARD_HAS_WIFI
155159
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
156160
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
@@ -201,7 +205,8 @@ enum class NetworkAdapter {
201205
NB,
202206
GSM,
203207
LORA,
204-
CATM1
208+
CATM1,
209+
CELL
205210
};
206211

207212
typedef void (*OnNetworkEventCallback)();
@@ -237,13 +242,11 @@ class ConnectionHandler {
237242

238243
NetworkConnectionState check();
239244

240-
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)
245+
#if !defined(BOARD_HAS_LORA)
241246
virtual unsigned long getTime() = 0;
242247
virtual Client &getClient() = 0;
243248
virtual UDP &getUDP() = 0;
244-
#endif
245-
246-
#if defined(BOARD_HAS_LORA)
249+
#else
247250
virtual int write(const uint8_t *buf, size_t size) = 0;
248251
virtual int read() = 0;
249252
virtual bool available() = 0;
@@ -304,4 +307,8 @@ class ConnectionHandler {
304307
#include "Arduino_CatM1ConnectionHandler.h"
305308
#endif
306309

310+
#if defined(BOARD_HAS_CELLULAR)
311+
#include "Arduino_CellularConnectionHandler.h"
312+
#endif
313+
307314
#endif /* CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)