Skip to content

Commit b8a5282

Browse files
committed
add example
1 parent c2a81e5 commit b8a5282

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

.github/workflows/compile-examples.yml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
- name: Blues Wireless Notecard
3939
SKETCH_PATHS: |
4040
- examples/ConnectionHandlerDemo
41+
- examples/CheckInternetAvailabilityDemo
4142
ARDUINOCORE_MBED_STAGING_PATH: extras/ArduinoCore-mbed
4243
ARDUINOCORE_API_STAGING_PATH: extras/ArduinoCore-API
4344
SKETCHES_REPORTS_PATH: sketches-reports
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/* SECRET_ fields are in `arduino_secrets.h` (included below)
2+
*
3+
* If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
4+
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
5+
* Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the
6+
* arduino_secrets.h file (or Secrets tab in Create Web Editor).
7+
*
8+
* WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
9+
*
10+
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
11+
* need a GSMConnectionHandler object as follows
12+
*
13+
* GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
14+
*
15+
* If using a MKR NB1500 you'll need a NBConnectionHandler object as follows
16+
*
17+
* NBConnectionHandler conMan(SECRET_PIN);
18+
*
19+
* If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows:
20+
*
21+
* DHCP mode
22+
* EthernetConnectionHandler conMan;
23+
*
24+
* Manual configuration
25+
* EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
26+
*
27+
* Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE.
28+
*
29+
* This sketch enables the ConnectionHandler to check for internet availability (only for IP based connectivity)
30+
* before reporting the Connected state. By deafult the check is disabled.
31+
*
32+
*/
33+
34+
#include <Arduino_ConnectionHandler.h>
35+
36+
#include "arduino_secrets.h"
37+
38+
#define CONN_TOGGLE_MS 60000
39+
40+
#if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
41+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
42+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
43+
#endif
44+
45+
#if defined(BOARD_HAS_ETHERNET)
46+
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
47+
#elif defined(BOARD_HAS_WIFI)
48+
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
49+
#elif defined(BOARD_HAS_GSM)
50+
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
51+
#elif defined(BOARD_HAS_NB)
52+
NBConnectionHandler conMan(SECRET_PIN);
53+
#elif defined(BOARD_HAS_LORA)
54+
LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY);
55+
#elif defined(BOARD_HAS_CATM1_NBIOT)
56+
CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
57+
#elif defined(BOARD_HAS_CELLULAR)
58+
CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
59+
#endif
60+
61+
bool attemptConnect = false;
62+
uint32_t lastConnToggleMs = 0;
63+
64+
void setup() {
65+
/* Initialize serial debug port and wait up to 5 seconds for port to open */
66+
Serial.begin(9600);
67+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
68+
69+
#ifndef __AVR__
70+
/* Set the debug message level:
71+
* - DBG_ERROR: Only show error messages
72+
* - DBG_WARNING: Show warning and error messages
73+
* - DBG_INFO: Show info, warning, and error messages
74+
* - DBG_DEBUG: Show debug, info, warning, and error messages
75+
* - DBG_VERBOSE: Show all messages
76+
*/
77+
setDebugMessageLevel(DBG_INFO);
78+
#endif
79+
/* Enable the connection handler to check for internet availability.
80+
* By default is disabled.
81+
*/
82+
conMan.enableCheckInternetAvailability(true);
83+
/* Add callbacks to the ConnectionHandler object to get notified of network
84+
* connection events. */
85+
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
86+
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
87+
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
88+
89+
Serial.print("Network Adapter Interface: ");
90+
switch (conMan.getInterface()) {
91+
case NetworkAdapter::WIFI:
92+
Serial.println("Wi-Fi");
93+
break;
94+
case NetworkAdapter::ETHERNET:
95+
Serial.println("Ethernet");
96+
break;
97+
case NetworkAdapter::NB:
98+
Serial.println("Narrowband");
99+
break;
100+
case NetworkAdapter::GSM:
101+
Serial.println("GSM");
102+
break;
103+
case NetworkAdapter::LORA:
104+
Serial.println("LoRa");
105+
break;
106+
case NetworkAdapter::CATM1:
107+
Serial.println("Category M1");
108+
break;
109+
case NetworkAdapter::CELL:
110+
Serial.println("Cellular");
111+
break;
112+
default:
113+
Serial.println("Unknown");
114+
break;
115+
}
116+
}
117+
118+
void loop() {
119+
/* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
120+
if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) {
121+
Serial.println("Toggling connection...");
122+
if (attemptConnect) {
123+
conMan.connect();
124+
} else {
125+
conMan.disconnect();
126+
}
127+
attemptConnect = !attemptConnect;
128+
lastConnToggleMs = millis();
129+
}
130+
131+
/* The following code keeps on running connection workflows on our
132+
* ConnectionHandler object, hence allowing reconnection in case of failure
133+
* and notification of connect/disconnect event if enabled (see
134+
* addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within
135+
* the loop or methods called from it will delay the execution of .update(),
136+
* which might not guarantee the correct functioning of the ConnectionHandler
137+
* object.
138+
*/
139+
conMan.check();
140+
}
141+
142+
void onNetworkConnect() {
143+
Serial.println(">>>> CONNECTED to network");
144+
}
145+
146+
void onNetworkDisconnect() {
147+
Serial.println(">>>> DISCONNECTED from network");
148+
}
149+
150+
void onNetworkError() {
151+
Serial.println(">>>> ERROR");
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Required for WiFiConnectionHandler
2+
const char SECRET_WIFI_SSID[] = "NETWORK NAME";
3+
const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD";
4+
5+
// Required for GSMConnectionHandler
6+
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
7+
const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler
8+
const char SECRET_GSM_USER[] = "GSM USERNAME";
9+
const char SECRET_GSM_PASS[] = "GSM PASSWORD";
10+
11+
// Required for LoRaConnectionHandler
12+
const char SECRET_APP_EUI[] = "APP_EUI";
13+
const char SECRET_APP_KEY[] = "APP_KEY";
14+
15+
// Required for EthernetConnectionHandler (without DHCP mode)
16+
const char SECRET_IP[] = "IP ADDRESS";
17+
const char SECRET_DNS[] = "DNS ADDRESS";
18+
const char SECRET_GATEWAY[] = "GATEWAY ADDRESS";
19+
const char SECRET_NETMASK[] = "NETWORK MASK";

0 commit comments

Comments
 (0)