Created
October 8, 2015 21:15
-
-
Save indraastra/a27f7508229ca4066e21 to your computer and use it in GitHub Desktop.
WiFi Status Monitor
This file contains 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
SYSTEM_MODE(MANUAL); | |
SYSTEM_THREAD(ENABLED); | |
#define UPDATE_IF_CHANGED(var, new_value, desc) {\ | |
if ((new_value) != (var)) {\ | |
(var) = (new_value);\ | |
Serial.print((desc));\ | |
Serial.print(": ");\ | |
Serial.println((var));\ | |
}\ | |
} | |
typedef struct { | |
bool cloud_connecting; | |
bool cloud_connected; | |
bool wifi_ready; | |
bool wifi_listening; | |
bool wifi_connecting; | |
bool wifi_has_credentials; | |
} WifiState; | |
WifiState state = {0, 0, 0, 0, 0, 0}; | |
void setup() { | |
Serial.begin(9600); | |
delay(1000); | |
updateState(); | |
} | |
bool _cloudConnecting = false; | |
bool cloudConnecting() { | |
return _cloudConnecting; | |
} | |
void updateState() { | |
if (!_cloudConnecting) UPDATE_IF_CHANGED(state.wifi_has_credentials, WiFi.hasCredentials(), "Wifi hasCredentials") | |
UPDATE_IF_CHANGED(state.wifi_ready, WiFi.ready(), "Wifi ready") | |
UPDATE_IF_CHANGED(state.wifi_listening, WiFi.listening(), "Wifi listening") | |
UPDATE_IF_CHANGED(state.wifi_connecting, WiFi.connecting(), "Wifi connecting") | |
UPDATE_IF_CHANGED(state.cloud_connecting, cloudConnecting(), "Cloud connecting") | |
UPDATE_IF_CHANGED(state.cloud_connected, Particle.connected(), "Cloud connected") | |
} | |
void loop() { | |
updateState(); | |
if (!Particle.connected()) { | |
if (!WiFi.ready()) { | |
if (!WiFi.listening() && !WiFi.connecting() && WiFi.hasCredentials()) { | |
Serial.println("Connecting to network!"); | |
WiFi.connect(WIFI_CONNECT_SKIP_LISTEN); | |
} | |
} else { | |
if (!_cloudConnecting) { | |
Serial.println("Connecting to cloud!"); | |
Particle.connect(); | |
_cloudConnecting = true; | |
} | |
} | |
} else { | |
if (_cloudConnecting) { | |
Serial.println("Connected to cloud!"); | |
} | |
_cloudConnecting = false; | |
Particle.process(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment