Skip to content

add NBConnectionHandler for mkrnb1500 #7

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 1 commit into from
Dec 20, 2019
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Arduino Library for network connections management

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.)
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.)


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

### How to use it
- 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).
- 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).

- 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.

Expand Down
8 changes: 6 additions & 2 deletions examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
need a GSMConnectionHandler object as follows

GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER,
SECRET_GSM_PASS)
SECRET_GSM_PASS);

If using a MKR NB1500 you'll need a NBConnectionHandler object as follows

NBConnectionHandler conMan(SECRET_PIN);
*/

WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
Expand Down Expand Up @@ -66,4 +70,4 @@ void onNetworkConnect(void *_arg) {

void onNetworkDisconnect(void *_arg) {
Serial.println(">>>> DISCONNECTED from network");
}
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Client KEYWORD1
ConnectionHandler KEYWORD1
WiFiConnectionHandler KEYWORD1
GSMConnectionHandler KEYWORD1
NBConnectionHandler KEYWORD1
EthernetConnectionHandler KEYWORD1

####################################################
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name=Arduino_ConnectionHandler
version=0.1.4
author=Ubi de Feo, Cristian Maglie, Andrea Catozzi, Alexander Entinger et al.
maintainer=Arduino.cc
sentence=Arduino Library for network connection management (WiFi, GSM, [Ethernet])
sentence=Arduino Library for network connection management (WiFi, GSM, NB, [Ethernet])
paragraph=Originally part of ArduinoIoTCloud
category=Communication
url=https://github.com/arduino-libraries/Arduino_ConnectionHandler
Expand Down
10 changes: 10 additions & 0 deletions src/Arduino_ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ class ConnectionHandler {
#define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY
#endif

#ifdef ARDUINO_SAMD_MKRNB1500
#include <MKRNB.h>
#define BOARD_HAS_NB
#define NETWORK_HARDWARE_ERROR
#define NETWORK_IDLE_STATUS NB_NetworkStatus_t::IDLE
#define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY
#endif

#if defined(ARDUINO_ESP8266_ESP12) \
|| defined(ESP8266) \
|| defined(ESP8266_ESP01) \
Expand Down Expand Up @@ -159,6 +167,8 @@ class ConnectionHandler {
#include "Arduino_WiFiConnectionHandler.h"
#elif defined(BOARD_HAS_GSM)
#include "Arduino_GSMConnectionHandler.h"
#elif defined(BOARD_HAS_NB)
#include "Arduino_NBConnectionHandler.h"
#endif

#endif /* CONNECTION_HANDLER_H_ */
2 changes: 1 addition & 1 deletion src/Arduino_GSMConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ class GSMConnectionHandler : public ConnectionHandler {

#endif /* #ifdef BOARD_HAS_GSM */

#endif /* GSM_CONNECTION_MANAGER_H_ */
#endif /* #ifndef GSM_CONNECTION_MANAGER_H_ */
232 changes: 232 additions & 0 deletions src/Arduino_NBConnectionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2019 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to license@arduino.cc.
*/

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

/*
static int const DBG_NONE = -1;
static int const DBG_ERROR = 0;
static int const DBG_WARNING = 1;
static int const DBG_INFO = 2;
static int const DBG_DEBUG = 3;
static int const DBG_VERBOSE = 4;
*/

#include "Arduino_NBConnectionHandler.h"

#ifdef BOARD_HAS_NB /* Only compile if this is a board with NB */

/******************************************************************************
CONSTANTS
******************************************************************************/

static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;

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

NBConnectionHandler::NBConnectionHandler(const char *pin, bool _keepAlive) :
pin(pin),
lastConnectionTickTime(millis()),
connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
keepAlive(_keepAlive),
_on_connect_event_callback(NULL),
_on_disconnect_event_callback(NULL),
_on_error_event_callback(NULL) {
}

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

void NBConnectionHandler::init() {
char msgBuffer[120];
if (nbAccess.begin(pin) == NB_READY) {
Debug.print(DBG_INFO, "SIM card ok");
nbAccess.setTimeout(CHECK_INTERVAL_RETRYING);
changeConnectionState(NetworkConnectionState::CONNECTING);
} else {
Debug.print(DBG_ERROR, "SIM not present or wrong PIN");
while (1);
}
}

void NBConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
switch (event) {
case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break;
case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break;
case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break;
case NetworkConnectionEvent::INIT: ; break;
case NetworkConnectionEvent::CONNECTING: ; break;
case NetworkConnectionEvent::DISCONNECTING: ; break;
case NetworkConnectionEvent::CLOSED: ; break;
}
}

void NBConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) {
_on_connect_event_callback = callback;
}
void NBConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
_on_disconnect_event_callback = callback;
}
void NBConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) {
_on_error_event_callback = callback;
}

void NBConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) {
if (callback) {
(*callback)(callback_arg);
}
}

unsigned long NBConnectionHandler::getTime() {
return nbAccess.getTime();
}

void NBConnectionHandler::update() {
unsigned long const now = millis();
int nbAccessAlive;
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
switch (netConnectionState) {
case NetworkConnectionState::INIT: {
init();
}
break;

case NetworkConnectionState::CONNECTING: {
// NOTE: Blocking Call when 4th parameter == true
NB_NetworkStatus_t networkStatus;
networkStatus = gprs.attachGPRS();
Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", networkStatus);
if (networkStatus == NB_NetworkStatus_t::ERROR) {
// NO FURTHER ACTION WILL FOLLOW THIS
changeConnectionState(NetworkConnectionState::ERROR);
return;
}
Debug.print(DBG_INFO, "Sending PING to outer space...");
int pingResult;
// pingResult = gprs.ping("time.arduino.cc");
// Debug.print(DBG_INFO, "NB.ping(): %d", pingResult);
// if (pingResult < 0) {
if (pingResult < 0) {
Debug.print(DBG_ERROR, "PING failed");
Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval);
return;
} else {
Debug.print(DBG_INFO, "Connected to GPRS Network");
changeConnectionState(NetworkConnectionState::CONNECTED);
return;
}
}
break;
case NetworkConnectionState::CONNECTED: {
nbAccessAlive = nbAccess.isAccessAlive();
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nbAccessAlive);
if (nbAccessAlive != 1) {
changeConnectionState(NetworkConnectionState::DISCONNECTED);
return;
}
Debug.print(DBG_VERBOSE, "Connected to Cellular Network");
}
break;
case NetworkConnectionState::DISCONNECTED: {
//gprs.detachGPRS();
if (keepAlive) {
Debug.print(DBG_VERBOSE, "keep alive > INIT");
changeConnectionState(NetworkConnectionState::INIT);
} else {
changeConnectionState(NetworkConnectionState::CLOSED);
}
//changeConnectionState(NetworkConnectionState::CONNECTING);
}
break;
}
lastConnectionTickTime = now;
}
}

/******************************************************************************
PRIVATE MEMBER FUNCTIONS
******************************************************************************/

void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState) {
int newInterval = CHECK_INTERVAL_IDLE;
switch (_newState) {
case NetworkConnectionState::INIT: {
newInterval = CHECK_INTERVAL_INIT;
}
break;
case NetworkConnectionState::CONNECTING: {
Debug.print(DBG_INFO, "Connecting to Cellular Network");
newInterval = CHECK_INTERVAL_CONNECTING;
}
break;
case NetworkConnectionState::CONNECTED: {
execNetworkEventCallback(_on_connect_event_callback, 0);
newInterval = CHECK_INTERVAL_CONNECTED;
}
break;
case NetworkConnectionState::GETTIME: {
}
break;
case NetworkConnectionState::DISCONNECTING: {
Debug.print(DBG_VERBOSE, "Disconnecting from Cellular Network");
nbAccess.shutdown();
}
case NetworkConnectionState::DISCONNECTED: {
if (netConnectionState == NetworkConnectionState::CONNECTED) {
execNetworkEventCallback(_on_disconnect_event_callback, 0);
Debug.print(DBG_ERROR, "Disconnected from Cellular Network");
Debug.print(DBG_ERROR, "Attempting reconnection");
if (keepAlive) {
Debug.print(DBG_ERROR, "Attempting reconnection");
}
}
newInterval = CHECK_INTERVAL_DISCONNECTED;
}
break;
case NetworkConnectionState::ERROR: {
execNetworkEventCallback(_on_error_event_callback, 0);
Debug.print(DBG_ERROR, "GPRS attach failed\n\rMake sure the antenna is connected and reset your board.");
}
break;
}
connectionTickTimeInterval = newInterval;
lastConnectionTickTime = millis();
netConnectionState = _newState;
}


void NBConnectionHandler::connect() {
if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) {
return;
}
keepAlive = true;
changeConnectionState(NetworkConnectionState::INIT);

}
void NBConnectionHandler::disconnect() {
//WiFi.end();

changeConnectionState(NetworkConnectionState::DISCONNECTING);
keepAlive = false;
}

#endif /* #ifdef BOARD_HAS_NB */
Loading