From e4318b49fed7adbc0d33e3e486da765be0466186 Mon Sep 17 00:00:00 2001 From: Ram Rohit Gannavarapu Date: Thu, 21 Oct 2021 15:40:08 -0600 Subject: [PATCH 1/2] Added Example and Bug fixes on Ethernet --- .../Ethernet_ConnectionHandlerDemo.ino | 67 +++++++++++++++++++ src/Arduino_ConnectionHandler.h | 2 +- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino diff --git a/examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino b/examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino new file mode 100644 index 00000000..305221fd --- /dev/null +++ b/examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino @@ -0,0 +1,67 @@ +/* SECRET_ fields are in arduino_secrets.h included above + * if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO + * WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding + * Network Name (SECRET_SSID) and password (SECRET_PASS) in the arduino_secrets.h + * file (or Secrets tab in Create Web Editor). + * + * WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS); + * + * If using a MKR GSM 1400 or other GSM boards supporting the same API you'll + * need a GSMConnectionHandler object as follows + * + * GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); + * + * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows + * + * NBConnectionHandler conMan(SECRET_PIN); + * If using an Ethernet based shield you will need a EthernetConnectionHandler object as follows + * + * EthernetConnectionHandler conMan(mac); + * + * this example was tested on a Teensy4.0 board with a Wiznet850 Ethernet Adapter + */ + +#include + +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +EthernetConnectionHandler conMan(mac); + +void setup() { + SPI.begin(); + Ethernet.init(10); // The CS pin on most Arduino Ethernet shields + + Serial.begin(9600); + /* Give a few seconds for the Serial connection to be available */ + delay(4000); + + setDebugMessageLevel(DBG_INFO); + + conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); + conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); + conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); +} + +void loop() { + /* The following code keeps on running connection workflows on our + * ConnectionHandler object, hence allowing reconnection in case of failure + * and notification of connect/disconnect event if enabled (see + * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within + * the loop or methods called from it will delay the execution of .update(), + * which might not guarantee the correct functioning of the ConnectionHandler + * object. + */ + + conMan.check(); +} + +void onNetworkConnect() { + Serial.println(">>>> CONNECTED to network"); +} + +void onNetworkDisconnect() { + Serial.println(">>>> DISCONNECTED from network"); +} + +void onNetworkError() { + Serial.println(">>>> ERROR"); +} \ No newline at end of file diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 348cc94c..59671a00 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -176,7 +176,7 @@ class ConnectionHandler { NetworkConnectionState check(); - #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) + #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) virtual unsigned long getTime() = 0; virtual Client &getClient() = 0; virtual UDP &getUDP() = 0; From 7a2788a579e750c2c0f4057cb133ad5a5d69e41b Mon Sep 17 00:00:00 2001 From: Ram Rohit Gannavarapu Date: Thu, 21 Oct 2021 16:37:58 -0600 Subject: [PATCH 2/2] move example --- .../ConnectionHandlerDemo.ino | 10 +++ .../Ethernet_ConnectionHandlerDemo.ino | 67 ------------------- 2 files changed, 10 insertions(+), 67 deletions(-) delete mode 100644 examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index 2246fc10..3cef607d 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -16,6 +16,9 @@ * NBConnectionHandler conMan(SECRET_PIN); */ + +//#define ARDUINO_ETHERNET_SHIELD /* Uncomment this line if you want to use a ethernet shield */ + #include "arduino_secrets.h" #include @@ -28,11 +31,18 @@ GSMConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_ NBConnectionHandler conMan(SECRET_PIN); #elif defined(BOARD_HAS_LORA) LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); +#elif defined(BOARD_HAS_ETHERNET) +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +EthernetConnectionHandler conMan(mac); #endif void setup() { Serial.begin(9600); /* Give a few seconds for the Serial connection to be available */ + #if defined(BOARD_HAS_ETHERNET) + SPI.begin(); + Ethernet.init(10); // CS pin on most Arduino shields + #endif delay(4000); setDebugMessageLevel(DBG_INFO); diff --git a/examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino b/examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino deleted file mode 100644 index 305221fd..00000000 --- a/examples/Ethernet_ConnectionHandlerDemo/Ethernet_ConnectionHandlerDemo.ino +++ /dev/null @@ -1,67 +0,0 @@ -/* SECRET_ fields are in arduino_secrets.h included above - * if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO - * WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding - * Network Name (SECRET_SSID) and password (SECRET_PASS) in the arduino_secrets.h - * file (or Secrets tab in Create Web Editor). - * - * WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS); - * - * If using a MKR GSM 1400 or other GSM boards supporting the same API you'll - * need a GSMConnectionHandler object as follows - * - * GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); - * - * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows - * - * NBConnectionHandler conMan(SECRET_PIN); - * If using an Ethernet based shield you will need a EthernetConnectionHandler object as follows - * - * EthernetConnectionHandler conMan(mac); - * - * this example was tested on a Teensy4.0 board with a Wiznet850 Ethernet Adapter - */ - -#include - -byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -EthernetConnectionHandler conMan(mac); - -void setup() { - SPI.begin(); - Ethernet.init(10); // The CS pin on most Arduino Ethernet shields - - Serial.begin(9600); - /* Give a few seconds for the Serial connection to be available */ - delay(4000); - - setDebugMessageLevel(DBG_INFO); - - conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); - conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); - conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); -} - -void loop() { - /* The following code keeps on running connection workflows on our - * ConnectionHandler object, hence allowing reconnection in case of failure - * and notification of connect/disconnect event if enabled (see - * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within - * the loop or methods called from it will delay the execution of .update(), - * which might not guarantee the correct functioning of the ConnectionHandler - * object. - */ - - conMan.check(); -} - -void onNetworkConnect() { - Serial.println(">>>> CONNECTED to network"); -} - -void onNetworkDisconnect() { - Serial.println(">>>> DISCONNECTED from network"); -} - -void onNetworkError() { - Serial.println(">>>> ERROR"); -} \ No newline at end of file