-
Notifications
You must be signed in to change notification settings - Fork 82
Add GSM and Ethernet conn_manager + add fallback UDP based getTime() #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
db14c67
GSM: implement full state machine
facchinm 68d2c3c
GSM: add fallback getTime retrieve
facchinm 1324ac6
Merge branch 'master' into gsm_proper_connmanager
facchinm 19c688e
Create NTP util class to handle getTime() fallback
facchinm e0abeb8
Restructure GSM connection manager
facchinm 75ddfcc
Automatically handle getTime fallback in IoTCloud class
facchinm da7179b
Add EthernetConnectionManager
facchinm e01a981
[EthConnectionManager] Make "now" const
aentinger 4014095
- added link to NPT on Arduino Tutorial and WikiPedia
ubidefeo a243573
- refactor GSMConnectionManager
ubidefeo 096022a
- cleanup
ubidefeo 98efd08
- cleanup WiFi and GSM ConnectionManager
ubidefeo e905238
- whitespace cleanup
ubidefeo 8646505
- more cleanup and minor refactoring
ubidefeo 8eefcc0
- reworked sentence
ubidefeo e4e11e5
- extended example with GSM compatibility options
ubidefeo 20cbb75
Applying suggestions by A. Catozzi upon internal review
ubidefeo 7fc775b
- switch state cleanup in ArduinoIoTCloud
ubidefeo bff994e
Merge branch 'master' into gsm_proper_connmanager
ubidefeo ae7dae9
- applied per1234's suggestions on #31
ubidefeo f86f04f
- one more missing from per1234's list
ubidefeo 961a71c
- implemented some of the changes requested by @ilcato on PR #37
ubidefeo fb161bd
- `ArduinoIoTConnectionStatus` cleanup as per @ilcato 's suggestion
ubidefeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
#include "ConnectionManager.h" | ||
|
||
#include <Ethernet.h> | ||
#define BOARD_HAS_ETHERNET | ||
|
||
class EthConnectionManager : public ConnectionManager { | ||
public: | ||
EthConnectionManager(uint8_t *mac, int ss_pin); | ||
|
||
virtual unsigned long getTime(); | ||
virtual void init(); | ||
virtual void check(); | ||
virtual Client &getClient() { return ethClient; }; | ||
virtual UDP &getUDP() { return udp; }; | ||
|
||
private: | ||
|
||
void changeConnectionState(NetworkConnectionState _newState); | ||
const int CHECK_INTERVAL_IDLE = 100; | ||
const int CHECK_INTERVAL_INIT = 100; | ||
const int CHECK_INTERVAL_CONNECTING = 500; | ||
const int CHECK_INTERVAL_GETTIME = 100; | ||
const int CHECK_INTERVAL_CONNECTED = 10000; | ||
const int CHECK_INTERVAL_RETRYING = 5000; | ||
const int CHECK_INTERVAL_DISCONNECTED = 1000; | ||
const int CHECK_INTERVAL_ERROR = 500; | ||
|
||
unsigned long lastConnectionTickTime, lastNetworkStep; | ||
uint8_t* mac; | ||
int ss_pin; | ||
EthernetClient ethClient; | ||
EthernetUDP udp; | ||
int connectionTickTimeInterval; | ||
}; | ||
|
||
#if !defined(BOARD_HAS_WIFI) && !defined(BOARD_HAS_GSM) | ||
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; | ||
#endif | ||
|
||
EthConnectionManager::EthConnectionManager(uint8_t *mac, int ss_pin = -1) : | ||
mac(mac), | ||
ss_pin(ss_pin), | ||
lastConnectionTickTime(millis()), | ||
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) { | ||
} | ||
|
||
unsigned long EthConnectionManager::getTime() { | ||
//handled by fallback manager | ||
return lastValidTimestamp + 1; | ||
} | ||
|
||
void EthConnectionManager::init() { | ||
} | ||
|
||
void EthConnectionManager::changeConnectionState(NetworkConnectionState _newState) { | ||
netConnectionState = _newState; | ||
int newInterval = CHECK_INTERVAL_IDLE; | ||
switch (_newState) { | ||
case CONNECTION_STATE_INIT: | ||
newInterval = CHECK_INTERVAL_INIT; | ||
break; | ||
case CONNECTION_STATE_CONNECTING: | ||
newInterval = CHECK_INTERVAL_CONNECTING; | ||
break; | ||
case CONNECTION_STATE_GETTIME: | ||
newInterval = CHECK_INTERVAL_GETTIME; | ||
break; | ||
case CONNECTION_STATE_CONNECTED: | ||
newInterval = CHECK_INTERVAL_CONNECTED; | ||
break; | ||
case CONNECTION_STATE_DISCONNECTED: | ||
newInterval = CHECK_INTERVAL_DISCONNECTED; | ||
|
||
break; | ||
} | ||
connectionTickTimeInterval = newInterval; | ||
lastConnectionTickTime = millis(); | ||
} | ||
|
||
void EthConnectionManager::check() { | ||
char msgBuffer[120]; | ||
unsigned long const now = millis(); | ||
int networkStatus = 0; | ||
if (now - lastConnectionTickTime > connectionTickTimeInterval) { | ||
switch (netConnectionState) { | ||
case CONNECTION_STATE_INIT: | ||
if (ss_pin == -1) { | ||
networkStatus = Ethernet.begin(mac); | ||
} else { | ||
networkStatus = Ethernet.begin(mac, ss_pin); | ||
} | ||
networkStatus = Ethernet.hardwareStatus(); | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Eth hardware status(): %d", networkStatus); | ||
debugMessage(msgBuffer, 2); | ||
if (networkStatus == EthernetNoHardware) { | ||
debugMessage("No Ethernet chip connected", 0); | ||
// don't continue: | ||
changeConnectionState(CONNECTION_STATE_ERROR); | ||
lastConnectionTickTime = now; | ||
return; | ||
} | ||
networkStatus = Ethernet.linkStatus(); | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Eth link status(): %d", networkStatus); | ||
debugMessage(msgBuffer, 2); | ||
if (networkStatus == LinkOFF) { | ||
debugMessage("Failed to configure Ethernet via dhcp", 0); | ||
// don't continue: | ||
changeConnectionState(CONNECTION_STATE_ERROR); | ||
lastConnectionTickTime = now; | ||
return; | ||
} | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Ethernet shield recognized: ID", Ethernet.hardwareStatus()); | ||
debugMessage(msgBuffer, 0); | ||
changeConnectionState(CONNECTION_STATE_CONNECTING); | ||
break; | ||
case CONNECTION_STATE_CONNECTING: | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Connecting via dhcp"); | ||
debugMessage(msgBuffer, 2); | ||
if (ss_pin == -1) { | ||
networkStatus = Ethernet.begin(mac); | ||
} else { | ||
networkStatus = Ethernet.begin(mac, ss_pin); | ||
} | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Ethernet.status(): %d", networkStatus); | ||
debugMessage(msgBuffer, 2); | ||
if (networkStatus == 0) { | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Connection failed"); | ||
debugMessage(msgBuffer, 0); | ||
|
||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); | ||
debugMessage(msgBuffer, 2); | ||
//changeConnectionState(CONNECTION_STATE_CONNECTING); | ||
return; | ||
} else { | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Connected!"); | ||
debugMessage(msgBuffer, 2); | ||
changeConnectionState(CONNECTION_STATE_GETTIME); | ||
return; | ||
} | ||
break; | ||
case CONNECTION_STATE_GETTIME: | ||
debugMessage("Acquiring Time from Network", 3); | ||
unsigned long networkTime; | ||
networkTime = getTime(); | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Network Time: %u", networkTime); | ||
debugMessage(msgBuffer, 3); | ||
if(networkTime > lastValidTimestamp){ | ||
lastValidTimestamp = networkTime; | ||
changeConnectionState(CONNECTION_STATE_CONNECTED); | ||
} | ||
break; | ||
case CONNECTION_STATE_CONNECTED: | ||
// keep testing connection | ||
Ethernet.maintain(); | ||
networkStatus = Ethernet.linkStatus(); | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Eth link status(): %d", networkStatus); | ||
debugMessage(msgBuffer, 4); | ||
if (networkStatus != LinkON) { | ||
changeConnectionState(CONNECTION_STATE_DISCONNECTED); | ||
return; | ||
} | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Connected"); | ||
debugMessage(msgBuffer, 2); | ||
break; | ||
case CONNECTION_STATE_DISCONNECTED: | ||
*msgBuffer = 0; | ||
sprintf(msgBuffer, "Connection lost."); | ||
debugMessage(msgBuffer, 0); | ||
debugMessage("Attempting reconnection", 1); | ||
changeConnectionState(CONNECTION_STATE_CONNECTING); | ||
//wifiClient.stop(); | ||
break; | ||
} | ||
lastConnectionTickTime = now; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an exit condition from the CONNECTION_STATE_GETTIME state in a similar way of the WiFiConnectionManager.