Skip to content

Commit 6a2513c

Browse files
committed
Added example for ConnectionManager (WIP)
1 parent 69132fb commit 6a2513c

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifdef BOARD_HAS_WIFI
2+
3+
#ifndef ARDUINO_SAMD_MKR1000
4+
#include <WiFiNINA.h>
5+
#else
6+
#include <WiFi101.h>
7+
#endif
8+
9+
extern char ssid[];
10+
extern char pass[];
11+
12+
// WiFi client declaration
13+
WiFiClient wifiClient;
14+
15+
// specific WiFi module getTime method
16+
unsigned long getTime() {
17+
return WiFi.getTime();
18+
}
19+
20+
void initConnection() {
21+
int attempts = 0;
22+
23+
// check for the presence of the shield:
24+
if (WiFi.status() == NETWORK_HARDWARE_ERROR) {
25+
Serial.println("WiFi shield not present");
26+
// don't continue:
27+
while (true);
28+
}
29+
// attempt to connect to WiFi network:
30+
31+
while (status != NETWORK_CONNECTED && attempts++ < NETWORK_LAYER_CONNECTION_RETRIES) {
32+
Serial.print("Attempting to connect to WPA SSID: ");
33+
Serial.println(ssid);
34+
// Connect to WPA/WPA2 network:
35+
status = WiFi.begin(ssid, pass);
36+
// wait 10 seconds for connection:
37+
delay(NETWORK_LAYER_CONNECTION_TIMEOUT);
38+
}
39+
40+
if (status != NETWORK_CONNECTED) {
41+
Serial.println("Failed to connect to Wifi!");
42+
while (true);
43+
}
44+
45+
// you're connected now, so print out the data:
46+
Serial.println("You're connected to the WiFi network");
47+
48+
// begin of the ArduinoIoTCloud
49+
if (!ArduinoCloud.begin(wifiClient)) {
50+
Serial.println("Starting Arduino Cloud failed!");
51+
while (true);
52+
}
53+
54+
// onGetTime callback assignment.
55+
ArduinoCloud.onGetTime(getTime);
56+
57+
Serial.println("Attempting to connect to Arduino Cloud");
58+
attempts = 0;
59+
while (!ArduinoCloud.connect() && attempts++ < ARDUINO_IOT_CLOUD_CONNECTION_RETRIES) {
60+
Serial.print(".");
61+
delay(ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT);
62+
}
63+
64+
if (attempts >= ARDUINO_IOT_CLOUD_CONNECTION_RETRIES) {
65+
Serial.println("\nFailed to connect to Arduino Cloud!");
66+
while (1);
67+
}
68+
69+
Serial.println("\nSuccessfully connected to Arduino Cloud :)");
70+
71+
CloudSerial.begin(9600);
72+
}
73+
74+
75+
// check network connection, if it is disconnected, re-connect it.
76+
void checkNetworkConnection() {
77+
if (millis() - lastNetworkCheck > NETWORK_CONNECTION_INTERVAL) {
78+
Serial.print("<<Network Status: ");
79+
80+
// Do nothing if wifi is connected
81+
if (WiFi.status() == NETWORK_CONNECTED) {
82+
// uopdate cheking time
83+
Serial.println("CONNECTED");
84+
lastNetworkCheck = millis();
85+
return;
86+
}
87+
// Check Wifi status, until it is connected (blocking!!!)
88+
while (WiFi.status() != NETWORK_CONNECTED) {
89+
Serial.print("..Reconnection to connect to WPA SSID: ");
90+
Serial.println(ssid);
91+
status = WiFi.begin(ssid, pass);
92+
// wait 10 seconds for connection:
93+
delay(NETWORK_CONNECTION_TIMEOUT);
94+
}
95+
96+
Serial.println("..Reconnected to the Nework!");
97+
// Call the reconnect method to clean up the ArduinoCloud connection
98+
while (!ArduinoCloud.reconnect(wifiClient)) {
99+
delay(ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT);
100+
}
101+
102+
Serial.println("..Reconnected to the Cloud!");
103+
104+
delay(500);
105+
// uopdate cheking time
106+
lastNetworkCheck = millis();
107+
}
108+
}
109+
110+
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
3+
#define SECRET_GSMPIN ""
4+
#define SECRET_GPRSAPN ""
5+
#define SECRET_GPRSLOGIN ""
6+
#define SECRET_GPRSPWD ""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <GSMConnectionManager.h>
2+
3+
ConnectionManager *ArduinoIoTPreferredConnection = new GSMConnectionManager(
4+
SECRET_GSMPIN, SECRET_GPRSAPN, SECRET_GPRSLOGIN, SECRET_GPRSPWD);
5+
6+
void onLedSwitchChange();
7+
void onIntValueChange();
8+
9+
bool ledSwitch;
10+
int intValue;
11+
12+
void initProperties() {
13+
ArduinoCloud.setThingId(THING_ID);
14+
ArduinoCloud.addProperty(ledSwitch, READWRITE, ON_CHANGE, onLedSwitchChange);
15+
ArduinoCloud.addProperty(intValue, READWRITE, ON_CHANGE, onIntValueChange);
16+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "arduino_secrets.h"
2+
/*
3+
Sketch generated by the Arduino IoT Cloud Thing "test_getting_started_18_01"
4+
https://create.arduino.cc/cloud/things/52e306ef-a7b7-47e5-aacd-9046e71a70b6
5+
6+
Arduino IoT Cloud Properties description
7+
8+
The following variables are automatically generated and updated when changes are made to the Thing properties
9+
10+
bool ledSwitch;
11+
int intValue;
12+
13+
Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
14+
which are called when their values are changed from the Dashboard.
15+
These functions are generated with the Thing and added at the end of this sketch.
16+
*/
17+
18+
// Your THING_ID
19+
/* THING_ID should be part of cloudProperties.h */
20+
#define THING_ID "52e306ef-a7b7-47e5-aacd-9046e71a70b6"
21+
22+
// the following include needs data from the Secret tab to define the arrays above
23+
#include "cloudProperties.h"
24+
25+
/*
26+
The following data fields are filled by the Secret tab.
27+
Do not modify them here
28+
*/
29+
char ssid[] = SECRET_SSID; // your network SSID (name)
30+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
31+
32+
#define UPDATE_CYCLE_TIME 1000
33+
unsigned long lastTickTime;
34+
unsigned int cyclesCount = 0;
35+
36+
void setup() {
37+
// Initialize serial and wait for port to open:
38+
Serial.begin(9600);
39+
// wait up to 5 seconds for user to open Serial port
40+
unsigned long serialBeginTime = millis();
41+
while (!Serial && (millis() - serialBeginTime > 5000));
42+
43+
Serial.println("Starting Arduino IoT Cloud on MKR");
44+
45+
// Defined in cloudProperties.h
46+
initProperties();
47+
// Defined in ArduinoCloudSettings.h
48+
initConnection();
49+
50+
// set last network check to now, since it is connected
51+
//lastNetworkCheck = millis();
52+
//lastTickTime = millis();
53+
54+
ledSwitch = true;
55+
ledSwitch = false;
56+
intValue = 0;
57+
}
58+
59+
void loop() {
60+
unsigned long msNow = millis();
61+
ArduinoCloud.update();
62+
if(msNow - lastTickTime > UPDATE_CYCLE_TIME){
63+
intValue++;
64+
onIntValueChange();
65+
Serial.println(intValue);
66+
67+
lastTickTime = msNow;
68+
}
69+
70+
// Check if device is connected to the network, if it is do nothing, otherwise reconnect it. Leave this at the end of loop().
71+
checkNetworkConnection();
72+
}
73+
74+
ConnectionManager *conn = new GSMConnectionManager("", "", "", "");
75+
76+
void initConnection() {
77+
ArduinoIoTPreferredConnection->init();
78+
79+
// begin of the ArduinoIoTCloud
80+
if (!ArduinoCloud.begin(conn)) {
81+
Serial.println("Starting Arduino Cloud failed!");
82+
while (true);
83+
}
84+
85+
int attempts = 0;
86+
Serial.println("Attempting to connect to Arduino Cloud");
87+
while (!ArduinoCloud.connect() && attempts++ < ARDUINO_IOT_CLOUD_CONNECTION_RETRIES) {
88+
Serial.print(".");
89+
delay(ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT);
90+
}
91+
92+
if (attempts >= ARDUINO_IOT_CLOUD_CONNECTION_RETRIES) {
93+
Serial.println("\nFailed to connect to Arduino Cloud!");
94+
while (1);
95+
}
96+
97+
Serial.println("\nSuccessfully connected to Arduino Cloud :)");
98+
99+
CloudSerial.begin(9600);
100+
}
101+
102+
unsigned long lastNetworkCheck = millis();
103+
104+
void checkNetworkConnection() {
105+
ArduinoIoTPreferredConnection->check();
106+
107+
Serial.println("..Reconnected to the Nework!");
108+
109+
// Call the reconnect method to clean up the ArduinoCloud connection
110+
while (!ArduinoCloud.reconnect(ArduinoIoTPreferredConnection->getClient())) {
111+
delay(ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT);
112+
}
113+
114+
Serial.println("..Reconnected to the Cloud!");
115+
116+
delay(500);
117+
// update cheking time
118+
lastNetworkCheck = millis();
119+
}
120+
121+
void onLedSwitchChange() {
122+
Serial.print("LED set to ");
123+
Serial.println(ledSwitch);
124+
digitalWrite(LED_BUILTIN, ledSwitch);
125+
}
126+
127+
128+
void onIntValueChange() {
129+
Serial.print("integer set to ");
130+
Serial.println(intValue);
131+
}

0 commit comments

Comments
 (0)