Skip to content

Commit ccc2dbb

Browse files
committed
- updated example
- minor cleanup
1 parent a2e77b3 commit ccc2dbb

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

+30-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@
44
#include <FTDebouncer.h>
55

66
/* SECRET_ fields are in arduino_secrets.h included above
7-
*/
7+
if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
8+
WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
9+
Network Name (SECRET_SSID) and password (SECRET_PASS) in the
10+
arduino_secrets.h file (or Secrets tab in Create Web Editor). (This example
11+
defaults to WiFi)
12+
13+
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
14+
15+
If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
16+
need a GSMConnectionHandler object as follows
17+
18+
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER,
19+
SECRET_GSM_PASS)
20+
*/
21+
822
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
923

1024
#define PIN_CONNECT 2
@@ -14,54 +28,36 @@ FTDebouncer deb;
1428

1529
void setup() {
1630
Serial.begin(9600);
17-
unsigned long serialBeginTime = millis();
18-
// while(millis() - serialBeginTime < 4000 || !Serial){}
31+
// give a few seconds for the Serial connection to be available
1932
delay(4000);
20-
setDebugMessageLevel(4);
33+
34+
setDebugMessageLevel(2);
35+
2136
deb.addPin(PIN_CONNECT, HIGH, INPUT_PULLUP);
2237
deb.addPin(PIN_DISCONNECT, HIGH, INPUT_PULLUP);
2338
deb.init();
24-
// conMan.addCallback(NetworkConnectionEvent::CONNECTED, &onNetworkConnect);
25-
conMan.addConnectCallback(onNetworkConnect);
26-
conMan.addDisconnectCallback(onNetworkDisconnect);
39+
40+
// the following methods allow the sketch to be notified when connected or
41+
// disconnected to the network
42+
43+
conMan.addConnectCallback(onNetworkConnect); // look at function onNetworkConnect towards the end of this sketch
44+
conMan.addDisconnectCallback(onNetworkDisconnect); // look at function onNetworkDisconnect towards the end of this sketch
2745
}
2846

2947
void loop() {
30-
deb.update();
48+
// the following code keeps on running connection workflows on our ConnectionHandler object,
49+
// hence allowing reconnection in case of failure and notification of connect/disconnect event if enabled (see addConnectCallback/addDisconnectCallback)
50+
// NOTE: any use of delay() within the loop or methods called from it will delay the execution of .update(),
51+
// which might not guarantee the correct functioning of the ConnectionHandler object.
52+
3153
conMan.update();
32-
}
3354

34-
void connectionStateChanged(NetworkConnectionState _newState) {
35-
Serial.println((int)_newState);
36-
}
37-
38-
void onPinActivated(uint8_t _pinNr) {
39-
Serial.print("activated ");
40-
Serial.println(_pinNr);
41-
switch (_pinNr) {
42-
case PIN_CONNECT:
43-
conMan.connect();
44-
break;
45-
case PIN_DISCONNECT:
46-
conMan.disconnect();
47-
break;
48-
}
49-
}
50-
void onPinDeactivated(uint8_t _pinNr) {
51-
Serial.print("deactivated ");
52-
Serial.println(_pinNr);
53-
switch (_pinNr) {
54-
default: {
55-
} break;
56-
}
5755
}
5856

5957
void onNetworkConnect(void *_arg) {
60-
Serial.println(">>>> CALLBACK!!!");
6158
Serial.println(">>>> CONNECTED to network");
6259
}
6360

6461
void onNetworkDisconnect(void *_arg) {
65-
Serial.println(">>>> CALLBACK!!!");
6662
Serial.println(">>>> DISCONNECTED from network");
6763
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
const char SECRET_SSID[] = "NETWORK_NAME";
2-
const char SECRET_PASS[] = "NETWORK_PASS";
1+
const char SECRET_SSID[] = "NETWORK NAME";
2+
const char SECRET_PASS[] = "NETWORK PASSWORD";
3+
4+
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
5+
const char SECRET_PIN[] = "0000";
6+
const char SECRET_GSM_USER[] = "GSM USERNAME";
7+
const char SECRET_GSM_PASS[] = "GSM PASSWORD";

src/Arduino_ConnectionHandler.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <Client.h>
2626
#include <Udp.h>
2727

28-
//#include "NTPUtils.h"
2928
#include <Arduino_DebugUtils.h>
3029

3130
/******************************************************************************
@@ -131,4 +130,10 @@ class ConnectionHandler {
131130
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED
132131
#endif
133132

133+
#ifdef BOARD_HAS_WIFI
134+
#include "Arduino_WiFiConnectionHandler.h"
135+
#elif define(BOARD_HAS_GSM)
136+
#include "Arduino_GSMConnectionHandler.h"
137+
#endif
138+
134139
#endif /* CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)