Created
October 19, 2015 22:16
-
-
Save indraastra/955f40ea35b0348e9032 to your computer and use it in GitHub Desktop.
Listening mode w/ hasCredentials blocking
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); | |
bool _connectingToCloud = false; | |
void setup() { | |
Serial.begin(9600); | |
delay(2000); | |
} | |
void manageConnection() { | |
if (!Particle.connected()) { | |
if (!WiFi.ready()) { | |
// Only connect to the network if we have credentials and aren't already | |
// in listening or connecting modes. | |
// Edit: WiFi.hasCredentials() currently has issues. | |
// if (!WiFi.listening() && !WiFi.connecting() && WiFi.hasCredentials()) { | |
if (!WiFi.listening() && !WiFi.connecting()) { | |
Serial.println("> Connecting to network!"); | |
WiFi.connect(WIFI_CONNECT_SKIP_LISTEN); | |
} | |
} else { | |
if (!_connectingToCloud) { | |
Serial.println("> Connecting to cloud!"); | |
// NOTE: This only needs to be called once and after that, the system | |
// thread should maintain the connection automatically until | |
// Particle.disconnect() is called. Still, it shouldn't hurt to call | |
// this every time we get disconnected. | |
// source: https://github.com/spark/firmware/issues/663#issuecomment-147549451 | |
Particle.connect(); | |
_connectingToCloud = true; | |
} | |
} | |
} else { | |
if (_connectingToCloud) { | |
Serial.println("> Connected to cloud!"); | |
} | |
_connectingToCloud = false; | |
Particle.process(); | |
} | |
} | |
uint32_t last_printed = 0; | |
void loop() { | |
manageConnection(); | |
// Application code. | |
if (millis() - last_printed > 1000) { | |
Serial.println(WiFi.hasCredentials()); | |
//Serial.println("> Still running!"); | |
last_printed = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment