Skip to content

Instantly share code, notes, and snippets.

@indraastra
Created October 19, 2015 22:16
Show Gist options
  • Save indraastra/955f40ea35b0348e9032 to your computer and use it in GitHub Desktop.
Save indraastra/955f40ea35b0348e9032 to your computer and use it in GitHub Desktop.
Listening mode w/ hasCredentials blocking
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