Skip to content

Commit bff5110

Browse files
ubidefeofacchinm
authored andcommitted
Cleaner, more elegant logging (#24)
Restructured logging procedures - updated logging levels per message type (more in the future) - cleaned up redundancies - increased performances - messages only printed during change of state (once) * - added WiFi Firmware version check and update message - tested on MKR1000 and MKR1010 NOTE: WiFINina wrongly reports "1.2.0" when WiFi.firmwareVersion() is invoked, although version "1.2.1" is set in the source files
1 parent 4addca0 commit bff5110

File tree

4 files changed

+100
-60
lines changed

4 files changed

+100
-60
lines changed

Diff for: src/ArduinoIoTCloud.cpp

+43-25
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ void ArduinoIoTCloudClass::connectionCheck() {
304304

305305
if (connection->getStatus() != CONNECTION_STATE_CONNECTED) {
306306
if(iotStatus == IOT_STATUS_CLOUD_CONNECTED){
307-
iotStatus = IOT_STATUS_CLOUD_DISCONNECTED;
307+
setIoTConnectionState(IOT_STATUS_CLOUD_DISCONNECTED);
308308
}else{
309-
iotStatus = IOT_STATUS_CLOUD_CONNECTING;
309+
//setIoTConnectionState(IOT_STATUS_CLOUD_CONNECTING);
310310
}
311311
return;
312312
}
@@ -317,61 +317,79 @@ void ArduinoIoTCloudClass::connectionCheck() {
317317

318318
switch (iotStatus) {
319319
case IOT_STATUS_IDLE:
320+
{
321+
int connectionAttempt;
320322
if(connection == NULL){
321-
if(!begin(*_net, _brokerAddress)){
322-
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
323-
iotStatus = IOT_STATUS_CLOUD_ERROR;
324-
return;
325-
}
323+
connectionAttempt = begin(*_net, _brokerAddress);
326324
}else{
327-
if (!begin(connection, _brokerAddress)) {
328-
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
329-
iotStatus = IOT_STATUS_CLOUD_ERROR;
330-
return;
331-
}
325+
connectionAttempt = begin(connection, _brokerAddress);
332326
}
333-
334-
iotStatus = IOT_STATUS_CLOUD_CONNECTING;
327+
if(!connectionAttempt){
328+
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
329+
setIoTConnectionState(IOT_STATUS_CLOUD_ERROR);
330+
return;
331+
}
332+
setIoTConnectionState(IOT_STATUS_CLOUD_CONNECTING);
335333
break;
334+
}
335+
336336
case IOT_STATUS_CLOUD_ERROR:
337337
debugMessage("Cloud Error. Retrying...", 0);
338-
iotStatus = IOT_STATUS_CLOUD_RECONNECTING;
338+
setIoTConnectionState(IOT_STATUS_CLOUD_RECONNECTING);
339339
break;
340340
case IOT_STATUS_CLOUD_CONNECTED:
341-
debugMessage("connected to Arduino IoT Cloud", 3);
341+
debugMessage(".", 4, false, true);
342342
break;
343343
case IOT_STATUS_CLOUD_DISCONNECTED:
344-
debugMessage("disconnected from Arduino IoT Cloud", 0);
345-
iotStatus = IOT_STATUS_CLOUD_RECONNECTING;
344+
setIoTConnectionState(IOT_STATUS_CLOUD_RECONNECTING);
346345
break;
347346
case IOT_STATUS_CLOUD_RECONNECTING:
348-
debugMessage("IoT Cloud reconnecting...", 1);
349-
//wifiClient.stop();
350347
int arduinoIoTReconnectionAttempt;
351348
arduinoIoTReconnectionAttempt = reconnect(*_net);
352349
*msgBuffer = 0;
353350
sprintf(msgBuffer, "ArduinoCloud.reconnect(): %d", arduinoIoTReconnectionAttempt);
354-
debugMessage(msgBuffer, 1);
351+
debugMessage(msgBuffer, 2);
355352
if (arduinoIoTReconnectionAttempt == 1) {
356-
iotStatus = IOT_STATUS_CLOUD_CONNECTED;
353+
setIoTConnectionState(IOT_STATUS_CLOUD_CONNECTED);
357354
CloudSerial.begin(9600);
358355
CloudSerial.println("Hello from Cloud Serial!");
359356
}
360357
break;
361358
case IOT_STATUS_CLOUD_CONNECTING:
362-
debugMessage("IoT Cloud connecting...", 1);
359+
363360
int arduinoIoTConnectionAttempt;
364361
arduinoIoTConnectionAttempt = connect();
365362
*msgBuffer = 0;
366363
sprintf(msgBuffer, "ArduinoCloud.connect(): %d", arduinoIoTConnectionAttempt);
367-
debugMessage(msgBuffer, 2);
364+
debugMessage(msgBuffer, 4);
368365
if (arduinoIoTConnectionAttempt == 1) {
369-
iotStatus = IOT_STATUS_CLOUD_CONNECTED;
366+
setIoTConnectionState(IOT_STATUS_CLOUD_CONNECTED);
370367
CloudSerial.begin(9600);
371368
CloudSerial.println("Hello from Cloud Serial!");
372369
}
373370
break;
374371
}
375372
}
376373

374+
void ArduinoIoTCloudClass::setIoTConnectionState(ArduinoIoTConnectionStatus _newState){
375+
switch(_newState){
376+
case IOT_STATUS_CLOUD_ERROR:
377+
debugMessage("Arduino, we have a problem.", 0);
378+
break;
379+
case IOT_STATUS_CLOUD_CONNECTING:
380+
debugMessage("Connecting to Arduino IoT Cloud...", 0);
381+
break;
382+
case IOT_STATUS_CLOUD_RECONNECTING:
383+
debugMessage("Reconnecting to Arduino IoT Cloud...", 0);
384+
break;
385+
case IOT_STATUS_CLOUD_CONNECTED:
386+
debugMessage("Connected to Arduino IoT Cloud", 0);
387+
break;
388+
case IOT_STATUS_CLOUD_DISCONNECTED:
389+
debugMessage("Disconnected from Arduino IoT Cloud", 0);
390+
break;
391+
}
392+
iotStatus = _newState;
393+
}
394+
377395
ArduinoIoTCloudClass ArduinoCloud;

Diff for: src/ArduinoIoTCloud.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class ArduinoIoTCloudClass {
121121
bool mqttReconnect(int const maxRetries, int const timeout);
122122

123123
ArduinoIoTConnectionStatus getIoTStatus() { return iotStatus; }
124-
124+
void setIoTConnectionState(ArduinoIoTConnectionStatus _newState);
125125
private:
126126
ArduinoIoTConnectionStatus iotStatus = IOT_STATUS_IDLE;
127127
ConnectionManager *connection;

Diff for: src/ConnectionManager.h

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

2727
enum NetworkConnectionState {
28-
CONNECTION_STATE_IDLE,
2928
CONNECTION_STATE_INIT,
3029
CONNECTION_STATE_CONNECTING,
3130
CONNECTION_STATE_CONNECTED,
@@ -46,7 +45,7 @@ class ConnectionManager {
4645

4746
protected:
4847
unsigned long lastValidTimestamp = 0;
49-
NetworkConnectionState netConnectionState = CONNECTION_STATE_IDLE;
48+
NetworkConnectionState netConnectionState = CONNECTION_STATE_INIT;
5049

5150
};
5251

@@ -57,6 +56,7 @@ class ConnectionManager {
5756
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
5857
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
5958
#define NETWORK_CONNECTED WL_CONNECTED
59+
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED
6060
#endif
6161

6262
#ifdef ARDUINO_SAMD_MKRWIFI1010
@@ -65,6 +65,7 @@ class ConnectionManager {
6565
#define NETWORK_HARDWARE_ERROR WL_NO_MODULE
6666
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
6767
#define NETWORK_CONNECTED WL_CONNECTED
68+
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION
6869
#endif
6970

7071
#ifdef ARDUINO_SAMD_MKRGSM1400
@@ -76,12 +77,18 @@ class ConnectionManager {
7677
#endif
7778

7879
static int debugMessageLevel = ARDUINO_CLOUD_DEBUG_LEVEL;
79-
inline void debugMessage(char *_msg, uint8_t _debugLevel) {
80+
inline void debugMessage(char *_msg, uint8_t _debugLevel, bool _timestamp = true, bool _newline = true) {
8081
if (_debugLevel <= debugMessageLevel) {
8182
char prepend[20];
8283
sprintf(prepend, "\n[ %d ] ", millis());
83-
Serial.print(prepend);
84-
Serial.println(_msg);
84+
if(_timestamp)
85+
Serial.print(prepend);
86+
if(_newline){
87+
Serial.println(_msg);
88+
}else{
89+
Serial.print(_msg);
90+
}
91+
8592
}
8693
}
8794

Diff for: src/WiFiConnectionManager.h

+44-29
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ class WiFiConnectionManager : public ConnectionManager {
3131

3232
void changeConnectionState(NetworkConnectionState _newState);
3333

34-
const int CHECK_INTERVAL_IDLE = 100;
3534
const int CHECK_INTERVAL_INIT = 100;
3635
const int CHECK_INTERVAL_CONNECTING = 500;
37-
const int CHECK_INTERVAL_GETTIME = 1000;
36+
const int CHECK_INTERVAL_GETTIME = 100;
3837
const int CHECK_INTERVAL_CONNECTED = 10000;
3938
const int CHECK_INTERVAL_RETRYING = 5000;
4039
const int CHECK_INTERVAL_DISCONNECTED = 1000;
4140
const int CHECK_INTERVAL_ERROR = 500;
4241

42+
const int MAX_GETTIME_RETRY = 30;
43+
4344
const char *ssid, *pass;
4445
unsigned long lastConnectionTickTime, lastNetworkStep;
46+
unsigned long getTimeRetries;
47+
4548
WiFiClient wifiClient;
4649
int connectionTickTimeInterval;
4750
};
@@ -51,7 +54,8 @@ static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
5154
WiFiConnectionManager::WiFiConnectionManager(const char *ssid, const char *pass) :
5255
ssid(ssid), pass(pass),
5356
lastConnectionTickTime(millis()),
54-
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
57+
connectionTickTimeInterval(CHECK_INTERVAL_INIT),
58+
getTimeRetries(MAX_GETTIME_RETRY) {
5559
}
5660

5761
unsigned long WiFiConnectionManager::getTime() {
@@ -62,28 +66,39 @@ void WiFiConnectionManager::init() {
6266
}
6367

6468
void WiFiConnectionManager::changeConnectionState(NetworkConnectionState _newState) {
65-
netConnectionState = _newState;
66-
int newInterval = CHECK_INTERVAL_IDLE;
69+
char msgBuffer[120];
70+
int newInterval = CHECK_INTERVAL_INIT;
6771
switch (_newState) {
6872
case CONNECTION_STATE_INIT:
6973
newInterval = CHECK_INTERVAL_INIT;
7074
break;
7175
case CONNECTION_STATE_CONNECTING:
76+
*msgBuffer = 0;
77+
sprintf(msgBuffer, "Connecting to \"%s\"", ssid);
78+
debugMessage(msgBuffer, 2);
7279
newInterval = CHECK_INTERVAL_CONNECTING;
7380
break;
7481
case CONNECTION_STATE_GETTIME:
7582
newInterval = CHECK_INTERVAL_GETTIME;
83+
debugMessage("Acquiring Time from Network", 3);
7684
break;
7785
case CONNECTION_STATE_CONNECTED:
7886
newInterval = CHECK_INTERVAL_CONNECTED;
7987
break;
8088
case CONNECTION_STATE_DISCONNECTED:
89+
*msgBuffer = 0;
90+
sprintf(msgBuffer, "WiFi.status(): %d", WiFi.status());
91+
debugMessage(msgBuffer, 4);
92+
*msgBuffer = 0;
93+
sprintf(msgBuffer, "Connection to \"%s\" lost.", ssid);
94+
debugMessage(msgBuffer, 0);
95+
debugMessage("Attempting reconnection", 0);
8196
newInterval = CHECK_INTERVAL_DISCONNECTED;
82-
8397
break;
8498
}
8599
connectionTickTimeInterval = newInterval;
86100
lastConnectionTickTime = millis();
101+
netConnectionState = _newState;
87102
}
88103

89104
void WiFiConnectionManager::check() {
@@ -92,9 +107,6 @@ void WiFiConnectionManager::check() {
92107
int networkStatus = 0;
93108
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
94109
switch (netConnectionState) {
95-
case CONNECTION_STATE_IDLE:
96-
changeConnectionState(CONNECTION_STATE_INIT);
97-
break;
98110
case CONNECTION_STATE_INIT:
99111
networkStatus = WiFi.status();
100112
*msgBuffer = 0;
@@ -108,19 +120,21 @@ void WiFiConnectionManager::check() {
108120
return;
109121
}
110122
*msgBuffer = 0;
111-
sprintf(msgBuffer, "WiFi Firmware v. %s", WiFi.firmwareVersion());
123+
sprintf(msgBuffer, "Current WiFi Firmware: %s", WiFi.firmwareVersion());
112124
debugMessage(msgBuffer, 0);
125+
if(strcmp(WiFi.firmwareVersion(), WIFI_FIRMWARE_VERSION_REQUIRED) != 0){
126+
*msgBuffer = 0;
127+
sprintf(msgBuffer, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED);
128+
debugMessage(msgBuffer, 0);
129+
debugMessage("Please update to latest version for optimal performance.", 0);
130+
}
113131
changeConnectionState(CONNECTION_STATE_CONNECTING);
114132
break;
115133
case CONNECTION_STATE_CONNECTING:
116-
*msgBuffer = 0;
117-
sprintf(msgBuffer, "Connecting to \"%s\"", ssid);
118-
debugMessage(msgBuffer, 2);
119-
120134
networkStatus = WiFi.begin(ssid, pass);
121135
*msgBuffer = 0;
122136
sprintf(msgBuffer, "WiFi.status(): %d", networkStatus);
123-
debugMessage(msgBuffer, 2);
137+
debugMessage(msgBuffer, 4);
124138
if (networkStatus != NETWORK_CONNECTED) {
125139
*msgBuffer = 0;
126140
sprintf(msgBuffer, "Connection to \"%s\" failed", ssid);
@@ -136,46 +150,47 @@ void WiFiConnectionManager::check() {
136150
sprintf(msgBuffer, "Connected to \"%s\"", ssid);
137151
debugMessage(msgBuffer, 2);
138152
changeConnectionState(CONNECTION_STATE_GETTIME);
153+
getTimeRetries = MAX_GETTIME_RETRY;
139154
return;
140155
}
141156
break;
142157
case CONNECTION_STATE_GETTIME:
143-
debugMessage("Acquiring Time from Network", 3);
158+
144159
unsigned long networkTime;
145160
networkTime = WiFi.getTime();
146-
*msgBuffer = 0;
147-
sprintf(msgBuffer, "Network Time: %u", networkTime);
148-
debugMessage(msgBuffer, 3);
161+
162+
debugMessage(".", 3, false, false);
149163
if(networkTime > lastValidTimestamp){
150164
lastValidTimestamp = networkTime;
165+
*msgBuffer = 0;
166+
sprintf(msgBuffer, "Network Time: %u", networkTime);
167+
debugMessage(msgBuffer, 3);
151168
changeConnectionState(CONNECTION_STATE_CONNECTED);
152-
}
169+
} else if (WiFi.status() != WL_CONNECTED) {
170+
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
171+
} else if (!getTimeRetries--) {
172+
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
173+
}
153174
break;
154175
case CONNECTION_STATE_CONNECTED:
155176
// keep testing connection
156177
networkStatus = WiFi.status();
157178
*msgBuffer = 0;
158179
sprintf(msgBuffer, "WiFi.status(): %d", networkStatus);
159-
debugMessage(msgBuffer, 2);
180+
debugMessage(msgBuffer, 4);
160181
if (networkStatus != WL_CONNECTED) {
161182
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
162183
return;
163184
}
164185
*msgBuffer = 0;
165186
sprintf(msgBuffer, "Connected to \"%s\"", ssid);
166-
debugMessage(msgBuffer, 2);
187+
debugMessage(msgBuffer, 4);
167188
break;
168189
case CONNECTION_STATE_DISCONNECTED:
169190
//WiFi.disconnect();
170191
WiFi.end();
171192

172-
*msgBuffer = 0;
173-
sprintf(msgBuffer, "DISC | WiFi.status(): %d", WiFi.status());
174-
debugMessage(msgBuffer, 1);
175-
*msgBuffer = 0;
176-
sprintf(msgBuffer, "Connection to \"%s\" lost.", ssid);
177-
debugMessage(msgBuffer, 0);
178-
debugMessage("Attempting reconnection", 1);
193+
179194
changeConnectionState(CONNECTION_STATE_CONNECTING);
180195
//wifiClient.stop();
181196
break;

0 commit comments

Comments
 (0)