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.