From d038082dc686e8e2a5f2200b23f73bc9be45f30b Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Wed, 4 Dec 2019 11:21:03 +0100 Subject: [PATCH 01/18] Created TCP/IP connection handler --- src/Arduino_ConnectionHandler.h | 7 ++-- src/Arduino_GSMConnectionHandler.h | 4 +-- src/Arduino_WiFiConnectionHandler.h | 7 ++-- src/TCPIP_ConnectionHandler.h | 54 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 src/TCPIP_ConnectionHandler.h diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 6db47859..e02c8a08 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -22,8 +22,8 @@ INCLUDES ******************************************************************************/ -#include -#include +//#include +//#include #include @@ -57,9 +57,6 @@ class ConnectionHandler { virtual void init() = 0; virtual void check() = 0; virtual void update() = 0; - virtual unsigned long getTime() = 0; - virtual Client &getClient(); - virtual UDP &getUDP(); virtual NetworkConnectionState getStatus() { return netConnectionState; diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index 260fc0ca..360b7025 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -22,7 +22,7 @@ INCLUDE ******************************************************************************/ -#include "Arduino_ConnectionHandler.h" +#include "TCPIP_ConnectionHandler.h" #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ @@ -30,7 +30,7 @@ CLASS DECLARATION ******************************************************************************/ -class GSMConnectionHandler : public ConnectionHandler { +class GSMConnectionHandler : public TCPIPConnectionHandler { public: GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true); diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index 6b117029..bdbf67d5 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -22,15 +22,16 @@ INCLUDE ******************************************************************************/ -#include "Arduino_ConnectionHandler.h" - #ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ +//extern void connectionStateChanged(NetworkConnectionState _newState); + +#include "TCPIP_ConnectionHandler.h" /****************************************************************************** CLASS DECLARATION ******************************************************************************/ -class WiFiConnectionHandler : public ConnectionHandler { +class WiFiConnectionHandler : public TCPIPConnectionHandler { public: WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive = true); diff --git a/src/TCPIP_ConnectionHandler.h b/src/TCPIP_ConnectionHandler.h new file mode 100644 index 00000000..ed5864c7 --- /dev/null +++ b/src/TCPIP_ConnectionHandler.h @@ -0,0 +1,54 @@ +/* + 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. +*/ + +#ifndef TCPIP_CONNECTION_HANDLER_H_ +#define TCPIP_CONNECTION_HANDLER_H_ + +/****************************************************************************** + INCLUDES + ******************************************************************************/ + +#include +#include + +#include + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class TCPIPConnectionHandler : public ConnectionHandler { + public: + virtual void init() = 0; + virtual void check() = 0; + virtual void update() = 0; + virtual unsigned long getTime() = 0; + virtual Client &getClient() = 0; + virtual UDP &getUDP() = 0; + + virtual void connect() = 0; + virtual void disconnect() = 0; + +}; + +#ifdef BOARD_HAS_WIFI + #include "Arduino_WiFiConnectionHandler.h" +#elif defined(BOARD_HAS_GSM) + #include "Arduino_GSMConnectionHandler.h" +#endif + +#endif /* TCPIP_CONNECTION_HANDLER_H_ */ From 2d3b983de0b8f144e278b6c0e52151b0e40a8238 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Fri, 6 Dec 2019 12:10:45 +0100 Subject: [PATCH 02/18] Created LPWAN connection handler --- src/Arduino_ConnectionHandler.h | 18 +- src/Arduino_GSMConnectionHandler.h | 9 +- src/Arduino_LPWANConnectionHandler.h | 54 +++ src/Arduino_LoRaConnectionHandler.cpp | 248 ++++++++++++++ src/Arduino_LoRaConnectionHandler.h | 87 +++++ ...ler.h => Arduino_TcpIpConnectionHandler.h} | 11 +- src/Arduino_WiFiConnectionHandler.cpp | 309 +++++++++++------- src/Arduino_WiFiConnectionHandler.h | 22 +- 8 files changed, 614 insertions(+), 144 deletions(-) create mode 100644 src/Arduino_LPWANConnectionHandler.h create mode 100644 src/Arduino_LoRaConnectionHandler.cpp create mode 100644 src/Arduino_LoRaConnectionHandler.h rename src/{TCPIP_ConnectionHandler.h => Arduino_TcpIpConnectionHandler.h} (82%) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index e02c8a08..b146ae72 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -118,6 +118,11 @@ class ConnectionHandler { #define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY #endif +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) + #include + #define BOARD_HAS_LORA +#endif + #if defined(ARDUINO_ESP8266_ESP12) \ || defined(ESP8266) \ || defined(ESP8266_ESP01) \ @@ -160,12 +165,13 @@ class ConnectionHandler { #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED #endif -#ifdef BOARD_HAS_WIFI - #include "Arduino_WiFiConnectionHandler.h" -#elif defined(BOARD_HAS_GSM) - #include "Arduino_GSMConnectionHandler.h" -#elif defined(BOARD_HAS_NB) - #include "Arduino_NBConnectionHandler.h" + +#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) + #include "Arduino_TcpIpConnectionHandler.h" +#endif + +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) + #include "Arduino_LPWANConnectionHandler.h" #endif #endif /* CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index 360b7025..183619c9 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -22,15 +22,20 @@ INCLUDE ******************************************************************************/ -#include "TCPIP_ConnectionHandler.h" + #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ +#include "Arduino_TcpIpConnectionHandler.h" +#ifdef ARDUINO_SAMD_MKRGSM1400 + #include +#endif + /****************************************************************************** CLASS DECLARATION ******************************************************************************/ -class GSMConnectionHandler : public TCPIPConnectionHandler { +class GSMConnectionHandler : public TcpIpConnectionHandler { public: GSMConnectionHandler(const char *pin, const char *apn, const char *login, const char *pass, const bool keepAlive = true); diff --git a/src/Arduino_LPWANConnectionHandler.h b/src/Arduino_LPWANConnectionHandler.h new file mode 100644 index 00000000..0a2a6d0f --- /dev/null +++ b/src/Arduino_LPWANConnectionHandler.h @@ -0,0 +1,54 @@ +/* + 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. +*/ + +#ifndef ARDUINO_LPWAN_CONNECTION_HANDLER_H_ +#define ARDUINO_LPWAN_CONNECTION_HANDLER_H_ + +/****************************************************************************** + INCLUDES + ******************************************************************************/ + +#include +#include + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class LPWANConnectionHandler : public ConnectionHandler { + public: + virtual void init() = 0; + virtual void check() = 0; + virtual void update() = 0; + virtual unsigned long getTime() = 0; + + virtual void write(const uint8_t *buf, size_t size) = 0; + virtual int read() = 0; + virtual bool available() = 0; + + virtual void connect() = 0; + virtual void disconnect() = 0; + +}; + +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) + //#error BOARD_HAS_LORA + #include "Arduino_LoRaConnectionHandler.h" +#endif + + +#endif /* LPWAN_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp new file mode 100644 index 00000000..be689a54 --- /dev/null +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -0,0 +1,248 @@ +/* + 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 + ******************************************************************************/ + +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) /* Only compile if the board has WiFi */ + +#include "Arduino_LoRaConnectionHandler.h" + +/****************************************************************************** + CONSTANTS + ******************************************************************************/ + +static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ + +/****************************************************************************** + CTOR/DTOR + ******************************************************************************/ + +LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey) : + appeui(_appeui), + appkey(_appkey), + lastConnectionTickTime(millis()), + connectionTickTimeInterval(CHECK_INTERVAL_IDLE), + keepAlive(false), + _on_connect_event_callback(NULL), + _on_disconnect_event_callback(NULL), + _on_error_event_callback(NULL) { +} + +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +void LoRaConnectionHandler::init() { +} + +// INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED, CLOSED, ERROR +void LoRaConnectionHandler::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 LoRaConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { + _on_connect_event_callback = callback; +} +void LoRaConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { + _on_disconnect_event_callback = callback; +} +void LoRaConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { + _on_error_event_callback = callback; +} + +void LoRaConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { + if (callback) { + (*callback)(callback_arg); + } +} + +unsigned long LoRaConnectionHandler::getTime() { + return 0; +} + +void LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { + int err; + modem.beginPacket(); + modem.write(buf, size); + err = modem.endPacket(true); + if (err > 0) { + Serial.println("Message sent correctly!"); + } else { + Serial.println("Error sending message :("); + Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength"); + Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)"); + } +} + +int LoRaConnectionHandler::read() { + return modem.read(); +} + +bool LoRaConnectionHandler::available() { + return modem.available(); +} + +void LoRaConnectionHandler::update() { + + unsigned long const now = millis(); + int networkStatus = 0; + if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ + + lastConnectionTickTime = now; + + switch (netConnectionState) { + case NetworkConnectionState::INIT: { + Debug.print(DBG_VERBOSE, "::INIT"); + + delay(1000); + + changeConnectionState(NetworkConnectionState::CONNECTING); + } + break; + case NetworkConnectionState::CONNECTING: { + Debug.print(DBG_VERBOSE, "::CONNECTING"); + networkStatus = modem.joinOTAA(appeui, appkey);; + + if (networkStatus != true) { + changeConnectionState(NetworkConnectionState::ERROR); + return; + } + + Debug.print(DBG_INFO, "Connected to the network"); + changeConnectionState(NetworkConnectionState::CONNECTED); + return; + } + break; + case NetworkConnectionState::CONNECTED: { + + networkStatus = modem.connected(); + Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus); + if (networkStatus != true) { + changeConnectionState(NetworkConnectionState::DISCONNECTED); + return; + } + Debug.print(DBG_VERBOSE, "Connected to the network"); + } + break; + case NetworkConnectionState::DISCONNECTING: { + changeConnectionState(NetworkConnectionState::DISCONNECTED); + } + break; + case NetworkConnectionState::DISCONNECTED: { + if (keepAlive) { + changeConnectionState(NetworkConnectionState::INIT); + } else { + changeConnectionState(NetworkConnectionState::CLOSED); + } + } + break; + case NetworkConnectionState::ERROR: { + + } + break; + case NetworkConnectionState::CLOSED: { + + } + break; + } + } /* time bracket */ +} + +/****************************************************************************** + PRIVATE MEMBER FUNCTIONS + ******************************************************************************/ + +void LoRaConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { + if (_newState == netConnectionState) { + return; + } + int newInterval = CHECK_INTERVAL_INIT; + switch (_newState) { + case NetworkConnectionState::INIT: { + Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); + newInterval = CHECK_INTERVAL_INIT; + } + break; + case NetworkConnectionState::CONNECTING: { + Debug.print(DBG_INFO, "Connecting to the 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 the network"); + } + break; + case NetworkConnectionState::DISCONNECTED: { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + //Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + + Debug.print(DBG_ERROR, "Connection to the network lost."); + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); + } + + newInterval = CHECK_INTERVAL_DISCONNECTED; + } + break; + case NetworkConnectionState::CLOSED: { + + Debug.print(DBG_VERBOSE, "Connection to the network terminated"); + } + break; + case NetworkConnectionState::ERROR: { + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); + } + break; + } + connectionTickTimeInterval = newInterval; + lastConnectionTickTime = millis(); + netConnectionState = _newState; + //connectionStateChanged(netConnectionState); +} + +void LoRaConnectionHandler::connect() { + if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { + return; + } + keepAlive = true; + changeConnectionState(NetworkConnectionState::INIT); + +} +void LoRaConnectionHandler::disconnect() { + // do nothing + return; +} +#endif diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h new file mode 100644 index 00000000..192cfc04 --- /dev/null +++ b/src/Arduino_LoRaConnectionHandler.h @@ -0,0 +1,87 @@ +/* + 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. +*/ + +#ifndef ARDUINO_LORA_CONNECTION_HANDLER_H_ +#define ARDUINO_LORA_CONNECTION_HANDLER_H_ + +//#ifdef defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "Arduino_LPWANConnectionHandler.h" + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class LoRaConnectionHandler : public LPWANConnectionHandler { + public: + LoRaConnectionHandler(const char *_appeui, const char *_appkey); + + void init(); + unsigned long getTime(); + void check() { + update(); + } + void update(); + + void write(const uint8_t *buf, size_t size); + int read(); + bool available(); + + void disconnect(); + void connect(); + + virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); + virtual void addConnectCallback(OnNetworkEventCallback callback); + virtual void addDisconnectCallback(OnNetworkEventCallback callback); + virtual void addErrorCallback(OnNetworkEventCallback callback); + + private: + + void changeConnectionState(NetworkConnectionState _newState); + + const int CHECK_INTERVAL_IDLE = 100; + const int CHECK_INTERVAL_INIT = 100; + const int CHECK_INTERVAL_CONNECTING = 500; + const int CHECK_INTERVAL_CONNECTED = 10000; + const int CHECK_INTERVAL_RETRYING = 5000; + const int CHECK_INTERVAL_DISCONNECTING = 500; + const int CHECK_INTERVAL_DISCONNECTED = 1000; + const int CHECK_INTERVAL_ERROR = 500; + + LoRaModem modem; + const char *appeui, *appkey; + unsigned long lastConnectionTickTime; + + int connectionTickTimeInterval; + + bool keepAlive; + + OnNetworkEventCallback _on_connect_event_callback, + _on_disconnect_event_callback, + _on_error_event_callback; + + static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); + +}; + +//#endif + +#endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */ diff --git a/src/TCPIP_ConnectionHandler.h b/src/Arduino_TcpIpConnectionHandler.h similarity index 82% rename from src/TCPIP_ConnectionHandler.h rename to src/Arduino_TcpIpConnectionHandler.h index ed5864c7..33931ec3 100644 --- a/src/TCPIP_ConnectionHandler.h +++ b/src/Arduino_TcpIpConnectionHandler.h @@ -15,8 +15,8 @@ a commercial license, send an email to license@arduino.cc. */ -#ifndef TCPIP_CONNECTION_HANDLER_H_ -#define TCPIP_CONNECTION_HANDLER_H_ +#ifndef ARDUINO_TCPIP_CONNECTION_HANDLER_H_ +#define ARDUINO_TCPIP_CONNECTION_HANDLER_H_ /****************************************************************************** INCLUDES @@ -26,12 +26,13 @@ #include #include +#include /****************************************************************************** CLASS DECLARATION ******************************************************************************/ -class TCPIPConnectionHandler : public ConnectionHandler { +class TcpIpConnectionHandler : public ConnectionHandler { public: virtual void init() = 0; virtual void check() = 0; @@ -45,9 +46,9 @@ class TCPIPConnectionHandler : public ConnectionHandler { }; -#ifdef BOARD_HAS_WIFI +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT) #include "Arduino_WiFiConnectionHandler.h" -#elif defined(BOARD_HAS_GSM) +#elif defined(ARDUINO_SAMD_MKRGSM1400) #include "Arduino_GSMConnectionHandler.h" #endif diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index 4f60b862..a794cfe8 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -19,9 +19,25 @@ 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; +*/ + +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT) + #include "Arduino_WiFiConnectionHandler.h" -#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ +/****************************************************************************** + CONSTANTS + ******************************************************************************/ + +static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ /****************************************************************************** CTOR/DTOR @@ -85,142 +101,197 @@ unsigned long WiFiConnectionHandler::getTime() { void WiFiConnectionHandler::update() { unsigned long const now = millis(); - if((now - lastConnectionTickTime) > connectionTickTimeInterval) - { + int networkStatus = 0; + if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ + lastConnectionTickTime = now; switch (netConnectionState) { - case NetworkConnectionState::INIT: netConnectionState = update_handleInit (); break; - case NetworkConnectionState::CONNECTING: netConnectionState = update_handleConnecting (); break; - case NetworkConnectionState::CONNECTED: netConnectionState = update_handleConnected (); break; - case NetworkConnectionState::GETTIME: netConnectionState = update_handleGetTime (); break; - case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break; - case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected (); break; - case NetworkConnectionState::ERROR: break; - case NetworkConnectionState::CLOSED: break; + case NetworkConnectionState::INIT: { + Debug.print(DBG_VERBOSE, "::INIT"); + #if !defined(BOARD_ESP8266) + networkStatus = WiFi.status(); + + Debug.print(DBG_INFO, "WiFi.status(): %d", networkStatus); + if (networkStatus == NETWORK_HARDWARE_ERROR) { + // NO FURTHER ACTION WILL FOLLOW THIS + changeConnectionState(NetworkConnectionState::ERROR); + return; + } + Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); + if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { + Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); + Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); + delay(5000); + } + #else + Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); + WiFi.disconnect(); + delay(300); + networkStatus = WiFi.begin(ssid, pass); + delay(1000); + #endif + + changeConnectionState(NetworkConnectionState::CONNECTING); + } + break; + case NetworkConnectionState::CONNECTING: { + Debug.print(DBG_VERBOSE, "::CONNECTING"); + networkStatus = WiFi.status(); + + #if !defined(BOARD_ESP8266) + + if (networkStatus != WL_CONNECTED) { + networkStatus = WiFi.begin(ssid, pass); + } + + #else + + networkStatus = WiFi.status(); + + #endif + + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", networkStatus); + if (networkStatus != NETWORK_CONNECTED) { + Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); + Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); + + return; + } else { + Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); + changeConnectionState(NetworkConnectionState::GETTIME); + return; + } + } + break; + case NetworkConnectionState::CONNECTED: { + + networkStatus = WiFi.status(); + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", networkStatus); + if (networkStatus != WL_CONNECTED) { + changeConnectionState(NetworkConnectionState::DISCONNECTED); + return; + } + Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); + } + break; + case NetworkConnectionState::GETTIME: { + Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); +#if defined(BOARD_ESP8266) + configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); +#endif + changeConnectionState(NetworkConnectionState::CONNECTED); + } + break; + case NetworkConnectionState::DISCONNECTING: { + if (networkStatus != WL_CONNECTED) { + changeConnectionState(NetworkConnectionState::DISCONNECTED); + } + } + break; + case NetworkConnectionState::DISCONNECTED: { + #if !defined(BOARD_ESP8266) + WiFi.end(); + #endif + if (keepAlive) { + changeConnectionState(NetworkConnectionState::INIT); + } else { + changeConnectionState(NetworkConnectionState::CLOSED); + } + + } + break; + case NetworkConnectionState::ERROR: { + + } + break; + case NetworkConnectionState::CLOSED: { + + } + break; } - } + } /* time bracket */ } /****************************************************************************** PRIVATE MEMBER FUNCTIONS ******************************************************************************/ -void WiFiConnectionHandler::connect() { - if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { +void WiFiConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { + if (_newState == netConnectionState) { return; } - keepAlive = true; - connectionTickTimeInterval = CHECK_INTERVAL_INIT; - netConnectionState = NetworkConnectionState::INIT; -} - -void WiFiConnectionHandler::disconnect() { - keepAlive = false; - netConnectionState = NetworkConnectionState::DISCONNECTING; -} - -NetworkConnectionState WiFiConnectionHandler::update_handleInit() { - Debug.print(DBG_VERBOSE, "::INIT"); - -#ifndef BOARD_ESP8266 - Debug.print(DBG_INFO, "WiFi.status(): %d", WiFi.status()); - if (WiFi.status() == NETWORK_HARDWARE_ERROR) { - execNetworkEventCallback(_on_error_event_callback, 0); - Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); - Debug.print(DBG_ERROR, "Then reset and retry."); - return NetworkConnectionState::ERROR; + int newInterval = CHECK_INTERVAL_INIT; + switch (_newState) { + case NetworkConnectionState::INIT: { + Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); + newInterval = CHECK_INTERVAL_INIT; + } + break; + case NetworkConnectionState::CONNECTING: { + Debug.print(DBG_INFO, "Connecting to \"%s\"", ssid); + 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 \"%s\"", ssid); + WiFi.disconnect(); + } + break; + case NetworkConnectionState::DISCONNECTED: { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + + Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); + } + + newInterval = CHECK_INTERVAL_DISCONNECTED; + } + break; + case NetworkConnectionState::CLOSED: { + + #if !defined(BOARD_ESP8266) + WiFi.end(); + #endif + + Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); + } + break; + case NetworkConnectionState::ERROR: { + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); + Debug.print(DBG_ERROR, "Then reset and retry."); + } + break; } - - Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); - - if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { - Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); - Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); - delay(5000); - } -#else - Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); - WiFi.disconnect(); - delay(300); - WiFi.begin(ssid, pass); - delay(1000); -#endif /* ifndef BOARD_ESP8266 */ - - connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; - return NetworkConnectionState::CONNECTING; + connectionTickTimeInterval = newInterval; + lastConnectionTickTime = millis(); + netConnectionState = _newState; + //connectionStateChanged(netConnectionState); } -NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() { - Debug.print(DBG_VERBOSE, "::CONNECTING"); - -#ifndef BOARD_ESP8266 - if (WiFi.status() != WL_CONNECTED) { - WiFi.begin(ssid, pass); - } -#endif /* ifndef BOARD_ESP8266 */ - - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - if (WiFi.status() != NETWORK_CONNECTED) { - Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); - Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); - return NetworkConnectionState::CONNECTING; - } - else { - Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); - execNetworkEventCallback(_on_connect_event_callback, 0); - connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; - return NetworkConnectionState::GETTIME; - } -} - -NetworkConnectionState WiFiConnectionHandler::update_handleConnected() { - - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - if (WiFi.status() != WL_CONNECTED) - { - execNetworkEventCallback(_on_disconnect_event_callback, 0); - - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); - - if (keepAlive) { - Debug.print(DBG_ERROR, "Attempting reconnection"); - } - - connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; - return NetworkConnectionState::DISCONNECTED; +void WiFiConnectionHandler::connect() { + if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { + return; } - Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); - return NetworkConnectionState::CONNECTED; -} - -NetworkConnectionState WiFiConnectionHandler::update_handleGetTime() { - Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); -#ifdef BOARD_ESP8266 - configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); -#endif - return NetworkConnectionState::CONNECTED; -} + keepAlive = true; + changeConnectionState(NetworkConnectionState::INIT); -NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting() { - Debug.print(DBG_VERBOSE, "Disconnecting from \"%s\"", ssid); - WiFi.disconnect(); - return NetworkConnectionState::DISCONNECTED; } +void WiFiConnectionHandler::disconnect() { + //WiFi.end(); -NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected() { -#ifndef BOARD_ESP8266 - WiFi.end(); -#endif /* ifndef BOARD_ESP8266 */ - if (keepAlive) { - connectionTickTimeInterval = CHECK_INTERVAL_INIT; - return NetworkConnectionState::INIT; - } - else { - Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); - return NetworkConnectionState::CLOSED; - } + changeConnectionState(NetworkConnectionState::DISCONNECTING); + keepAlive = false; } - #endif /* #ifdef BOARD_HAS_WIFI */ diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index bdbf67d5..06a42477 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -21,17 +21,17 @@ /****************************************************************************** INCLUDE ******************************************************************************/ - -#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ +//#if defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) +//#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ //extern void connectionStateChanged(NetworkConnectionState _newState); -#include "TCPIP_ConnectionHandler.h" +#include "Arduino_TcpIpConnectionHandler.h" /****************************************************************************** CLASS DECLARATION ******************************************************************************/ -class WiFiConnectionHandler : public TCPIPConnectionHandler { +class WiFiConnectionHandler : public TcpIpConnectionHandler { public: WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive = true); @@ -58,11 +58,16 @@ class WiFiConnectionHandler : public TCPIPConnectionHandler { private: + void changeConnectionState(NetworkConnectionState _newState); + const int CHECK_INTERVAL_IDLE = 100; const int CHECK_INTERVAL_INIT = 100; const int CHECK_INTERVAL_CONNECTING = 500; const int CHECK_INTERVAL_CONNECTED = 10000; + const int CHECK_INTERVAL_RETRYING = 5000; + const int CHECK_INTERVAL_DISCONNECTING = 500; const int CHECK_INTERVAL_DISCONNECTED = 1000; + const int CHECK_INTERVAL_ERROR = 500; const char *ssid, *pass; unsigned long lastConnectionTickTime; @@ -78,15 +83,8 @@ class WiFiConnectionHandler : public TCPIPConnectionHandler { static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); - NetworkConnectionState update_handleInit (); - NetworkConnectionState update_handleConnecting (); - NetworkConnectionState update_handleConnected (); - NetworkConnectionState update_handleGetTime (); - NetworkConnectionState update_handleDisconnecting(); - NetworkConnectionState update_handleDisconnected (); - }; -#endif /* #ifdef BOARD_HAS_WIFI */ +//#endif /* #ifdef BOARD_HAS_WIFI */ #endif /* ARDUINO_WIFI_CONNECTION_HANDLER_H_ */ From a0e66da0f9372267c96c3f72a276a37bf42f792a Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Fri, 6 Dec 2019 17:27:24 +0100 Subject: [PATCH 03/18] Fixed error management and lora_band configuration --- src/Arduino_LoRaConnectionHandler.cpp | 63 ++++++++++++++++++++++----- src/Arduino_LoRaConnectionHandler.h | 3 +- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index be689a54..cdaf8068 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -33,15 +33,17 @@ static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED CTOR/DTOR ******************************************************************************/ -LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey) : +LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band band) : appeui(_appeui), appkey(_appkey), + band(band), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), keepAlive(false), _on_connect_event_callback(NULL), _on_disconnect_event_callback(NULL), _on_error_event_callback(NULL) { + netConnectionState = NetworkConnectionState::INIT; } /****************************************************************************** @@ -89,13 +91,50 @@ void LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { modem.beginPacket(); modem.write(buf, size); err = modem.endPacket(true); - if (err > 0) { - Serial.println("Message sent correctly!"); - } else { - Serial.println("Error sending message :("); - Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength"); - Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)"); - } + if (err != size) { + switch (err) { + case -1: { + Serial.println("Message length is bigger than max LoRa packet!"); + Serial.println(err); + } + break; + case -2: { + Serial.println("Message ack was not recieved, the message could not be delivered"); + } + break; + case 2: { + Serial.println("LoRa generic error (LORA_ERROR)"); + } + break; + case 3: { + Serial.println("LoRa malformed param error (LORA_ERROR_PARAM"); + } + break; + case 4: { + Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)"); + } + break; + case 5: { + Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); + } + break; + case 6: { + Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)"); + } + break; + case 7: { + Serial.println("LoRa rx error (LORA_ERROR_RX)"); + } + break; + case 8: { + Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)"); + } + break; + } + } + else { + Serial.println("Message sent correctly!"); + } } int LoRaConnectionHandler::read() { @@ -117,7 +156,10 @@ void LoRaConnectionHandler::update() { switch (netConnectionState) { case NetworkConnectionState::INIT: { Debug.print(DBG_VERBOSE, "::INIT"); - + if (!modem.begin(band)) { + Debug.print(DBG_VERBOSE, "Failed to start module"); + changeConnectionState(NetworkConnectionState::ERROR); + }; delay(1000); changeConnectionState(NetworkConnectionState::CONNECTING); @@ -125,8 +167,7 @@ void LoRaConnectionHandler::update() { break; case NetworkConnectionState::CONNECTING: { Debug.print(DBG_VERBOSE, "::CONNECTING"); - networkStatus = modem.joinOTAA(appeui, appkey);; - + networkStatus = modem.joinOTAA(appeui, appkey); if (networkStatus != true) { changeConnectionState(NetworkConnectionState::ERROR); return; diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index 192cfc04..75e748af 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -32,7 +32,7 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { public: - LoRaConnectionHandler(const char *_appeui, const char *_appkey); + LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = EU868); void init(); unsigned long getTime(); @@ -68,6 +68,7 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { LoRaModem modem; const char *appeui, *appkey; + _lora_band band; unsigned long lastConnectionTickTime; int connectionTickTimeInterval; From e178c6ceb6913119963f27f2d4e9424dabbfdb03 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Mon, 9 Dec 2019 11:50:51 +0100 Subject: [PATCH 04/18] LPWAN interface updated --- src/Arduino_LPWANConnectionHandler.h | 2 +- src/Arduino_LoRaConnectionHandler.cpp | 62 ++++++++------------------- src/Arduino_LoRaConnectionHandler.h | 2 +- 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/src/Arduino_LPWANConnectionHandler.h b/src/Arduino_LPWANConnectionHandler.h index 0a2a6d0f..366f0054 100644 --- a/src/Arduino_LPWANConnectionHandler.h +++ b/src/Arduino_LPWANConnectionHandler.h @@ -36,7 +36,7 @@ class LPWANConnectionHandler : public ConnectionHandler { virtual void update() = 0; virtual unsigned long getTime() = 0; - virtual void write(const uint8_t *buf, size_t size) = 0; + virtual int write(const uint8_t *buf, size_t size) = 0; virtual int read() = 0; virtual bool available() = 0; diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index cdaf8068..30fc1368 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -86,55 +86,29 @@ unsigned long LoRaConnectionHandler::getTime() { return 0; } -void LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { - int err; - modem.beginPacket(); - modem.write(buf, size); - err = modem.endPacket(true); - if (err != size) { - switch (err) { - case -1: { - Serial.println("Message length is bigger than max LoRa packet!"); - Serial.println(err); - } - break; - case -2: { - Serial.println("Message ack was not recieved, the message could not be delivered"); - } - break; - case 2: { - Serial.println("LoRa generic error (LORA_ERROR)"); - } - break; - case 3: { - Serial.println("LoRa malformed param error (LORA_ERROR_PARAM"); - } - break; - case 4: { - Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)"); - } - break; - case 5: { - Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); - } - break; - case 6: { - Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)"); - } - break; - case 7: { - Serial.println("LoRa rx error (LORA_ERROR_RX)"); - } - break; - case 8: { - Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)"); - } - break; +int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { + int err; + modem.beginPacket(); + modem.write(buf, size); + err = modem.endPacket(true); + /*Error manager according pr #68 of MKRWAN repo*/ + if (err != size) { + switch (err) { + case -20: {Serial.println("Message length is bigger than max LoRa packet!");} break; + case -1: {Serial.println("Message ack was not received, the message could not be delivered");} break; + case -2: {Serial.println("LoRa generic error (LORA_ERROR)");} break; + case -3: {Serial.println("LoRa malformed param error (LORA_ERROR_PARAM");} break; + case -4: {Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)");} break; + case -5: {Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)");} break; + case -6: {Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)");} break; + case -7: {Serial.println("LoRa rx error (LORA_ERROR_RX)");} break; + case -8: {Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)");} break; } } else { Serial.println("Message sent correctly!"); } + return err; } int LoRaConnectionHandler::read() { diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index 75e748af..b95fd840 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -41,7 +41,7 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { } void update(); - void write(const uint8_t *buf, size_t size); + int write(const uint8_t *buf, size_t size); int read(); bool available(); From fec3d64602ef781a9ea5d40da112246dced18979 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Mon, 16 Dec 2019 18:09:50 +0100 Subject: [PATCH 05/18] Refactoring restored in WiFiConnectionHandler --- src/Arduino_WiFiConnectionHandler.cpp | 309 ++++++++++---------------- src/Arduino_WiFiConnectionHandler.h | 19 +- 2 files changed, 129 insertions(+), 199 deletions(-) diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index a794cfe8..4f60b862 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -19,25 +19,9 @@ 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; -*/ - -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT) - #include "Arduino_WiFiConnectionHandler.h" -/****************************************************************************** - CONSTANTS - ******************************************************************************/ - -static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ +#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ /****************************************************************************** CTOR/DTOR @@ -101,197 +85,142 @@ unsigned long WiFiConnectionHandler::getTime() { void WiFiConnectionHandler::update() { unsigned long const now = millis(); - int networkStatus = 0; - if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ - + if((now - lastConnectionTickTime) > connectionTickTimeInterval) + { lastConnectionTickTime = now; switch (netConnectionState) { - case NetworkConnectionState::INIT: { - Debug.print(DBG_VERBOSE, "::INIT"); - #if !defined(BOARD_ESP8266) - networkStatus = WiFi.status(); - - Debug.print(DBG_INFO, "WiFi.status(): %d", networkStatus); - if (networkStatus == NETWORK_HARDWARE_ERROR) { - // NO FURTHER ACTION WILL FOLLOW THIS - changeConnectionState(NetworkConnectionState::ERROR); - return; - } - Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); - if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { - Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); - Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); - delay(5000); - } - #else - Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); - WiFi.disconnect(); - delay(300); - networkStatus = WiFi.begin(ssid, pass); - delay(1000); - #endif - - changeConnectionState(NetworkConnectionState::CONNECTING); - } - break; - case NetworkConnectionState::CONNECTING: { - Debug.print(DBG_VERBOSE, "::CONNECTING"); - networkStatus = WiFi.status(); - - #if !defined(BOARD_ESP8266) - - if (networkStatus != WL_CONNECTED) { - networkStatus = WiFi.begin(ssid, pass); - } - - #else - - networkStatus = WiFi.status(); - - #endif - - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", networkStatus); - if (networkStatus != NETWORK_CONNECTED) { - Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); - Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); - - return; - } else { - Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); - changeConnectionState(NetworkConnectionState::GETTIME); - return; - } - } - break; - case NetworkConnectionState::CONNECTED: { - - networkStatus = WiFi.status(); - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", networkStatus); - if (networkStatus != WL_CONNECTED) { - changeConnectionState(NetworkConnectionState::DISCONNECTED); - return; - } - Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); - } - break; - case NetworkConnectionState::GETTIME: { - Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); -#if defined(BOARD_ESP8266) - configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); -#endif - changeConnectionState(NetworkConnectionState::CONNECTED); - } - break; - case NetworkConnectionState::DISCONNECTING: { - if (networkStatus != WL_CONNECTED) { - changeConnectionState(NetworkConnectionState::DISCONNECTED); - } - } - break; - case NetworkConnectionState::DISCONNECTED: { - #if !defined(BOARD_ESP8266) - WiFi.end(); - #endif - if (keepAlive) { - changeConnectionState(NetworkConnectionState::INIT); - } else { - changeConnectionState(NetworkConnectionState::CLOSED); - } - - } - break; - case NetworkConnectionState::ERROR: { - - } - break; - case NetworkConnectionState::CLOSED: { - - } - break; + case NetworkConnectionState::INIT: netConnectionState = update_handleInit (); break; + case NetworkConnectionState::CONNECTING: netConnectionState = update_handleConnecting (); break; + case NetworkConnectionState::CONNECTED: netConnectionState = update_handleConnected (); break; + case NetworkConnectionState::GETTIME: netConnectionState = update_handleGetTime (); break; + case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break; + case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected (); break; + case NetworkConnectionState::ERROR: break; + case NetworkConnectionState::CLOSED: break; } - } /* time bracket */ + } } /****************************************************************************** PRIVATE MEMBER FUNCTIONS ******************************************************************************/ -void WiFiConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { - if (_newState == netConnectionState) { +void WiFiConnectionHandler::connect() { + if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { return; } - int newInterval = CHECK_INTERVAL_INIT; - switch (_newState) { - case NetworkConnectionState::INIT: { - Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); - newInterval = CHECK_INTERVAL_INIT; - } - break; - case NetworkConnectionState::CONNECTING: { - Debug.print(DBG_INFO, "Connecting to \"%s\"", ssid); - 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 \"%s\"", ssid); - WiFi.disconnect(); - } - break; - case NetworkConnectionState::DISCONNECTED: { - execNetworkEventCallback(_on_disconnect_event_callback, 0); - Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - - Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); - if (keepAlive) { - Debug.print(DBG_ERROR, "Attempting reconnection"); - } - - newInterval = CHECK_INTERVAL_DISCONNECTED; - } - break; - case NetworkConnectionState::CLOSED: { - - #if !defined(BOARD_ESP8266) - WiFi.end(); - #endif - - Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); - } - break; - case NetworkConnectionState::ERROR: { - execNetworkEventCallback(_on_error_event_callback, 0); - Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); - Debug.print(DBG_ERROR, "Then reset and retry."); - } - break; + keepAlive = true; + connectionTickTimeInterval = CHECK_INTERVAL_INIT; + netConnectionState = NetworkConnectionState::INIT; +} + +void WiFiConnectionHandler::disconnect() { + keepAlive = false; + netConnectionState = NetworkConnectionState::DISCONNECTING; +} + +NetworkConnectionState WiFiConnectionHandler::update_handleInit() { + Debug.print(DBG_VERBOSE, "::INIT"); + +#ifndef BOARD_ESP8266 + Debug.print(DBG_INFO, "WiFi.status(): %d", WiFi.status()); + if (WiFi.status() == NETWORK_HARDWARE_ERROR) { + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); + Debug.print(DBG_ERROR, "Then reset and retry."); + return NetworkConnectionState::ERROR; } - connectionTickTimeInterval = newInterval; - lastConnectionTickTime = millis(); - netConnectionState = _newState; - //connectionStateChanged(netConnectionState); + + Debug.print(DBG_ERROR, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); + + if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { + Debug.print(DBG_ERROR, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); + Debug.print(DBG_ERROR, "Please update to the latest version for best performance."); + delay(5000); + } +#else + Debug.print(DBG_ERROR, "WiFi status ESP: %d", WiFi.status()); + WiFi.disconnect(); + delay(300); + WiFi.begin(ssid, pass); + delay(1000); +#endif /* ifndef BOARD_ESP8266 */ + + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; + return NetworkConnectionState::CONNECTING; } -void WiFiConnectionHandler::connect() { - if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) { - return; +NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() { + Debug.print(DBG_VERBOSE, "::CONNECTING"); + +#ifndef BOARD_ESP8266 + if (WiFi.status() != WL_CONNECTED) { + WiFi.begin(ssid, pass); } - keepAlive = true; - changeConnectionState(NetworkConnectionState::INIT); +#endif /* ifndef BOARD_ESP8266 */ + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + if (WiFi.status() != NETWORK_CONNECTED) { + Debug.print(DBG_ERROR, "Connection to \"%s\" failed", ssid); + Debug.print(DBG_INFO, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); + return NetworkConnectionState::CONNECTING; + } + else { + Debug.print(DBG_INFO, "Connected to \"%s\"", ssid); + execNetworkEventCallback(_on_connect_event_callback, 0); + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; + return NetworkConnectionState::GETTIME; + } } -void WiFiConnectionHandler::disconnect() { - //WiFi.end(); - changeConnectionState(NetworkConnectionState::DISCONNECTING); - keepAlive = false; +NetworkConnectionState WiFiConnectionHandler::update_handleConnected() { + + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + if (WiFi.status() != WL_CONNECTED) + { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + + Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + Debug.print(DBG_ERROR, "Connection to \"%s\" lost.", ssid); + + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); + } + + connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; + return NetworkConnectionState::DISCONNECTED; + } + Debug.print(DBG_VERBOSE, "Connected to \"%s\"", ssid); + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState WiFiConnectionHandler::update_handleGetTime() { + Debug.print(DBG_VERBOSE, "NetworkConnectionState::GETTIME"); +#ifdef BOARD_ESP8266 + configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); +#endif + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting() { + Debug.print(DBG_VERBOSE, "Disconnecting from \"%s\"", ssid); + WiFi.disconnect(); + return NetworkConnectionState::DISCONNECTED; } + +NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected() { +#ifndef BOARD_ESP8266 + WiFi.end(); +#endif /* ifndef BOARD_ESP8266 */ + if (keepAlive) { + connectionTickTimeInterval = CHECK_INTERVAL_INIT; + return NetworkConnectionState::INIT; + } + else { + Debug.print(DBG_VERBOSE, "Connection to \"%s\" closed", ssid); + return NetworkConnectionState::CLOSED; + } +} + #endif /* #ifdef BOARD_HAS_WIFI */ diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index 06a42477..00298f45 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -21,12 +21,11 @@ /****************************************************************************** INCLUDE ******************************************************************************/ -//#if defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) -//#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ -//extern void connectionStateChanged(NetworkConnectionState _newState); #include "Arduino_TcpIpConnectionHandler.h" +#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ + /****************************************************************************** CLASS DECLARATION ******************************************************************************/ @@ -58,16 +57,11 @@ class WiFiConnectionHandler : public TcpIpConnectionHandler { private: - void changeConnectionState(NetworkConnectionState _newState); - const int CHECK_INTERVAL_IDLE = 100; const int CHECK_INTERVAL_INIT = 100; const int CHECK_INTERVAL_CONNECTING = 500; const int CHECK_INTERVAL_CONNECTED = 10000; - const int CHECK_INTERVAL_RETRYING = 5000; - const int CHECK_INTERVAL_DISCONNECTING = 500; const int CHECK_INTERVAL_DISCONNECTED = 1000; - const int CHECK_INTERVAL_ERROR = 500; const char *ssid, *pass; unsigned long lastConnectionTickTime; @@ -83,8 +77,15 @@ class WiFiConnectionHandler : public TcpIpConnectionHandler { static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); + NetworkConnectionState update_handleInit (); + NetworkConnectionState update_handleConnecting (); + NetworkConnectionState update_handleConnected (); + NetworkConnectionState update_handleGetTime (); + NetworkConnectionState update_handleDisconnecting(); + NetworkConnectionState update_handleDisconnected (); + }; -//#endif /* #ifdef BOARD_HAS_WIFI */ +#endif /* #ifdef BOARD_HAS_WIFI */ #endif /* ARDUINO_WIFI_CONNECTION_HANDLER_H_ */ From 50c8425f29f25d4b35f12b5caca6692ea26472ea Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Mon, 16 Dec 2019 18:12:16 +0100 Subject: [PATCH 06/18] Removed include of MKRGSM.h in TcpIpConnectionHandler It is alredy included in ConnectionHandler --- src/Arduino_GSMConnectionHandler.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index 183619c9..32d6d853 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -22,15 +22,11 @@ INCLUDE ******************************************************************************/ +#include "Arduino_TcpIpConnectionHandler.h" #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ -#include "Arduino_TcpIpConnectionHandler.h" -#ifdef ARDUINO_SAMD_MKRGSM1400 - #include -#endif - /****************************************************************************** CLASS DECLARATION ******************************************************************************/ From 847a8021f586d9d285f8125b8eef686b8be81651 Mon Sep 17 00:00:00 2001 From: fabio Date: Tue, 17 Dec 2019 09:41:36 +0100 Subject: [PATCH 07/18] refactor Lora handler like wifi handler --- src/Arduino_LoRaConnectionHandler.cpp | 244 ++++++++++++-------------- src/Arduino_LoRaConnectionHandler.h | 9 +- 2 files changed, 121 insertions(+), 132 deletions(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index 30fc1368..56dab6b3 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -43,7 +43,7 @@ LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_a _on_connect_event_callback(NULL), _on_disconnect_event_callback(NULL), _on_error_event_callback(NULL) { - netConnectionState = NetworkConnectionState::INIT; + netConnectionState = NetworkConnectionState::INIT; } /****************************************************************************** @@ -93,22 +93,39 @@ int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { err = modem.endPacket(true); /*Error manager according pr #68 of MKRWAN repo*/ if (err != size) { - switch (err) { - case -20: {Serial.println("Message length is bigger than max LoRa packet!");} break; - case -1: {Serial.println("Message ack was not received, the message could not be delivered");} break; - case -2: {Serial.println("LoRa generic error (LORA_ERROR)");} break; - case -3: {Serial.println("LoRa malformed param error (LORA_ERROR_PARAM");} break; - case -4: {Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)");} break; - case -5: {Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)");} break; - case -6: {Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)");} break; - case -7: {Serial.println("LoRa rx error (LORA_ERROR_RX)");} break; - case -8: {Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)");} break; - } - } - else { - Serial.println("Message sent correctly!"); - } - return err; + switch (err) { + case -20: { + Serial.println("Message length is bigger than max LoRa packet!"); + } break; + case -1: { + Serial.println("Message ack was not received, the message could not be delivered"); + } break; + case -2: { + Serial.println("LoRa generic error (LORA_ERROR)"); + } break; + case -3: { + Serial.println("LoRa malformed param error (LORA_ERROR_PARAM"); + } break; + case -4: { + Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)"); + } break; + case -5: { + Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); + } break; + case -6: { + Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)"); + } break; + case -7: { + Serial.println("LoRa rx error (LORA_ERROR_RX)"); + } break; + case -8: { + Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)"); + } break; + } + } else { + Serial.println("Message sent correctly!"); + } + return err; } int LoRaConnectionHandler::read() { @@ -126,126 +143,92 @@ void LoRaConnectionHandler::update() { if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ lastConnectionTickTime = now; - switch (netConnectionState) { - case NetworkConnectionState::INIT: { - Debug.print(DBG_VERBOSE, "::INIT"); - if (!modem.begin(band)) { - Debug.print(DBG_VERBOSE, "Failed to start module"); - changeConnectionState(NetworkConnectionState::ERROR); - }; - delay(1000); - - changeConnectionState(NetworkConnectionState::CONNECTING); - } - break; - case NetworkConnectionState::CONNECTING: { - Debug.print(DBG_VERBOSE, "::CONNECTING"); - networkStatus = modem.joinOTAA(appeui, appkey); - if (networkStatus != true) { - changeConnectionState(NetworkConnectionState::ERROR); - return; - } - - Debug.print(DBG_INFO, "Connected to the network"); - changeConnectionState(NetworkConnectionState::CONNECTED); - return; - } - break; - case NetworkConnectionState::CONNECTED: { - - networkStatus = modem.connected(); - Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus); - if (networkStatus != true) { - changeConnectionState(NetworkConnectionState::DISCONNECTED); - return; - } - Debug.print(DBG_VERBOSE, "Connected to the network"); - } - break; - case NetworkConnectionState::DISCONNECTING: { - changeConnectionState(NetworkConnectionState::DISCONNECTED); - } - break; - case NetworkConnectionState::DISCONNECTED: { - if (keepAlive) { - changeConnectionState(NetworkConnectionState::INIT); - } else { - changeConnectionState(NetworkConnectionState::CLOSED); - } - } - break; - case NetworkConnectionState::ERROR: { - - } - break; - case NetworkConnectionState::CLOSED: { - - } - break; + case NetworkConnectionState::INIT: netConnectionState = update_handleInit(); break; + case NetworkConnectionState::CONNECTING: netConnectionState = update_handleConnecting(); break; + case NetworkConnectionState::CONNECTED: netConnectionState = update_handleConnected(); break; + case NetworkConnectionState::DISCONNECTING: netConnectionState = update_handleDisconnecting(); break; + case NetworkConnectionState::DISCONNECTED: netConnectionState = update_handleDisconnected(); break; + case NetworkConnectionState::ERROR: break; + case NetworkConnectionState::CLOSED: break; } - } /* time bracket */ + } } /****************************************************************************** PRIVATE MEMBER FUNCTIONS ******************************************************************************/ -void LoRaConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { - if (_newState == netConnectionState) { - return; +NetworkConnectionState LoRaConnectionHandler::update_handleInit() { + Debug.print(DBG_VERBOSE, "::INIT"); + if (!modem.begin(band)) { + Debug.print(DBG_VERBOSE, "Failed to start module"); + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); + }; + delay(1000); + Debug.print(DBG_INFO, "Connecting to the network"); + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; + return NetworkConnectionState::CONNECTING; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleConnecting() { + Debug.print(DBG_VERBOSE, "::CONNECTING"); + bool networkStatus = modem.joinOTAA(appeui, appkey); + if (networkStatus != true) { + execNetworkEventCallback(_on_error_event_callback, 0); + Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); + return NetworkConnectionState::ERROR; } - int newInterval = CHECK_INTERVAL_INIT; - switch (_newState) { - case NetworkConnectionState::INIT: { - Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); - newInterval = CHECK_INTERVAL_INIT; - } - break; - case NetworkConnectionState::CONNECTING: { - Debug.print(DBG_INFO, "Connecting to the 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 the network"); - } - break; - case NetworkConnectionState::DISCONNECTED: { - execNetworkEventCallback(_on_disconnect_event_callback, 0); - //Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); - - Debug.print(DBG_ERROR, "Connection to the network lost."); - if (keepAlive) { - Debug.print(DBG_ERROR, "Attempting reconnection"); - } - - newInterval = CHECK_INTERVAL_DISCONNECTED; - } - break; - case NetworkConnectionState::CLOSED: { - - Debug.print(DBG_VERBOSE, "Connection to the network terminated"); - } - break; - case NetworkConnectionState::ERROR: { - execNetworkEventCallback(_on_error_event_callback, 0); - Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); - } - break; + + Debug.print(DBG_INFO, "Connected to the network"); + connectionTickTimeInterval = CHECK_INTERVAL_CONNECTED; + execNetworkEventCallback(_on_connect_event_callback, 0); + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleConnected() { + + bool networkStatus = modem.connected(); + Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus); + if (networkStatus != true) { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + //Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + + Debug.print(DBG_ERROR, "Connection to the network lost."); + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); + } + connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; + return NetworkConnectionState::DISCONNECTED; + } + Debug.print(DBG_VERBOSE, "Connected to the network"); + + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() { + execNetworkEventCallback(_on_disconnect_event_callback, 0); + //Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); + + Debug.print(DBG_ERROR, "Connection to the network lost."); + if (keepAlive) { + Debug.print(DBG_ERROR, "Attempting reconnection"); } - connectionTickTimeInterval = newInterval; - lastConnectionTickTime = millis(); - netConnectionState = _newState; - //connectionStateChanged(netConnectionState); + connectionTickTimeInterval = CHECK_INTERVAL_DISCONNECTED; + return NetworkConnectionState::DISCONNECTED; +} + +NetworkConnectionState LoRaConnectionHandler::update_handleDisconnected() { + if (keepAlive) { + Debug.print(DBG_VERBOSE, "CHANGING STATE TO ::INIT"); + connectionTickTimeInterval = CHECK_INTERVAL_INIT; + return NetworkConnectionState::INIT; + } else { + Debug.print(DBG_VERBOSE, "Connection to the network terminated"); + return NetworkConnectionState::CLOSED; + } + } void LoRaConnectionHandler::connect() { @@ -253,11 +236,12 @@ void LoRaConnectionHandler::connect() { return; } keepAlive = true; - changeConnectionState(NetworkConnectionState::INIT); + connectionTickTimeInterval = CHECK_INTERVAL_INIT; + netConnectionState = NetworkConnectionState::INIT; } void LoRaConnectionHandler::disconnect() { // do nothing - return; + return; } -#endif +#endif diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index b95fd840..3ac35056 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -55,8 +55,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { private: - void changeConnectionState(NetworkConnectionState _newState); - const int CHECK_INTERVAL_IDLE = 100; const int CHECK_INTERVAL_INIT = 100; const int CHECK_INTERVAL_CONNECTING = 500; @@ -80,6 +78,13 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { _on_error_event_callback; static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); + NetworkConnectionState update_handleInit(); + NetworkConnectionState update_handleConnecting(); + NetworkConnectionState update_handleConnected(); + + NetworkConnectionState update_handleDisconnecting(); + NetworkConnectionState update_handleDisconnected(); + }; From 2d50cea82a51af71adfcc13b458e0cc2eb9a5ada Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Tue, 17 Dec 2019 10:54:00 +0100 Subject: [PATCH 08/18] Fixed comment in LoRaConnectionHandler --- src/Arduino_LoRaConnectionHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index 56dab6b3..65f8fab9 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -19,7 +19,7 @@ INCLUDE ******************************************************************************/ -#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) /* Only compile if the board has WiFi */ +#if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) /* Only compile if the board has LoRa */ #include "Arduino_LoRaConnectionHandler.h" From 8ea22ecf42478c98b99e3ce11918e8a2e0fe92c9 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Wed, 18 Dec 2019 11:54:46 +0100 Subject: [PATCH 09/18] Unnecessary code and comments have been removed --- src/Arduino_ConnectionHandler.h | 3 --- src/Arduino_LPWANConnectionHandler.h | 1 - src/Arduino_LoRaConnectionHandler.cpp | 4 ---- 3 files changed, 8 deletions(-) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index b146ae72..9f17aebd 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -22,9 +22,6 @@ INCLUDES ******************************************************************************/ -//#include -//#include - #include /****************************************************************************** diff --git a/src/Arduino_LPWANConnectionHandler.h b/src/Arduino_LPWANConnectionHandler.h index 366f0054..7f16d8d8 100644 --- a/src/Arduino_LPWANConnectionHandler.h +++ b/src/Arduino_LPWANConnectionHandler.h @@ -46,7 +46,6 @@ class LPWANConnectionHandler : public ConnectionHandler { }; #if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) - //#error BOARD_HAS_LORA #include "Arduino_LoRaConnectionHandler.h" #endif diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index 65f8fab9..ac9faa30 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -91,7 +91,6 @@ int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { modem.beginPacket(); modem.write(buf, size); err = modem.endPacket(true); - /*Error manager according pr #68 of MKRWAN repo*/ if (err != size) { switch (err) { case -20: { @@ -166,7 +165,6 @@ NetworkConnectionState LoRaConnectionHandler::update_handleInit() { execNetworkEventCallback(_on_error_event_callback, 0); Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); }; - delay(1000); Debug.print(DBG_INFO, "Connecting to the network"); connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; return NetworkConnectionState::CONNECTING; @@ -193,7 +191,6 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnected() { Debug.print(DBG_VERBOSE, "Connection state: %d", networkStatus); if (networkStatus != true) { execNetworkEventCallback(_on_disconnect_event_callback, 0); - //Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); Debug.print(DBG_ERROR, "Connection to the network lost."); if (keepAlive) { @@ -209,7 +206,6 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnected() { NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() { execNetworkEventCallback(_on_disconnect_event_callback, 0); - //Debug.print(DBG_VERBOSE, "WiFi.status(): %d", WiFi.status()); Debug.print(DBG_ERROR, "Connection to the network lost."); if (keepAlive) { From fb26c34a8b90a98523387fd525e12f87f2d84b71 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Thu, 19 Dec 2019 10:51:40 +0100 Subject: [PATCH 10/18] Common code moved to the ConnectionHandler class --- src/Arduino_ConnectionHandler.cpp | 55 +++++++++++++++++++++++++++ src/Arduino_ConnectionHandler.h | 18 ++++----- src/Arduino_GSMConnectionHandler.cpp | 33 +--------------- src/Arduino_GSMConnectionHandler.h | 10 ----- src/Arduino_LoRaConnectionHandler.cpp | 34 +---------------- src/Arduino_LoRaConnectionHandler.h | 10 ----- src/Arduino_WiFiConnectionHandler.cpp | 34 +---------------- src/Arduino_WiFiConnectionHandler.h | 10 ----- 8 files changed, 67 insertions(+), 137 deletions(-) create mode 100644 src/Arduino_ConnectionHandler.cpp diff --git a/src/Arduino_ConnectionHandler.cpp b/src/Arduino_ConnectionHandler.cpp new file mode 100644 index 00000000..cfc6b707 --- /dev/null +++ b/src/Arduino_ConnectionHandler.cpp @@ -0,0 +1,55 @@ +/* + 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 + ******************************************************************************/ + +#include "Arduino_ConnectionHandler.h" + +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +void ConnectionHandler::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 ConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { + _on_connect_event_callback = callback; +} +void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { + _on_disconnect_event_callback = callback; +} +void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { + _on_error_event_callback = callback; +} + +void ConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { + if (callback) { + (*callback)(callback_arg); + } +} + diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 9f17aebd..352c495a 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -60,21 +60,21 @@ class ConnectionHandler { } virtual void connect(); virtual void disconnect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - - private: - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; + void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); + void addConnectCallback(OnNetworkEventCallback callback); + void addDisconnectCallback(OnNetworkEventCallback callback); + void addErrorCallback(OnNetworkEventCallback callback); protected: + OnNetworkEventCallback _on_connect_event_callback = NULL, + _on_disconnect_event_callback = NULL, + _on_error_event_callback = NULL; unsigned long lastValidTimestamp = 0; /* UNUSED */ NetworkConnectionState netConnectionState = NetworkConnectionState::DISCONNECTED; + static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); + }; #ifdef ARDUINO_SAMD_MKR1000 diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index 95c4403e..acadcf0e 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -49,10 +49,7 @@ GSMConnectionHandler::GSMConnectionHandler(const char *pin, const char *apn, con pass(pass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(_keepAlive) { } /****************************************************************************** @@ -71,34 +68,6 @@ void GSMConnectionHandler::init() { } } -void GSMConnectionHandler::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 GSMConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { - _on_connect_event_callback = callback; -} -void GSMConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { - _on_disconnect_event_callback = callback; -} -void GSMConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { - _on_error_event_callback = callback; -} - -void GSMConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { - if (callback) { - (*callback)(callback_arg); - } -} - unsigned long GSMConnectionHandler::getTime() { return gsmAccess.getTime(); } diff --git a/src/Arduino_GSMConnectionHandler.h b/src/Arduino_GSMConnectionHandler.h index 32d6d853..e8bbf240 100644 --- a/src/Arduino_GSMConnectionHandler.h +++ b/src/Arduino_GSMConnectionHandler.h @@ -56,11 +56,6 @@ class GSMConnectionHandler : public TcpIpConnectionHandler { virtual void disconnect(); virtual void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - private: void changeConnectionState(NetworkConnectionState _newState); @@ -80,11 +75,6 @@ class GSMConnectionHandler : public TcpIpConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); }; #endif /* #ifdef BOARD_HAS_GSM */ diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index ac9faa30..900822b7 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -39,10 +39,7 @@ LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_a band(band), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(false), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(false) { netConnectionState = NetworkConnectionState::INIT; } @@ -53,35 +50,6 @@ LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_a void LoRaConnectionHandler::init() { } -// INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED, CLOSED, ERROR -void LoRaConnectionHandler::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 LoRaConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { - _on_connect_event_callback = callback; -} -void LoRaConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { - _on_disconnect_event_callback = callback; -} -void LoRaConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { - _on_error_event_callback = callback; -} - -void LoRaConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { - if (callback) { - (*callback)(callback_arg); - } -} - unsigned long LoRaConnectionHandler::getTime() { return 0; } diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index 3ac35056..58906d3d 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -48,11 +48,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { void disconnect(); void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - private: const int CHECK_INTERVAL_IDLE = 100; @@ -73,11 +68,6 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); NetworkConnectionState update_handleInit(); NetworkConnectionState update_handleConnecting(); NetworkConnectionState update_handleConnected(); diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index 4f60b862..f7b948c4 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -32,10 +32,7 @@ WiFiConnectionHandler::WiFiConnectionHandler(const char *_ssid, const char *_pas pass(_pass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(_keepAlive) { } /****************************************************************************** @@ -45,35 +42,6 @@ WiFiConnectionHandler::WiFiConnectionHandler(const char *_ssid, const char *_pas void WiFiConnectionHandler::init() { } -// INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED, CLOSED, ERROR -void WiFiConnectionHandler::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 WiFiConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { - _on_connect_event_callback = callback; -} -void WiFiConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { - _on_disconnect_event_callback = callback; -} -void WiFiConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { - _on_error_event_callback = callback; -} - -void WiFiConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg) { - if (callback) { - (*callback)(callback_arg); - } -} - unsigned long WiFiConnectionHandler::getTime() { #if !defined(BOARD_ESP8266) return WiFi.getTime(); diff --git a/src/Arduino_WiFiConnectionHandler.h b/src/Arduino_WiFiConnectionHandler.h index 00298f45..37d9ee38 100644 --- a/src/Arduino_WiFiConnectionHandler.h +++ b/src/Arduino_WiFiConnectionHandler.h @@ -49,10 +49,6 @@ class WiFiConnectionHandler : public TcpIpConnectionHandler { virtual void disconnect(); virtual void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); WiFiUDP udp; private: @@ -71,12 +67,6 @@ class WiFiConnectionHandler : public TcpIpConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); - NetworkConnectionState update_handleInit (); NetworkConnectionState update_handleConnecting (); NetworkConnectionState update_handleConnected (); From 777537be3734672920caf03f2ab8f2085f263c93 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Thu, 19 Dec 2019 10:55:58 +0100 Subject: [PATCH 11/18] Error management refactoring --- src/Arduino_LoRaConnectionHandler.cpp | 22 +++++++++++----------- src/Arduino_LoRaConnectionHandler.h | 12 ++++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index 900822b7..65cf1d80 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -61,33 +61,33 @@ int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { err = modem.endPacket(true); if (err != size) { switch (err) { - case -20: { - Serial.println("Message length is bigger than max LoRa packet!"); - } break; - case -1: { + case LoRaCommunicationError::LORA_ERROR_ACK_NOT_RECEIVED: { Serial.println("Message ack was not received, the message could not be delivered"); } break; - case -2: { + case LoRaCommunicationError::LORA_ERROR_GENERIC: { Serial.println("LoRa generic error (LORA_ERROR)"); } break; - case -3: { + case LoRaCommunicationError::LORA_ERROR_WRONG_PARAM: { Serial.println("LoRa malformed param error (LORA_ERROR_PARAM"); } break; - case -4: { + case LoRaCommunicationError::LORA_ERROR_COMMUNICATION_BUSY: { Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)"); } break; - case -5: { + case LoRaCommunicationError::LORA_ERROR_MESSAGE_OVERFLOW: { Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); } break; - case -6: { + case LoRaCommunicationError::LORA_ERROR_NO_NETWORK_AVAILABLE: { Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)"); } break; - case -7: { + case LoRaCommunicationError::LORA_ERROR_RX_PACKET: { Serial.println("LoRa rx error (LORA_ERROR_RX)"); } break; - case -8: { + case LoRaCommunicationError::LORA_ERROR_REASON_UNKNOWN: { Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)"); } break; + case LoRaCommunicationError::LORA_ERROR_MAX_PACKET_SIZE: { + Serial.println("Message length is bigger than max LoRa packet!"); + } break; } } else { Serial.println("Message sent correctly!"); diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index 58906d3d..3f16bb1c 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -26,6 +26,18 @@ #include "Arduino_LPWANConnectionHandler.h" +typedef enum { + LORA_ERROR_ACK_NOT_RECEIVED = -1, + LORA_ERROR_GENERIC = -2, + LORA_ERROR_WRONG_PARAM = -3, + LORA_ERROR_COMMUNICATION_BUSY = -4, + LORA_ERROR_MESSAGE_OVERFLOW = -5, + LORA_ERROR_NO_NETWORK_AVAILABLE = -6, + LORA_ERROR_RX_PACKET = -7, + LORA_ERROR_REASON_UNKNOWN = -8, + LORA_ERROR_MAX_PACKET_SIZE = -20 +} LoRaCommunicationError; + /****************************************************************************** CLASS DECLARATION ******************************************************************************/ From d8cb973278d47095f51cbcf77394074c353d5775 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Thu, 19 Dec 2019 10:57:25 +0100 Subject: [PATCH 12/18] Introduced a delay after modem.begin() in the LoRaConnectionHandler the delay let the chip to be correctly initialized before the connection attempt --- src/Arduino_LoRaConnectionHandler.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index 65cf1d80..ea7a1464 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -133,6 +133,8 @@ NetworkConnectionState LoRaConnectionHandler::update_handleInit() { execNetworkEventCallback(_on_error_event_callback, 0); Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); }; + //A delay is required between modem.begin(band) and modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized befor the connection attempt + delay(100); Debug.print(DBG_INFO, "Connecting to the network"); connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; return NetworkConnectionState::CONNECTING; From e6f2753bb9c48612de4be5a3028a35c93df43964 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Thu, 19 Dec 2019 12:21:42 +0100 Subject: [PATCH 13/18] Removing commented useless code --- src/Arduino_LoRaConnectionHandler.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index 3f16bb1c..db1a8b16 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -18,8 +18,6 @@ #ifndef ARDUINO_LORA_CONNECTION_HANDLER_H_ #define ARDUINO_LORA_CONNECTION_HANDLER_H_ -//#ifdef defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) - /****************************************************************************** INCLUDE ******************************************************************************/ @@ -90,6 +88,4 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { }; -//#endif - #endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */ From a1dee1cc44c5f2c810c815f45f0118fd9929a4d1 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Fri, 20 Dec 2019 14:06:09 +0100 Subject: [PATCH 14/18] Managed NBConnectionHandler in the TcpIpConnectionHandlers --- src/Arduino_NBConnectionHandler.cpp | 33 +--------------------------- src/Arduino_NBConnectionHandler.h | 12 +--------- src/Arduino_TcpIpConnectionHandler.h | 2 ++ 3 files changed, 4 insertions(+), 43 deletions(-) diff --git a/src/Arduino_NBConnectionHandler.cpp b/src/Arduino_NBConnectionHandler.cpp index e24045a4..2c46be46 100644 --- a/src/Arduino_NBConnectionHandler.cpp +++ b/src/Arduino_NBConnectionHandler.cpp @@ -56,10 +56,7 @@ NBConnectionHandler::NBConnectionHandler(const char *pin, const char *apn, const pass(pass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), - keepAlive(_keepAlive), - _on_connect_event_callback(NULL), - _on_disconnect_event_callback(NULL), - _on_error_event_callback(NULL) { + keepAlive(_keepAlive) { } /****************************************************************************** @@ -78,34 +75,6 @@ void NBConnectionHandler::init() { } } -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(); } diff --git a/src/Arduino_NBConnectionHandler.h b/src/Arduino_NBConnectionHandler.h index 02a733d3..ab3822dc 100644 --- a/src/Arduino_NBConnectionHandler.h +++ b/src/Arduino_NBConnectionHandler.h @@ -30,7 +30,7 @@ CLASS DECLARATION ******************************************************************************/ -class NBConnectionHandler : public ConnectionHandler { +class NBConnectionHandler : public TcpIpConnectionHandler { public: NBConnectionHandler(const char *pin, const bool keepAlive = true); NBConnectionHandler(const char *pin, const char *apn, const bool keepAlive = true); @@ -57,11 +57,6 @@ class NBConnectionHandler : public ConnectionHandler { virtual void disconnect(); virtual void connect(); - virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); - virtual void addConnectCallback(OnNetworkEventCallback callback); - virtual void addDisconnectCallback(OnNetworkEventCallback callback); - virtual void addErrorCallback(OnNetworkEventCallback callback); - private: void changeConnectionState(NetworkConnectionState _newState); @@ -81,11 +76,6 @@ class NBConnectionHandler : public ConnectionHandler { bool keepAlive; - OnNetworkEventCallback _on_connect_event_callback, - _on_disconnect_event_callback, - _on_error_event_callback; - - static void execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg); }; #endif /* #ifdef BOARD_HAS_NB */ diff --git a/src/Arduino_TcpIpConnectionHandler.h b/src/Arduino_TcpIpConnectionHandler.h index 33931ec3..018f46f9 100644 --- a/src/Arduino_TcpIpConnectionHandler.h +++ b/src/Arduino_TcpIpConnectionHandler.h @@ -50,6 +50,8 @@ class TcpIpConnectionHandler : public ConnectionHandler { #include "Arduino_WiFiConnectionHandler.h" #elif defined(ARDUINO_SAMD_MKRGSM1400) #include "Arduino_GSMConnectionHandler.h" +#elif defined(BOARD_HAS_NB) + #include "Arduino_NBConnectionHandler.h" #endif #endif /* TCPIP_CONNECTION_HANDLER_H_ */ From 4fa5e383d7fd0740bbfe80e352e5554867ef097f Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Wed, 8 Jan 2020 15:19:56 +0100 Subject: [PATCH 15/18] Added possibility to specify the LoRa class in the constructor of LoRaConnectionHandler --- src/Arduino_LoRaConnectionHandler.cpp | 5 ++++- src/Arduino_LoRaConnectionHandler.h | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index ea7a1464..c3ca2cf1 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -33,10 +33,11 @@ static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED CTOR/DTOR ******************************************************************************/ -LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band band) : +LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band band, _lora_class deviceClass) : appeui(_appeui), appkey(_appkey), band(band), + deviceClass(deviceClass), lastConnectionTickTime(millis()), connectionTickTimeInterval(CHECK_INTERVAL_IDLE), keepAlive(false) { @@ -135,6 +136,8 @@ NetworkConnectionState LoRaConnectionHandler::update_handleInit() { }; //A delay is required between modem.begin(band) and modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized befor the connection attempt delay(100); + modem.configureClass(deviceClass); + delay(100); Debug.print(DBG_INFO, "Connecting to the network"); connectionTickTimeInterval = CHECK_INTERVAL_CONNECTING; return NetworkConnectionState::CONNECTING; diff --git a/src/Arduino_LoRaConnectionHandler.h b/src/Arduino_LoRaConnectionHandler.h index db1a8b16..ecb31e89 100644 --- a/src/Arduino_LoRaConnectionHandler.h +++ b/src/Arduino_LoRaConnectionHandler.h @@ -42,7 +42,7 @@ typedef enum { class LoRaConnectionHandler : public LPWANConnectionHandler { public: - LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = EU868); + LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = _lora_band::EU868, _lora_class = _lora_class::CLASS_A); void init(); unsigned long getTime(); @@ -72,6 +72,7 @@ class LoRaConnectionHandler : public LPWANConnectionHandler { LoRaModem modem; const char *appeui, *appkey; _lora_band band; + _lora_class deviceClass; unsigned long lastConnectionTickTime; int connectionTickTimeInterval; From aac1039ac985153079ecf45cbfce2f16390d5fe9 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Wed, 8 Jan 2020 15:23:51 +0100 Subject: [PATCH 16/18] Changed serial logs in debug logs --- src/Arduino_LoRaConnectionHandler.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index c3ca2cf1..d465e678 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -63,35 +63,35 @@ int LoRaConnectionHandler::write(const uint8_t *buf, size_t size) { if (err != size) { switch (err) { case LoRaCommunicationError::LORA_ERROR_ACK_NOT_RECEIVED: { - Serial.println("Message ack was not received, the message could not be delivered"); + Debug.print(DBG_ERROR, "Message ack was not received, the message could not be delivered"); } break; case LoRaCommunicationError::LORA_ERROR_GENERIC: { - Serial.println("LoRa generic error (LORA_ERROR)"); + Debug.print(DBG_ERROR, "LoRa generic error (LORA_ERROR)"); } break; case LoRaCommunicationError::LORA_ERROR_WRONG_PARAM: { - Serial.println("LoRa malformed param error (LORA_ERROR_PARAM"); + Debug.print(DBG_ERROR, "LoRa malformed param error (LORA_ERROR_PARAM"); } break; case LoRaCommunicationError::LORA_ERROR_COMMUNICATION_BUSY: { - Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)"); + Debug.print(DBG_ERROR, "LoRa chip is busy (LORA_ERROR_BUSY)"); } break; case LoRaCommunicationError::LORA_ERROR_MESSAGE_OVERFLOW: { - Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); + Debug.print(DBG_ERROR, "LoRa chip overflow error (LORA_ERROR_OVERFLOW)"); } break; case LoRaCommunicationError::LORA_ERROR_NO_NETWORK_AVAILABLE: { - Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)"); + Debug.print(DBG_ERROR, "LoRa no network error (LORA_ERROR_NO_NETWORK)"); } break; case LoRaCommunicationError::LORA_ERROR_RX_PACKET: { - Serial.println("LoRa rx error (LORA_ERROR_RX)"); + Debug.print(DBG_ERROR, "LoRa rx error (LORA_ERROR_RX)"); } break; case LoRaCommunicationError::LORA_ERROR_REASON_UNKNOWN: { - Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)"); + Debug.print(DBG_ERROR, "LoRa unknown error (LORA_ERROR_UNKNOWN)"); } break; case LoRaCommunicationError::LORA_ERROR_MAX_PACKET_SIZE: { - Serial.println("Message length is bigger than max LoRa packet!"); + Debug.print(DBG_ERROR, "Message length is bigger than max LoRa packet!"); } break; } } else { - Serial.println("Message sent correctly!"); + Debug.print(DBG_INFO, "Message sent correctly!"); } return err; } From 717a3c9d1497724d5778774f8296e90ca4ff6e36 Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Mon, 13 Jan 2020 10:14:29 +0100 Subject: [PATCH 17/18] Fixed typo in the comments --- src/Arduino_LoRaConnectionHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arduino_LoRaConnectionHandler.cpp b/src/Arduino_LoRaConnectionHandler.cpp index d465e678..e5dbf579 100644 --- a/src/Arduino_LoRaConnectionHandler.cpp +++ b/src/Arduino_LoRaConnectionHandler.cpp @@ -134,7 +134,7 @@ NetworkConnectionState LoRaConnectionHandler::update_handleInit() { execNetworkEventCallback(_on_error_event_callback, 0); Debug.print(DBG_ERROR, "Something went wrong; are you indoor? Move near a window, then reset and retry."); }; - //A delay is required between modem.begin(band) and modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized befor the connection attempt + //A delay is required between modem.begin(band) and modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized before the connection attempt delay(100); modem.configureClass(deviceClass); delay(100); From 5ff90a50f409b3c6f111f8a6c93d6efc50c404fc Mon Sep 17 00:00:00 2001 From: mirkokurt Date: Wed, 15 Jan 2020 10:02:02 +0100 Subject: [PATCH 18/18] Added MKRWAN in depends --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 333b6417..035be2ad 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Originally part of ArduinoIoTCloud category=Communication url=https://github.com/arduino-libraries/Arduino_ConnectionHandler architectures=samd,esp32,esp8266 -depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB +depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN