Skip to content

Commit 8a981d5

Browse files
authored
Merge pull request #7 from AlbyIanna/mkrnb1500
Add NBConnectionHandler for MKR NB1500
2 parents 21540b3 + 5354aaf commit 8a981d5

File tree

8 files changed

+344
-6
lines changed

8 files changed

+344
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Arduino Library for network connections management
22

3-
Library for handling and managing network connections by providing keepAlive functionalities and reconnection attempts. Initially for WiFi and GSM, its goal is to be extended to more networking layers over diverse hardware (Ethernet, BlueTooth et al.)
3+
Library for handling and managing network connections by providing keepAlive functionalities and reconnection attempts. Initially for WiFi, GSM and NB, its goal is to be extended to more networking layers over diverse hardware (Ethernet, BlueTooth et al.)
44

55

66
## Usage
@@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3131
**Connection Handler** is configured via a series of compiler directives, including the correct implementation of the class based on which board is selected.
3232

3333
### How to use it
34-
- Instantiate the class with `ConnectionHandler conHandler(SECRET_SSID, SECRET_PASS);` if you are using a WiFi board, otherwise replace **WiFi** with **GSM** or any future implementation minding to carefully pass the init parameters to the constructor (i.e.: for the GSM you'll need to pass PIN, APN, login, password).
34+
- Instantiate the class with `ConnectionHandler conHandler(SECRET_SSID, SECRET_PASS);` if you are using a WiFi board, otherwise replace **WiFi** with **GSM** or **NB** or any future implementation minding to carefully pass the init parameters to the constructor (i.e.: for the GSM you'll need to pass PIN, APN, login, password).
3535

3636
- The `update()` method does all the work. It uses a finite state machine and is responsible for connection and reconnection to a network. The method is designed to be non-blocking by using time (milliseconds) to perform its tasks.
3737

examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
need a GSMConnectionHandler object as follows
1717
1818
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER,
19-
SECRET_GSM_PASS)
19+
SECRET_GSM_PASS);
20+
21+
If using a MKR NB1500 you'll need a NBConnectionHandler object as follows
22+
23+
NBConnectionHandler conMan(SECRET_PIN);
2024
*/
2125

