Skip to content

Commit 98ca332

Browse files
Added an example for updateTimeoutInterval usage
1 parent 7a9f07b commit 98ca332

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* SECRET_ fields are in `arduino_secrets.h` (included below)
2+
*
3+
* This example is a lightly modified version of ConnectionHandlerDemo to showcase
4+
* the possibility of changing the timeout values for the different states a connectionhandler have
5+
*/
6+
7+
#include <Arduino_ConnectionHandler.h>
8+
9+
#include "arduino_secrets.h"
10+
11+
#if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
12+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
13+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
14+
#endif
15+
16+
#if defined(BOARD_HAS_ETHERNET)
17+
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
18+
#elif defined(BOARD_HAS_WIFI)
19+
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
20+
#elif defined(BOARD_HAS_GSM)
21+
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
22+
#elif defined(BOARD_HAS_NB)
23+
NBConnectionHandler conMan(SECRET_PIN);
24+
#elif defined(BOARD_HAS_LORA)
25+
LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY);
26+
#elif defined(BOARD_HAS_CATM1_NBIOT)
27+
CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
28+
#elif defined(BOARD_HAS_CELLULAR)
29+
CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
30+
#endif
31+
32+
bool attemptConnect = false;
33+
uint32_t lastConnToggleMs = 0;
34+
35+
void setup() {
36+
/* Initialize serial debug port and wait up to 5 seconds for port to open */
37+
Serial.begin(9600);
38+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
39+
40+
#ifndef __AVR__
41+
/* Set the debug message level:
42+
* - DBG_ERROR: Only show error messages
43+
* - DBG_WARNING: Show warning and error messages
44+
* - DBG_INFO: Show info, warning, and error messages
45+
* - DBG_DEBUG: Show debug, info, warning, and error messages
46+
* - DBG_VERBOSE: Show all messages
47+
*/
48+
setDebugMessageLevel(DBG_INFO);
49+
#endif
50+
51+
/* Add callbacks to the ConnectionHandler object to get notified of network
52+
* connection events. */
53+
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
54+
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
55+
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
56+
57+
/* By using updateTimeoutInterval I can change the timeout value for a specific
58+
* state of the connection handler
59+
*/
60+
conMan.updateTimeoutInterval(NetworkConnectionState::INIT, 8000);
61+
conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTING, 1000);
62+
conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTED, 20000);
63+
conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTING, 200);
64+
conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTED, 2000);
65+
conMan.updateTimeoutInterval(NetworkConnectionState::CLOSED, 2000);
66+
conMan.updateTimeoutInterval(NetworkConnectionState::ERROR, 2000);
67+
}
68+
69+
void loop() {
70+
conMan.check();
71+
}
72+
73+
void onNetworkConnect() {
74+
Serial.println(">>>> CONNECTED to network");
75+
}
76+
77+
void onNetworkDisconnect() {
78+
Serial.println(">>>> DISCONNECTED from network");
79+
}
80+
81+
void onNetworkError() {
82+
Serial.println(">>>> ERROR");
83+
}
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)