forked from arduino-libraries/Arduino_ConnectionHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionHandlerDemo.ino
81 lines (72 loc) · 2.78 KB
/
ConnectionHandlerDemo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* SECRET_ fields are in arduino_secrets.h included above
* if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
* Network Name (SECRET_SSID) and password (SECRET_PASS) in the arduino_secrets.h
* file (or Secrets tab in Create Web Editor).
*
* WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
*
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
* need a GSMConnectionHandler object as follows
*
* GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
*
* If using a MKR NB1500 you'll need a NBConnectionHandler object as follows
*
* NBConnectionHandler conMan(SECRET_PIN);
*
* If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows:
*
* DHCP mode
* EthernetConnectionHandler conMan();
*
* Manual configuration
* EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
*
* Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE.
*
*/
#include "arduino_secrets.h"
#include <Arduino_ConnectionHandler.h>
#if defined(BOARD_HAS_ETHERNET)
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
#elif defined(BOARD_HAS_WIFI)
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
#elif defined(BOARD_HAS_GSM)
GSMConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS);
#elif defined(BOARD_HAS_NB)
NBConnectionHandler conMan(SECRET_PIN);
#elif defined(BOARD_HAS_LORA)
LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY);
#endif
void setup() {
Serial.begin(9600);
/* Give a few seconds for the Serial connection to be available */
delay(4000);
#ifndef __AVR__
setDebugMessageLevel(DBG_INFO);
#endif
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
}
void loop() {
/* The following code keeps on running connection workflows on our
* ConnectionHandler object, hence allowing reconnection in case of failure
* and notification of connect/disconnect event if enabled (see
* addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within
* the loop or methods called from it will delay the execution of .update(),
* which might not guarantee the correct functioning of the ConnectionHandler
* object.
*/
conMan.check();
}
void onNetworkConnect() {
Serial.println(">>>> CONNECTED to network");
}
void onNetworkDisconnect() {
Serial.println(">>>> DISCONNECTED from network");
}
void onNetworkError() {
Serial.println(">>>> ERROR");
}