Skip to content

Check internet availability #137

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
139 changes: 139 additions & 0 deletions examples/GenericConnectionHandlerDemo/GenericConnectionHandlerDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* SECRET_ fields are in `arduino_secrets.h` (included below)
*
* If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
* Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the
* arduino_secrets.h file (or Secrets tab in Create Web Editor).
*
* WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
*
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
* need a GSMConnectionHandler object as follows
*
* GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
*
* If using a MKR NB1500 you'll need a NBConnectionHandler object as follows
*
* NBConnectionHandler conMan(SECRET_PIN);
*
* If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows:
*
* DHCP mode
* EthernetConnectionHandler conMan;
*
* Manual configuration
* EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
*
* Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE.
*
*/

#include <GenericConnectionHandler.h>

#include "arduino_secrets.h"

#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))
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
#endif

GenericConnectionHandler conMan;


bool attemptConnect = false;
uint32_t lastConnToggleMs = 0;

void setup() {
/* Initialize serial debug port and wait up to 5 seconds for port to open */
Serial.begin(9600);
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }

#ifndef __AVR__
/* Set the debug message level:
* - DBG_ERROR: Only show error messages
* - DBG_WARNING: Show warning and error messages
* - DBG_INFO: Show info, warning, and error messages
* - DBG_DEBUG: Show debug, info, warning, and error messages
* - DBG_VERBOSE: Show all messages
*/
setDebugMessageLevel(DBG_INFO);
#endif

models::NetworkSetting setting = models::settingsDefault(NetworkAdapter::WIFI);

strcpy(setting.wifi.ssid, SECRET_WIFI_SSID);
strcpy(setting.wifi.pwd, SECRET_WIFI_PASS);

/* Add callbacks to the ConnectionHandler object to get notified of network
* connection events. */
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);

conMan.updateSetting(setting);
Serial.print("Network Adapter Interface: ");
switch (conMan.getInterface()) {
case NetworkAdapter::WIFI:
Serial.println("Wi-Fi");
break;
case NetworkAdapter::ETHERNET:
Serial.println("Ethernet");
break;
case NetworkAdapter::NB:
Serial.println("Narrowband");
break;
case NetworkAdapter::GSM:
Serial.println("GSM");
break;
case NetworkAdapter::LORA:
Serial.println("LoRa");
break;
case NetworkAdapter::CATM1:
Serial.println("Category M1");
break;
case NetworkAdapter::CELL:
Serial.println("Cellular");
break;
default:
Serial.println("Unknown");
break;
}
}

void loop() {
/* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) {
Serial.println("Toggling connection...");
if (attemptConnect) {
conMan.connect();
} else {
conMan.disconnect();
}
attemptConnect = !attemptConnect;
lastConnToggleMs = millis();
}

/* The following code keeps on running connection workflows on our
* ConnectionHandler object, hence allowing reconnection in case of failure
* and notification of connect/disconnect event if enabled (see
* addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within
* the loop or methods called from it will delay the execution of .update(),
* which might not guarantee the correct functioning of the ConnectionHandler
* object.
*/
conMan.check();
}

void onNetworkConnect() {
Serial.println(">>>> CONNECTED to network");
}

void onNetworkDisconnect() {
Serial.println(">>>> DISCONNECTED from network");
}

void onNetworkError() {
Serial.println(">>>> ERROR");
}
19 changes: 19 additions & 0 deletions examples/GenericConnectionHandlerDemo/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Required for WiFiConnectionHandler
const char SECRET_WIFI_SSID[] = "SSID";
const char SECRET_WIFI_PASS[] = "PASSWORD";

// Required for GSMConnectionHandler
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler
const char SECRET_GSM_USER[] = "GSM USERNAME";
const char SECRET_GSM_PASS[] = "GSM PASSWORD";

// Required for LoRaConnectionHandler
const char SECRET_APP_EUI[] = "APP_EUI";
const char SECRET_APP_KEY[] = "APP_KEY";

// Required for EthernetConnectionHandler (without DHCP mode)
const char SECRET_IP[] = "IP ADDRESS";
const char SECRET_DNS[] = "DNS ADDRESS";
const char SECRET_GATEWAY[] = "GATEWAY ADDRESS";
const char SECRET_NETMASK[] = "NETWORK MASK";
58 changes: 45 additions & 13 deletions src/CatM1ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@
CTOR/DTOR
******************************************************************************/

CatM1ConnectionHandler::CatM1ConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, RadioAccessTechnologyType rat, uint32_t band, bool const keep_alive)
CatM1ConnectionHandler::CatM1ConnectionHandler()
: ConnectionHandler(true, NetworkAdapter::CATM1) { }

