As many of you know, the Spark people generously gave us two Spark Core devices to connect to our graphing calculators. It's been fun getting to know the device, but as it's still in its early days, there are plenty of confusing bits and incomplete documentation to work around. This topic is for documenting everything unexpected that we (or you!)
Setting WiFi Credentials and Connecting in Code
If you want your code to be able to set WiFi credentials in code, then connect to the network, there are a few gotchas that you need to be aware of:
For your edification, the following code will set credentials and then connect to WiFi.
Code:
Setting WiFi Credentials and Connecting in Code
If you want your code to be able to set WiFi credentials in code, then connect to the network, there are a few gotchas that you need to be aware of:
- If you are using SEMI_AUTOMATIC mode, you must call WiFi.on() before you do things like setCredentials(), clearCredentials(), and so on.
- If you get a "hard fault" error code from the RGB LED (that is, SOS in Morse code, one single flash, then SOS in Morse code again), you probably didn't call WiFi.on() before doing something like clearCredentials(). This is barely documented in existing documentation.
- If setCredentials() appears to take fewer that ~8 seconds, it didn't work. Debugging LEDs and Serial.print statements help a lot with this.
For your edification, the following code will set credentials and then connect to WiFi.
Code:
// Spark Core WiFi Credentials + Connect Demo
// By Christopher Mitchell, 2014
// No rights reserved for this code; WTFPL applies
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup() {
Serial.begin(9600);
// Press any key over a serial connection to start connecting
while (!Serial.available()) { };
WiFi.on();
Serial.println("Setting credentials.");
WiFi.setCredentials("MySSID", "MyWiFiPassword", WPA2);
Serial.println("Credentials set.");
}
void loop() {
if (!WiFi.ready()) {
if (WiFi.connecting())
return; // Wait for it to connect
if (!WiFi.hasCredentials()) // Wait for credentials
return;
Serial.println("Calling connect.");
WiFi.connect();
return; // We'll get back here eventually
}
if (WiFi.ready()) {
Serial.println("WiFi is ready.");
RGB.control(true);
RGB.color(0,255,0);
}
}