Skip to content

Commit d905de9

Browse files
committed
Added implementation for WiFiConnection Manager (WIP)
1 parent 6a2513c commit d905de9

File tree

5 files changed

+283
-27
lines changed

5 files changed

+283
-27
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ void ArduinoIoTCloudClass::update(int const reconnectionMaxRetries, int const re
182182
}
183183
}
184184

185-
int ArduinoIoTCloudClass::reconnect(Client& /*net*/)
185+
int ArduinoIoTCloudClass::reconnect()
186186
{
187-
// check if MQTT client is still connected
188187
if (_mqttClient->connected()) {
189188
_mqttClient->stop();
190189
}
@@ -256,4 +255,61 @@ void ArduinoIoTCloudClass::handleMessage(int length)
256255
}
257256
}
258257

258+
void ArduinoIoTCloudClass::connectionCheck() {
259+
connection->check();
260+
if (connection->getStatus() != CONNECTION_STATE_CONNECTED) {
261+
iotStatus = IOT_STATUS_CLOUD_DISCONNECTED;
262+
return;
263+
}
264+
char msgBuffer[120];
265+
int arduinoIoTConnectionAttempt;
266+
267+
switch (iotStatus) {
268+
case IOT_STATUS_IDLE:
269+
if (!begin(connection)) {
270+
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
271+
iotStatus = IOT_STATUS_CLOUD_ERROR;
272+
return;
273+
}
274+
// TODO: Controlla bene che non serva
275+
//ArduinoCloud.onGetTime(getTime);
276+
iotStatus = IOT_STATUS_CLOUD_CONNECTING;
277+
break;
278+
case IOT_STATUS_CLOUD_ERROR:
279+
debugMessage("Cloud Error. Retrying...", 0);
280+
break;
281+
case IOT_STATUS_CLOUD_CONNECTED:
282+
debugMessage("connected to Arduino IoT Cloud", 2);
283+
break;
284+
case IOT_STATUS_CLOUD_DISCONNECTED:
285+
iotStatus = IOT_STATUS_CLOUD_RECONNECTING;
286+
break;
287+
case IOT_STATUS_CLOUD_RECONNECTING:
288+
debugMessage("IoT Cloud reconnecting...", 1);
289+
//wifiClient.stop();
290+
arduinoIoTConnectionAttempt = reconnect();
291+
*msgBuffer = 0;
292+
sprintf(msgBuffer, "ArduinoCloud.reconnect(): %d", arduinoIoTConnectionAttempt);
293+
debugMessage(msgBuffer, 1);
294+
if (arduinoIoTConnectionAttempt) {
295+
CloudSerial.begin(9600);
296+
CloudSerial.println("Hello from Cloud Serial!");
297+
iotStatus = IOT_STATUS_CLOUD_CONNECTED;
298+
}
299+
break;
300+
case IOT_STATUS_CLOUD_CONNECTING:
301+
debugMessage("IoT Cloud connecting...", 3);
302+
arduinoIoTConnectionAttempt = connect();
303+
*msgBuffer = 0;
304+
sprintf(msgBuffer, "ArduinoCloud.connect(): %d", arduinoIoTConnectionAttempt);
305+
debugMessage(msgBuffer, 2);
306+
if (arduinoIoTConnectionAttempt) {
307+
CloudSerial.begin(9600);
308+
CloudSerial.println("Hello from Cloud Serial!");
309+
iotStatus = IOT_STATUS_CLOUD_CONNECTED;
310+
}
311+
break;
312+
}
313+
}
314+
259315
ArduinoIoTCloudClass ArduinoCloud;

src/ArduinoIoTCloud.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ typedef struct {
2323

2424
extern ConnectionManager *ArduinoIoTPreferredConnection;
2525

26+
enum ArduinoIoTConnectionStatus {
27+
IOT_STATUS_IDLE,/* only at start */
28+
IOT_STATUS_CLOUD_IDLE,
29+
IOT_STATUS_CLOUD_CONNECTING,
30+
IOT_STATUS_CLOUD_CONNECTED,
31+
IOT_STATUS_CLOUD_DISCONNECTED,
32+
IOT_STATUS_CLOUD_RECONNECTING,
33+
IOT_STATUS_CLOUD_ERROR,
34+
IOT_STATUS_NETWORK_IDLE,
35+
IOT_STATUS_NETWORK_CONNECTED,
36+
IOT_STATUS_NETWORK_CONNECTING,
37+
IOT_STATUS_NETWORK_DISCONNECTED,
38+
IOT_STATUS_NETWORK_ERROR,
39+
IOT_STATUS_ERROR_GENERIC
40+
};
41+
2642
class ArduinoIoTCloudClass {
2743

2844
public:
@@ -47,7 +63,7 @@ class ArduinoIoTCloudClass {
4763

4864
int connected();
4965
// Clean up existing Mqtt connection, create a new one and initialize it
50-
int reconnect(Client& net);
66+
int reconnect();
5167

5268
inline void setThingId(String const thing_id) { _thing_id = thing_id; };
5369

@@ -78,6 +94,8 @@ class ArduinoIoTCloudClass {
7894
return Thing.addPropertyReal(property, name, permission);
7995
}
8096

97+
void connectionCheck();
98+
8199
protected:
82100
friend class CloudSerialClass;
83101
int writeStdout(const byte data[], int length);
@@ -87,7 +105,10 @@ class ArduinoIoTCloudClass {
87105
// Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout)
88106
bool mqttReconnect(int const maxRetries, int const timeout);
89107

108+
ArduinoIoTConnectionStatus getIoTStatus() { return iotStatus; }
109+
90110
private:
111+
ArduinoIoTConnectionStatus iotStatus = IOT_STATUS_IDLE;
91112
ConnectionManager *connection;
92113
static void onMessage(int length);
93114
void handleMessage(int length);

src/ConnectionManager.h

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1+
12
#ifndef CONNECTION_MANAGER_H_INCLUDED
23
#define CONNECTION_MANAGER_H_INCLUDED
34

5+
#ifndef ARDUINO_CLOUD_DEBUG_LEVEL
6+
#define ARDUINO_CLOUD_DEBUG_LEVEL 3
7+
#endif
8+
49
#include <Client.h>
510

6-
/* NOT USED FOR NOW */
7-
enum ArduinoIoTConnectionStatus {
8-
IOT_STATUS_IDLE,/* only at start */
9-
IOT_STATUS_CLOUD_IDLE,
10-
IOT_STATUS_CLOUD_CONNECTING,
11-
IOT_STATUS_CLOUD_CONNECTED,
12-
IOT_STATUS_CLOUD_DISCONNECTED,
13-
IOT_STATUS_CLOUD_ERROR,
14-
IOT_STATUS_NETWORK_IDLE,
15-
IOT_STATUS_NETWORK_CONNECTED,
16-
IOT_STATUS_NETWORK_CONNECTING,
17-
IOT_STATUS_NETWORK_DISCONNECTED,
18-
IOT_STATUS_NETWORK_ERROR,
19-
IOT_STATUS_ERROR_GENERIC
11+
enum NetworkConnectionState {
12+
CONNECTION_STATE_IDLE,
13+
CONNECTION_STATE_INIT,
14+
CONNECTION_STATE_CONNECTING,
15+
CONNECTION_STATE_CONNECTED,
16+
CONNECTION_STATE_GETTIME,
17+
CONNECTION_STATE_DISCONNECTING,
18+
CONNECTION_STATE_DISCONNECTED,
19+
CONNECTION_STATE_ERROR
2020
};
2121

2222
class ConnectionManager {
2323
public:
2424
virtual void init() = 0;
2525
virtual void check() = 0;
26-
virtual ArduinoIoTConnectionStatus status() = 0;
2726
virtual unsigned long getTime() = 0;
2827
virtual Client &getClient();
28+
29+
virtual NetworkConnectionState getStatus() { return netConnectionState; }
30+
31+
protected:
32+
unsigned long lastValidTimestamp = 0;
33+
NetworkConnectionState netConnectionState = CONNECTION_STATE_IDLE;
2934
};
3035

36+
// Network Connection Status
37+
//int networkStatus = NETWORK_IDLE_STATUS;
38+
3139
// ********* NETWORK LAYER **********
3240
// max network layer connection retries
3341
#define NETWORK_LAYER_CONNECTION_RETRIES 6
@@ -42,10 +50,6 @@ class ConnectionManager {
4250
// max wifi connection retries
4351
#define ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT 3000
4452

45-
// Network Connection Status
46-
//int iotStatus = IOT_STATUS_IDLE;
47-
//int networkStatus = NETWORK_IDLE_STATUS;
48-
4953
// === NETWORK CONNECTION MANAGEMENT ===
5054
// last time when the Network Connection was checked
5155
//unsigned long lastNetworkCheck = 0;
@@ -56,7 +60,6 @@ class ConnectionManager {
5660

5761
#ifdef ARDUINO_SAMD_MKR1000
5862
#include <WiFi101.h>
59-
#include "WiFiConnectionManager.h"
6063
#define BOARD_HAS_WIFI
6164
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
6265
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
@@ -65,7 +68,6 @@ class ConnectionManager {
6568

6669
#ifdef ARDUINO_SAMD_MKRWIFI1010
6770
#include <WiFiNINA.h>
68-
#include "WiFiConnectionManager.h"
6971
#define BOARD_HAS_WIFI
7072
#define NETWORK_HARDWARE_ERROR WL_NO_MODULE
7173
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
@@ -82,4 +84,13 @@ class ConnectionManager {
8284

8385
#include <ArduinoIoTCloud.h>
8486

85-
#endif
87+
inline void debugMessage(char *_msg, uint8_t _debugLevel) {
88+
if (_debugLevel <= ARDUINO_CLOUD_DEBUG_LEVEL) {
89+
char prepend[20];
90+
sprintf(prepend, "\n[ %d ] ", millis());
91+
Serial.print(prepend);
92+
Serial.println(_msg);
93+
}
94+
}
95+
96+
#endif

src/GSMConnectionManager.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class GSMConnectionManager : public ConnectionManager {
99
virtual void init();
1010
virtual void check();
1111
virtual Client &getClient() { return networkClient; };
12-
virtual ArduinoIoTConnectionStatus status() { return IOT_STATUS_IDLE; };
1312

1413
GSMClient networkClient;
1514
GSM gsmAccess;

0 commit comments

Comments
 (0)