Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0e42d70

Browse files
committedJan 30, 2025
adding an example for GenericConnectionHandler
1 parent e7f9da7 commit 0e42d70

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
 
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
*/
30+
31+
#include <GenericConnectionHandler.h>
32+
33+
#include "arduino_secrets.h"
34+
35+
#define CONN_TOGGLE_MS 60000
36+
37+
#if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
38+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
39+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
40+
#endif
41+
42+
GenericConnectionHandler conMan;
43+
44+
45+
bool attemptConnect = false;
46+
uint32_t lastConnToggleMs = 0;
47+
48+
void setup() {
49+
/* Initialize serial debug port and wait up to 5 seconds for port to open */
50+
Serial.begin(9600);
51+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
52+
53+
#ifndef __AVR__
54+
/* Set the debug message level:
55+
* - DBG_ERROR: Only show error messages
56+
* - DBG_WARNING: Show warning and error messages
57+
* - DBG_INFO: Show info, warning, and error messages
58+
* - DBG_DEBUG: Show debug, info, warning, and error messages
59+
* - DBG_VERBOSE: Show all messages
60+
*/
61+
setDebugMessageLevel(DBG_INFO);
62+
#endif
63+
64+
models::NetworkSetting setting{
65+
NetworkAdapter::WIFI
66+
};
67+
strcpy(setting.wifi.ssid, SECRET_WIFI_SSID);
68+
strcpy(setting.wifi.pwd, SECRET_WIFI_PASS);
69+
70+
conMan.updateSetting(setting);
71+
72+
/* Add callbacks to the ConnectionHandler object to get notified of network
73+
* connection events. */
74+
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
75+
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
76+
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
77+
78+
Serial.print("Network Adapter Interface: ");
79+
switch (conMan.getInterface()) {
80+
case NetworkAdapter::WIFI:
81+
Serial.println("Wi-Fi");
82+
break;
83+
case NetworkAdapter::ETHERNET:
84+
Serial.println("Ethernet");
85+
break;
86+
case NetworkAdapter::NB:
87+
Serial.println("Narrowband");
88+
break;
89+
case NetworkAdapter::GSM:
90+
Serial.println("GSM");
91+
break;
92+
case NetworkAdapter::LORA:
93+
Serial.println("LoRa");
94+
break;
95+
case NetworkAdapter::CATM1:
96+
Serial.println("Category M1");
97+
break;
98+
case NetworkAdapter::CELL:
99+
Serial.println("Cellular");
100+
break;
101+
default:
102+
Serial.println("Unknown");
103+
break;
104+
}
105+
}
106+
107+
void loop() {
108+
/* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
109+
if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) {
110+
Serial.println("Toggling connection...");
111+
if (attemptConnect) {
112+
conMan.connect();
113+
} else {
114+
conMan.disconnect();
115+
}
116+
attemptConnect = !attemptConnect;
117+
lastConnToggleMs = millis();
118+
}
119+
120+
/* The following code keeps on running connection workflows on our
121+
* ConnectionHandler object, hence allowing reconnection in case of failure
122+
* and notification of connect/disconnect event if enabled (see
123+
* addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within
124+
* the loop or methods called from it will delay the execution of .update(),
125+
* which might not guarantee the correct functioning of the ConnectionHandler
126+
* object.
127+
*/
128+
conMan.check();
129+
}
130+
131+
void onNetworkConnect() {
132+
Serial.println(">>>> CONNECTED to network");
133+
}
134+
135+
void onNetworkDisconnect() {
136+
Serial.println(">>>> DISCONNECTED from network");
137+
}
138+
139+
void onNetworkError() {
140+
Serial.println(">>>> ERROR");
141+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Required for WiFiConnectionHandler
2+
const char SECRET_WIFI_SSID[] = "SSID";
3+
const char SECRET_WIFI_PASS[] = "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)
Please sign in to comment.