Last active
October 13, 2015 17:56
-
-
Save indraastra/e31b6790e3e44caf7af2 to your computer and use it in GitHub Desktop.
Bring up and maintain wifi connection in MANUAL mode
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); | |
void setup() { | |
Serial.begin(9600); | |
delay(1000); | |
} | |
bool _connectingToCloud = false; | |
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("still alive!"); | |
last_printed = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment