Can't reset WiFi credentials from firmware, flashes green

My application has the Spark Core in Deep Sleep most of the time, waking up periodically to check a sensor. I use SYSTEM_MODE(MANUAL) to only connect to WiFi/cloud when a sensor event is detected, so the Spark Core is typically only on for milliseconds at a time.

To enable my users to reset their WiFi credentials at home, I use a method where I check for two pins to be shorted in setup(). This is because the normal way to clear WiFi credentials (holding MODE button for 10s) can’t be used here since holding MODE at boot will trigger a factory reset and the Core wake period is so small.

I reset the WiFi credentials with this code:

void setup() {

    pinMode(wifiResetPinRead,INPUT);
    pinMode(wifiResetPinWrite,OUTPUT);
    digitalWrite(wifiResetPinWrite,HIGH);
    wifiResetReading = digitalRead(wifiResetPinRead);
    if(wifiResetReading > 0)
    {
        wifiResetInProgress = HIGH;
        WiFi.on();
        if (!WiFi.clearCredentials()){
            ledDebugCode();
        }
        WiFi.listen();
    }
    else
    {
        /* Normal boot */
        ...
    }
}

void loop() {
    if(wifiResetInProgress == LOW)
    {
        /* Normal application */
        ...
    }
}

With this code, shorting the two pins on boot causes the Core to go into the flashing blue mode. I don’t see the LED debug code so it seems that WiFi.clearCredentials() returns true. I then connect over serial port or using our iOS app with SmartConfig integration and send new WiFi credentials. After the core resets, it just constantly flashes green - can’t connect to WiFi.

This happens when we are resetting credentials in the field with devices that have been connected to another network. If we flash the device in the field and then set new credentials, it connects normally.

Any tips would be appreciated!

Thanks,
Andrew

@andrewa, one quick suggestion is to simply set the input pin to with pinMode(wifiResetPinRead, INPUT_PULLUP) and short the pin to ground when you want to reset the credentials. Also, instead of if/else, simply check for the exception otherwise boot as normal and adjust he code to wait for the credentials to be successfully entered before continuing:

    if (digitalRead(wifiResetPinRead) == LOW) {
        WiFi.on();
        if (!WiFi.clearCredentials()){
            ledDebugCode();
        }
        WiFi.listen();
        while (!WiFi.hasCredentials())
           Spark.process();
    }

... normal boot...

:slight_smile:

1 Like