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 Host + Notecard connected over I2C you'll need a
4
+ * NotecardConnectionHandler object as follows
5
+ *
6
+ * NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
7
+ *
8
+ * If using a Host + Notecard connected over Serial you'll need a
9
+ * NotecardConnectionHandler object as follows
10
+ *
11
+ * NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID, Serial);
12
+ *
13
+ * If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
3
14
* 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).
15
+ * Network Name (SECRET_WIFI_SSID ) and password (SECRET_WIFI_PASS ) in the
16
+ * arduino_secrets.h file (or Secrets tab in Create Web Editor).
6
17
*
7
- * WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS );
18
+ * WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS );
8
19
*
9
20
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
10
21
* need a GSMConnectionHandler object as follows
27
38
*
28
39
*/
29
40
41
+ #include < Arduino_ConnectionHandler.h>
42
+
30
43
#include " arduino_secrets.h"
31
44
32
- #include < Arduino_ConnectionHandler.h>
45
+ #define CONN_TOGGLE_MS 60000
46
+
47
+ #if !(defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
48
+ defined (BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
49
+ #error " Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
50
+ #endif
33
51
34
- #if defined(BOARD_HAS_ETHERNET)
52
+ #if defined(USE_NOTECARD)
53
+ /* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
54
+ * or UART. An empty string (or the default value provided below) will not
55
+ * override the Notecard's existing configuration.
56
+ * Learn more at: https://dev.blues.io */
57
+ #define NOTECARD_PRODUCT_UID " com.domain.you:product"
58
+ #endif
59
+
60
+ #if defined(USE_NOTECARD)
61
+ NotecardConnectionHandler conMan (NOTECARD_PRODUCT_UID);
62
+ #elif defined(BOARD_HAS_ETHERNET)
35
63
EthernetConnectionHandler conMan (SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
36
64
#elif defined(BOARD_HAS_WIFI)
37
- WiFiConnectionHandler conMan (SECRET_SSID, SECRET_PASS );
65
+ WiFiConnectionHandler conMan (SECRET_WIFI_SSID, SECRET_WIFI_PASS );
38
66
#elif defined(BOARD_HAS_GSM)
39
67
GSMConnectionHandler conMan (SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
40
68
#elif defined(BOARD_HAS_NB)
@@ -47,19 +75,76 @@ CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GS
47
75
CellularConnectionHandler conMan (SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
48
76
#endif
49
77
78
+ bool attemptConnect = false ;
79
+ uint32_t lastConnToggleMs = 0 ;
80
+
50
81
void setup () {
82
+ /* Initialize serial debug port and wait up to 5 seconds for port to open */
51
83
Serial.begin (9600 );
52
- /* Give a few seconds for the Serial connection to be available */
53
- delay ( 4000 );
84
+ for ( unsigned long const serialBeginTime = millis (); ! Serial && ( millis () - serialBeginTime <= 5000 ); ) { }
85
+
54
86
#ifndef __AVR__
87
+ /* Set the debug message level:
88
+ * - DBG_ERROR: Only show error messages
89
+ * - DBG_WARNING: Show warning and error messages
90
+ * - DBG_INFO: Show info, warning, and error messages
91
+ * - DBG_DEBUG: Show debug, info, warning, and error messages
92
+ * - DBG_VERBOSE: Show all messages
93
+ */
55
94
setDebugMessageLevel (DBG_INFO);
56
95
#endif
96
+
97
+ /* Add callbacks to the ConnectionHandler object to get notified of network
98
+ * connection events. */
57
99
conMan.addCallback (NetworkConnectionEvent::CONNECTED, onNetworkConnect);
58
100
conMan.addCallback (NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
59
101
conMan.addCallback (NetworkConnectionEvent::ERROR, onNetworkError);
102
+
103
+ Serial.print (" Network Adapter Interface: " );
104
+ switch (conMan.getInterface ()) {
105
+ case NetworkAdapter::WIFI:
106
+ Serial.println (" Wi-Fi" );
107
+ break ;
108
+ case NetworkAdapter::ETHERNET:
109
+ Serial.println (" Ethernet" );
110
+ break ;
111
+ case NetworkAdapter::NB:
112
+ Serial.println (" Narrowband" );
113
+ break ;
114
+ case NetworkAdapter::GSM:
115
+ Serial.println (" GSM" );
116
+ break ;
117
+ case NetworkAdapter::LORA:
118
+ Serial.println (" LoRa" );
119
+ break ;
120
+ case NetworkAdapter::CATM1:
121
+ Serial.println (" Category M1" );
122
+ break ;
123
+ case NetworkAdapter::CELL:
124
+ Serial.println (" Cellular" );
125
+ break ;
126
+ case NetworkAdapter::NOTECARD:
127
+ Serial.println (" Notecard" );
128
+ break ;
129
+ default :
130
+ Serial.println (" Unknown" );
131
+ break ;
132
+ }
60
133
}
61
134
62
135
void loop () {
136
+ /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
137
+ if ((millis () - lastConnToggleMs) > CONN_TOGGLE_MS) {
138
+ Serial.println (" Toggling connection..." );
139
+ if (attemptConnect) {
140
+ conMan.connect ();
141
+ } else {
142
+ conMan.disconnect ();
143
+ }
144
+ attemptConnect = !attemptConnect;
145
+ lastConnToggleMs = millis ();
146
+ }
147
+
63
148
/* The following code keeps on running connection workflows on our
64
149
* ConnectionHandler object, hence allowing reconnection in case of failure
65
150
* and notification of connect/disconnect event if enabled (see
@@ -68,7 +153,6 @@ void loop() {
68
153
* which might not guarantee the correct functioning of the ConnectionHandler
69
154
* object.
70
155
*/
71
-
72
156
conMan.check ();
73
157
}
74
158
0 commit comments