2226
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
@@ -66,4 +70,4 @@ void onNetworkConnect(void *_arg) {
6670

6771
void onNetworkDisconnect(void *_arg) {
6872
Serial.println(">>>> DISCONNECTED from network");
69-
}
73+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Client KEYWORD1
99
ConnectionHandler KEYWORD1
1010
WiFiConnectionHandler KEYWORD1
1111
GSMConnectionHandler KEYWORD1
12+
NBConnectionHandler KEYWORD1
1213
EthernetConnectionHandler KEYWORD1
1314

1415
####################################################

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name=Arduino_ConnectionHandler
22
version=0.1.4
33
author=Ubi de Feo, Cristian Maglie, Andrea Catozzi, Alexander Entinger et al.
44
maintainer=Arduino.cc
5-
sentence=Arduino Library for network connection management (WiFi, GSM, [Ethernet])
5+
sentence=Arduino Library for network connection management (WiFi, GSM, NB, [Ethernet])
66
paragraph=Originally part of ArduinoIoTCloud
77
category=Communication
88
url=https://github.com/arduino-libraries/Arduino_ConnectionHandler

src/Arduino_ConnectionHandler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ class ConnectionHandler {
113113
#define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY
114114
#endif
115115

116+
#ifdef ARDUINO_SAMD_MKRNB1500
117+
#include <MKRNB.h>
118+
#define BOARD_HAS_NB
119+
#define NETWORK_HARDWARE_ERROR
120+
#define NETWORK_IDLE_STATUS NB_NetworkStatus_t::IDLE
121+
#define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY
122+
#endif
123+
116124
#if defined(ARDUINO_ESP8266_ESP12) \
117125
|| defined(ESP8266) \
118126
|| defined(ESP8266_ESP01) \
@@ -159,6 +167,8 @@ class ConnectionHandler {
159167
#include "Arduino_WiFiConnectionHandler.h"
160168
#elif defined(BOARD_HAS_GSM)
161169
#include "Arduino_GSMConnectionHandler.h"
170+
#elif defined(BOARD_HAS_NB)
171+
#include "Arduino_NBConnectionHandler.h"
162172
#endif
163173

164174
#endif /* CONNECTION_HANDLER_H_ */

src/Arduino_GSMConnectionHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ class GSMConnectionHandler : public ConnectionHandler {
8888

8989
#endif /* #ifdef BOARD_HAS_GSM */
9090

91-
#endif /* GSM_CONNECTION_MANAGER_H_ */
91+
#endif /* #ifndef GSM_CONNECTION_MANAGER_H_ */

src/Arduino_NBConnectionHandler.cpp

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
/******************************************************************************
19+
INCLUDE
20+
******************************************************************************/
21+
22+
/*
23+
static int const DBG_NONE = -1;
24+
static int const DBG_ERROR = 0;
25+
static int const DBG_WARNING = 1;
26+
static int const DBG_INFO = 2;
27+
static int const DBG_DEBUG = 3;
28+
static int const DBG_VERBOSE = 4;
29+
*/
30+
31+
#include "Arduino_NBConnectionHandler.h"
32+
33+
#ifdef BOARD_HAS_NB /* Only compile if this is a board with NB */
34+
35+
/******************************************************************************
36+
CONSTANTS
37+
******************************************************************************/
38+
39+
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
40+
41+
/******************************************************************************
42+
CTOR/DTOR
43+
******************************************************************************/
44+
45+
NBConnectionHandler::NBConnectionHandler(const char *pin, bool _keepAlive) :
46+
pin(pin),
47+
lastConnectionTickTime(millis()),
48+
connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
49+
keepAlive(_keepAlive),
50+
_on_connect_event_callback(NULL),
51+
_on_disconnect_event_callback(NULL),
52+
_on_error_event_callback(NULL) {
53+
}
54+
55+
/******************************************************************************
56+
PUBLIC MEMBER FUNCTIONS
57+
******************************************************************************/
58+
59+
void NBConnectionHandler::init() {
60+
char msgBuffer[120];
61+
if (nbAccess.begin(pin) == NB_READY) {
62+
Debug.print(DBG_INFO, "SIM card ok");
63+
nbAccess.setTimeout(CHECK_INTERVAL_RETRYING);
64+
changeConnectionState(NetworkConnectionState::CONNECTING);
65+
} else {
66+
Debug.print(DBG_ERROR, "SIM not present or wrong PIN");
67+
while (1);
68+
}
69+
}
70+
71+
void NBConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
72+
switch (event) {
73+
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
74+
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
75+
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
76+
case NetworkConnectionEvent::INIT: ; break;
77+
case NetworkConnectionEvent::CONNECTING: ; break;
78+
case NetworkConnectionEvent::DISCONNECTING: ; break;
79+
case NetworkConnectionEvent::CLOSED: ; break;
80+
}
81+
}
82+
83+
void NBConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) {
84+
_on_connect_event_callback = callback;
85+
}
86+
void NBConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
87+
_on_disconnect_event_callback = callback;
88+
}
89+
void NBConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
90+
_on_error_event_callback = callback;
91+
}
92+
93+
void NBConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) {
94+
if (callback) {
95+
(*callback)(callback_arg);
96+
}
97+
}
98+
99+
unsigned long NBConnectionHandler::getTime() {
100+
return nbAccess.getTime();
101+
}
102+
103+
void NBConnectionHandler::update() {
104+
unsigned long const now = millis();
105+
int nbAccessAlive;
106+
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
107+
switch (netConnectionState) {
108+
case NetworkConnectionState::INIT: {
109+
init();
110+
}
111+
break;
112+
113+
case NetworkConnectionState::CONNECTING: {
114+
// NOTE: Blocking Call when 4th parameter == true
115+
NB_NetworkStatus_t networkStatus;
116+
networkStatus = gprs.attachGPRS();
117+
Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", networkStatus);
118+
if (networkStatus == NB_NetworkStatus_t::ERROR) {
119+
// NO FURTHER ACTION WILL FOLLOW THIS
120+
changeConnectionState(NetworkConnectionState::ERROR);
121+
return;
122+
}
123+
Debug.print(DBG_INFO, "Sending PING to outer space...");
124+
int pingResult;
125+
// pingResult = gprs.ping("time.arduino.cc");
126+
// Debug.print(DBG_INFO, "NB.ping(): %d", pingResult);
127+
// if (pingResult < 0) {
128+
if (pingResult < 0) {
129+
Debug.print(DBG_ERROR, "PING failed");
130+
Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
131+
return;
132+
} else {
133+
Debug.print(DBG_INFO, "Connected to GPRS Network");
134+
changeConnectionState(NetworkConnectionState::CONNECTED);
135+
return;
136+
}
137+
}
138+
break;
139+
case NetworkConnectionState::CONNECTED: {
140+
nbAccessAlive = nbAccess.isAccessAlive();
141+
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nbAccessAlive);
142+
if (nbAccessAlive != 1) {
143+
changeConnectionState(NetworkConnectionState::DISCONNECTED);
144+
return;
145+
}
146+
Debug.print(DBG_VERBOSE, "Connected to Cellular Network");
147+
}
148+
break;
149+
case NetworkConnectionState::DISCONNECTED: {
150+
//gprs.detachGPRS();
151+
if (keepAlive) {
152+
Debug.print(DBG_VERBOSE, "keep alive > INIT");
153+
changeConnectionState(NetworkConnectionState::INIT);
154+
} else {
155+
changeConnectionState(NetworkConnectionState::CLOSED);
156+
}
157+
//changeConnectionState(NetworkConnectionState::CONNECTING);
158+
}
159+
break;
160+
}
161+
lastConnectionTickTime = now;
162+
}
163+
}
164+
165+
/******************************************************************************
166+
PRIVATE MEMBER FUNCTIONS
167+
******************************************************************************/
168+
169+
void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState) {
170+
int newInterval = CHECK_INTERVAL_IDLE;
171+
switch (_newState) {
172+
case NetworkConnectionState::INIT: {
173+
newInterval = CHECK_INTERVAL_INIT;
174+
}
175+
break;
176+
case NetworkConnectionState::CONNECTING: {
177+
Debug.print(DBG_INFO, "Connecting to Cellular Network");
178+
newInterval = CHECK_INTERVAL_CONNECTING;
179+
}
180+
break;
181+
case NetworkConnectionState::CONNECTED: {
182+
execNetworkEventCallback(_on_connect_event_callback, 0);
183+
newInterval = CHECK_INTERVAL_CONNECTED;
184+
}
185+
break;
186+
case NetworkConnectionState::GETTIME: {
187+
}
188+
break;
189+
case NetworkConnectionState::DISCONNECTING: {
190+
Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network");
191+
nbAccess.shutdown();
192+
}
193+
case NetworkConnectionState::DISCONNECTED: {
194+
if (netConnectionState == NetworkConnectionState::CONNECTED) {
195+
execNetworkEventCallback(_on_disconnect_event_callback, 0);
196+
Debug.print(DBG_ERROR, "Disconnected from Cellular Network");
197+
Debug.print(DBG_ERROR, "Attempting reconnection");
198+
if (keepAlive) {
199+
Debug.print(DBG_ERROR, "Attempting reconnection");
200+
}
201+
}
202+
newInterval = CHECK_INTERVAL_DISCONNECTED;
203+
}
204+
break;
205+
case NetworkConnectionState::ERROR: {
206+
execNetworkEventCallback(_on_error_event_callback, 0);
207+
Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board.");
208+
}
209+
break;
210+
}
211+
connectionTickTimeInterval = newInterval;
212+
lastConnectionTickTime = millis();
213+
netConnectionState = _newState;
214+
}
215+
216+
217+
void NBConnectionHandler::connect() {
218+
if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) {
219+
return;
220+
}
221+
keepAlive = true;
222+
changeConnectionState(NetworkConnectionState::INIT);
223+
224+
}
225+
void NBConnectionHandler::disconnect() {
226+
//WiFi.end();
227+
228+
changeConnectionState(NetworkConnectionState::DISCONNECTING);
229+
keepAlive = false;
230+
}
231+
232+
#endif /* #ifdef BOARD_HAS_NB */

0 commit comments

Comments
 (0)