1
- /* SECRET_ fields are in arduino_secrets.h included above
2
- * if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
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
3
4
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
4
- * Network Name (SECRET_SSID ) and password (SECRET_PASS ) in the arduino_secrets.h
5
- * file (or Secrets tab in Create Web Editor).
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).
6
7
*
7
- * WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS );
8
+ * WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS );
8
9
*
9
10
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
10
11
* need a GSMConnectionHandler object as follows
27
28
*
28
29
*/
29
30
31
+ #include < Arduino_ConnectionHandler.h>
32
+
30
33
#include " arduino_secrets.h"
31
34
32
- #include < Arduino_ConnectionHandler.h>
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
33
41
34
42
#if defined(BOARD_HAS_ETHERNET)
35
43
EthernetConnectionHandler conMan (SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
36
44
#elif defined(BOARD_HAS_WIFI)
37
- WiFiConnectionHandler conMan (SECRET_SSID, SECRET_PASS );
45
+ WiFiConnectionHandler conMan (SECRET_WIFI_SSID, SECRET_WIFI_PASS );
38
46
#elif defined(BOARD_HAS_GSM)
39
47
GSMConnectionHandler conMan (SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
40
48
#elif defined(BOARD_HAS_NB)
@@ -47,19 +55,73 @@ CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GS
47
55
CellularConnectionHandler conMan (SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
48
56
#endif
49
57
58
+ bool attemptConnect = false ;
59
+ uint32_t lastConnToggleMs = 0 ;
60
+
50
61
void setup () {
62
+ /* Initialize serial debug port and wait up to 5 seconds for port to open */
51
63
Serial.begin (9600 );
52
- /* Give a few seconds for the Serial connection to be available */
53
- delay ( 4000 );
64
+ for ( unsigned long const serialBeginTime = millis (); ! Serial && ( millis () - serialBeginTime <= 5000 ); ) { }
65
+
54
66
#ifndef __AVR__
67
+ /* Set the debug message level:
68
+ * - DBG_ERROR: Only show error messages
69
+ * - DBG_WARNING: Show warning and error messages
70
+ * - DBG_INFO: Show info, warning, and error messages
71
+ * - DBG_DEBUG: Show debug, info, warning, and error messages
72
+ * - DBG_VERBOSE: Show all messages
73
+ */
55
74
setDebugMessageLevel (DBG_INFO);
56
75
#endif
76
+
77
+ /* Add callbacks to the ConnectionHandler object to get notified of network
78
+ * connection events. */
57
79
conMan.addCallback (NetworkConnectionEvent::CONNECTED, onNetworkConnect);
58
80
conMan.addCallback (NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
59
81
conMan.addCallback (NetworkConnectionEvent::ERROR, onNetworkError);
82
+
83
+ Serial.print (" Network Adapter Interface: " );
84
+ switch (conMan.getInterface ()) {
85
+ case NetworkAdapter::WIFI:
86
+ Serial.println (" Wi-Fi" );
87
+ break ;
88
+ case NetworkAdapter::ETHERNET:
89
+ Serial.println (" Ethernet" );
90
+ break ;
91
+ case NetworkAdapter::NB:
92
+ Serial.println (" Narrowband" );
93
+ break ;
94
+ case NetworkAdapter::GSM:
95
+ Serial.println (" GSM" );
96
+ break ;
97
+ case NetworkAdapter::LORA:
98
+ Serial.println (" LoRa" );
99
+ break ;
100
+ case NetworkAdapter::CATM1:
101
+ Serial.println (" Category M1" );
102
+ break ;
103
+ case NetworkAdapter::CELL:
104
+ Serial.println (" Cellular" );
105
+ break ;
106
+ default :
107
+ Serial.println (" Unknown" );
108
+ break ;
109
+ }
60
110
}
61
111
62
112
void loop () {
113
+ /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
114
+ if ((millis () - lastConnToggleMs) > CONN_TOGGLE_MS) {
115
+ Serial.println (" Toggling connection..." );
116
+ if (attemptConnect) {
117
+ conMan.connect ();
118
+ } else {
119
+ conMan.disconnect ();
120
+ }
121
+ attemptConnect = !attemptConnect;
122
+ lastConnToggleMs = millis ();
123
+ }
124
+
63
125
/* The following code keeps on running connection workflows on our
64
126
* ConnectionHandler object, hence allowing reconnection in case of failure
65
127
* and notification of connect/disconnect event if enabled (see
@@ -68,7 +130,6 @@ void loop() {
68
130
* which might not guarantee the correct functioning of the ConnectionHandler
69
131
* object.
70
132
*/
71
-
72
133
conMan.check ();
73
134
}
74
135
0 commit comments