CatM1ConnectionHandler::CatM1ConnectionHandler(
const char * pin, const char * apn, const char * login, const char * pass,
RadioAccessTechnologyType rat, uint32_t band, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::CATM1}
, _pin(pin)
, _apn(apn)
, _login(login)
, _pass(pass)
, _rat(rat)
, _band(band)
{

_settings.type = NetworkAdapter::CATM1;
strncpy(_settings.catm1.pin, pin, sizeof(_settings.catm1.pin)-1);
strncpy(_settings.catm1.apn, apn, sizeof(_settings.catm1.apn)-1);
strncpy(_settings.catm1.login, login, sizeof(_settings.catm1.login)-1);
strncpy(_settings.catm1.pass, pass, sizeof(_settings.catm1.pass)-1);
_settings.catm1.rat = static_cast<uint8_t>(rat);
_settings.catm1.band = band;
}

/******************************************************************************
Expand All @@ -59,18 +64,45 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
pinMode(ON_MKR2, OUTPUT);
digitalWrite(ON_MKR2, HIGH);
#endif

if(!GSM.begin(
_settings.catm1.pin,
_settings.catm1.apn,
_settings.catm1.login,
_settings.catm1.pass,
static_cast<RadioAccessTechnologyType>(_settings.catm1.rat) ,
_settings.catm1.band))
{
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
}
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
{
if(!GSM.begin(_pin, _apn, _login, _pass, _rat, _band))
if (!GSM.isConnected())
{
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
return NetworkConnectionState::INIT;
}

if(!_check_internet_availability){
return NetworkConnectionState::CONNECTED;
}

int ping_result = GSM.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("GSM.ping(): %d"), ping_result);
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("Internet check failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
return NetworkConnectionState::CONNECTING;
}
else
{
Debug.print(DBG_INFO, F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;
}
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTED;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()
Expand Down
9 changes: 1 addition & 8 deletions src/CatM1ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CatM1ConnectionHandler : public ConnectionHandler
{
public:

CatM1ConnectionHandler();
CatM1ConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, RadioAccessTechnologyType rat = CATM1, uint32_t band = BAND_3 | BAND_20 | BAND_19, bool const keep_alive = true);


Expand All @@ -59,14 +60,6 @@ class CatM1ConnectionHandler : public ConnectionHandler

private:

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

RadioAccessTechnologyType _rat;
uint32_t _band;

GSMUDP _gsm_udp;
GSMClient _gsm_client;
};
Expand Down
36 changes: 27 additions & 9 deletions src/CellularConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
/******************************************************************************
CTOR/DTOR
******************************************************************************/
CellularConnectionHandler::CellularConnectionHandler()
: ConnectionHandler(true, NetworkAdapter::CELL) {}

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)
{
_settings.type = NetworkAdapter::CELL;
strncpy(_settings.cell.pin, pin, sizeof(_settings.cell.pin)-1);
strncpy(_settings.cell.apn, apn, sizeof(_settings.cell.apn)-1);
strncpy(_settings.cell.login, login, sizeof(_settings.cell.login)-1);
strncpy(_settings.cell.pass, pass, sizeof(_settings.cell.pass)-1);

}

Expand All @@ -55,20 +58,35 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit()
{
_cellular.begin();
_cellular.setDebugStream(Serial);
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
if (strlen(_settings.cell.pin) > 0 && !_cellular.unlockSIM(_settings.cell.pin)) {
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}

if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.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::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;
if (!_cellular.isConnectedToInternet()) {
return NetworkConnectionState::INIT;
}
Debug.print(DBG_INFO, F("Connected to Network"));

if (!_check_internet_availability) {
return NetworkConnectionState::CONNECTED;
}

if(getTime() == 0){
Debug.print(DBG_ERROR, F("Internet check failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
return NetworkConnectionState::CONNECTING;
}

return NetworkConnectionState::CONNECTED;
}

Expand Down
7 changes: 1 addition & 6 deletions src/CellularConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class CellularConnectionHandler : public ConnectionHandler
{
public:

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


Expand All @@ -53,11 +53,6 @@ class CellularConnectionHandler : public ConnectionHandler

private:

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

ArduinoCellular _cellular;
TinyGsmClient _gsm_client = _cellular.getNetworkClient();
};
Expand Down
7 changes: 4 additions & 3 deletions src/ConnectionHandlerDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ enum class NetworkConnectionEvent {
};

enum class NetworkAdapter {
NONE,
WIFI,
ETHERNET,
NB,
Expand All @@ -190,12 +191,12 @@ enum class NetworkAdapter {

static unsigned int const CHECK_INTERVAL_TABLE[] =
{
/* INIT */ 100,
#if defined(BOARD_HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
/* CONNECTING */ 4000,
/* INIT */ 4000,
#else
/* CONNECTING */ 500,
/* INIT */ 500,
#endif
/* CONNECTING */ 500,
/* CONNECTED */ 10000,
/* DISCONNECTING */ 100,
/* DISCONNECTED */ 1000,
Expand Down
Loading