BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

How to "wipe" a device for a new user and new wifi provisioning

Hi Blynkers,

I’m having some trouble using the wifi provisioning with edgent.

First at all, I already readed the great post by @PeteKnight :slight_smile:

Thanks Pete for that great document.

My problem is that I have a lot of already designed hardware that doesn’t have space for adding buttons accesible for the device owners for “resetting” the wifi configuration. I have a lot of PCB boards already manufactured with all the components.

Usually, I assembly the device, and send the device to the owner for wifi provisioning without any issue.

The problem happens when for testing purposes, I make the provissioning of the device to my account, and after checking everything is ok, I delete the device on the Blynk Console, and send it to the owner.

If the owner try to make the provisioning, the device doesn’t provide any Hotspot for connecting trough wifi and complete the set up.

After a lot of trial and error, we discovered that the only way to allow the new owner to provission the device is using a second smartphone to provide a wifi AP with the exact SSID and pasword of my own wifi (the one the device was connected originally for testing).

When this supposed deleted device detects my old Wifi, then it starts providing its own Hotspot for make the new provissioning.

Does anybody knows how to really wipe an already provissioned device to leave it available for a new provissioning?

Before shipping can you clear all the device memory and reload the program? This is what I do if the reset button isn’t an option. I use different ESP32’s.

You need to clear the EEPROM of the device.
With an ESP8266 that’s easy - you just choose Erase Flash - All Flash Contents when you re-upload the sketch.

Unfortunately, that option is missing for the ESP32 in the Arduino IDE.

Suggesting the best solution is a little difficult, as you’ve not shared enough details about your hardware and what you’ve done with the Settings.h file.

Pete.

I’m sorry Pete.
My projects are done with ESP8266 NodeMCU Dev. Kit (it has a erase flash button), and I use the Wemos D1 Mini Pro too.

Yes, it seems that I will have o add that step to the process.

But I confirmed the following:

If you already provisioned a device with Bkynk Edgent , there is no way to move the device to another Wifi, unless the new Wifi share the same area of the old Wifi.

If the only solution for my devices using Wemos D1 Mini Pro will be adding a button, I will have to take to the trash a lot of hardware. Sad.

In your particular case, with no button added to the device, then yes -= that appears to be true.

Pete.

Hi All,

I think I found a workaround for this kind of situation. I’ll explain the process to find this solution:

I noticed that when the device is deleted from the Blynk Console the device detects it’s token as invalid one.

14:28:45.767 -> [141091] RUNNING => CONNECTING_CLOUD
14:28:45.767 -> [141103] Current time: Mon Mar  7 17:28:46 2022
14:28:45.767 -> [141103] Connecting to blynk.cloud:443
14:28:47.518 -> [142864] Invalid auth token
14:28:47.518 -> [142865] CONNECTING_CLOUD => WAIT_CONFIG
14:28:48.052 -> [143374] AP SSID: Blynk Control ATO-41DE2
14:28:48.052 -> [143375] AP IP:   192.168.4.1
14:28:48.052 -> [143375] AP URL:  blynk.setup

Then, I looked at the Edgent code to find where is that Invalid Token message handled.

It was at the configmode.h file at lines 412 to 416:

  if (Blynk.isTokenInvalid()) {
    config_set_last_error(BLYNK_PROV_ERR_TOKEN);
    BlynkState::set(MODE_WAIT_CONFIG);
  } else if (Blynk.connected()) {
    BlynkState::set(MODE_RUNNING);

Then, after the device is deleted on the Blynk console, the BlynkState changes to MODE_WAIT_CONFIG

The Blynk States are checked at the BlynkEdgent.h file at lines 96 to 109 on the run() function:

  void run() {
    app_loop();
    switch (BlynkState::get()) {
    case MODE_WAIT_CONFIG:       
    case MODE_CONFIGURING:       enterConfigMode();    break;
    case MODE_CONNECTING_NET:    enterConnectNet();    break;
    case MODE_CONNECTING_CLOUD:  enterConnectCloud();  break;
    case MODE_RUNNING:           runBlynkWithChecks(); break;
    case MODE_OTA_UPGRADE:       enterOTA();           break;
    case MODE_SWITCH_TO_STA:     enterSwitchToSTA();   break;
    case MODE_RESET_CONFIG:      enterResetConfig();   break;
    default:                     enterError();         break;
    }
  }

Oh surprise, on the state MODE_WAIT_CONFIG it do nothing.

Then, I decided to modify the configmode.h from line 412 to:

  if (Blynk.isTokenInvalid()) {
    config_set_last_error(BLYNK_PROV_ERR_TOKEN);
    BlynkState::set(MODE_RESET_CONFIG); // Cambia accion a Reset Config cuando no reconoce auth Token
//    BlynkState::set(MODE_WAIT_CONFIG); // Linea original.
  } else if (Blynk.connected()) {
    BlynkState::set(MODE_RUNNING);

    if (!configStore.getFlag(CONFIG_FLAG_VALID)) {
      configStore.last_error = BLYNK_PROV_ERR_NONE;
      configStore.setFlag(CONFIG_FLAG_VALID, true);
      config_save();
    }

With this change, If you delete the device on the Blynk Console, the device will do the same as if you pressed the reset button for 10 seconds!

At this point, I don’t know If I’m destroying something with this change, but I tested several times and it woks like a charme.

The next step, and very easy too, is to enable moving the device to another building with different WiFi when you don’t have the suggested Reset Button:

You need to modify the enterError() function on the ConfigMode.h file, lines 442 to 458. You need to change the restartMCU(); at line 457 for BlynkState::set(MODE_RESET_CONFIG);

void enterError() {
  BlynkState::set(MODE_ERROR);
  
  unsigned long timeoutMs = millis() + 10000;
  while (timeoutMs > millis() || g_buttonPressed)
  {
    delay(10);
    app_loop();
    if (!BlynkState::is(MODE_ERROR)) {
      return;
    }
  }
  DEBUG_PRINT("Restarting after error.");
  delay(10);
  BlynkState::set(MODE_RESET_CONFIG);
//  restartMCU(); 
}

Then, if the device is provissioned for a first time and in a restart doesn’t connect to Blynk after a 30 + 10 seconds time out, it will reset the wifi settings as the same of the 10 seconds button, and will wait for a new provisioning.

The above modification wasn’t fully tested yet, because with the first one was enough for solving my problem. I still don’t know if it will cause som trouble, If you want to try it, just be careful.

1 Like