espNOW + OTA toggle

I have a system with 3 esp32s, a server and 2 children. Children talk to the server & visa versa by way of espNOW. I'd like to be able to flash the child boards OTA. My understanding these cant be used concurrently, thats fine. I can pop it into OTA mode at the rare time I need to, then used the OTA finished call back to restart the board. In fact, I have that much implemented using ElegantOTA (async mode).

Where I'm stuck is resolving the situation in which I accidentally initiate OTA mode, and need to exit it& resume espNow coms without rebooting.

espNow starts by default, works fine--> stop espNOW to do an OTA--> works fine(browser page accessible), now when I try to get out of the OTA, I cant seem to get espNow running again, despite it being the exact same function that initializes it in the beginning. something with setting up the OTA mode is not getting set back, but I have no idea what??

here is the method that setups espNow, but wont re-set it up:
the couple of lines commented out were just things I was trying, none worked, the alternate disconnect broke the whole thing. This just reports the mac is sending to in serial & it succeeds.

        WiFi.mode(WIFI_MODE_STA);
        WiFi.disconnect();
        //esp_now_deinit();
        //WiFi.disconnect(true, true);
        memcpy(serverMac, MAC, sizeof(MAC[0])*6);
    
        if (esp_now_init() != ESP_OK)      {Serial.println("Error initializing ESP-NOW");     return;    }

        ESPNow.add_peer(serverMac);
        enSendData=true;
        if(enSendData) 
            Serial << F("data will be sent via espNOW to: ");
        for(uint8_t i=0; i<6;i++)
        {
            Serial.print(serverMac[i], HEX);
            Serial.print("\t");
        }
        Serial << endl;

and here is what I run to setup

OTA mode, there is more to it, but this is all the settings changes. I'll attach the full init if anyone wants to have a look at it.ota_setup.h (7.4 KB)

if (WiFi.status() == WL_CONNECTED) 
    {
        WiFi.disconnect(true, true);
        #if(otaDB>=1)
            printf("WiFi was previously connected - now disconnected\n");
        #endif
    }
    WiFi.mode(WIFI_STA);
    WiFi.persistent(false);
    WiFi.setAutoReconnect(true);
    WiFi.setSleep(false);
    
    if (WiFi.begin(ssid, password))
    {
        #if(otaDB>=1)
            printf("WiFi begin success.\n");
        #endif
    }

i've played around with trying to put a few of those settings back, only to be thoroughly frustrating.

Thanks for giving my issue your attention, much appreciated!

ps--I do have a work around for this in my back pocket, but I'm so close to having it working the right way, I'd rather not deploy it...i'm just not the kind of person to let it go, I'm sure some of you can relate

no one with any experience switching between espNOW & OTA ???

actually this would be the same for anyone switching between espNow & any type of wifi network, it wouldn't need to be specifically OTA related.

well after much tinkering, I finally got espNow to restart properly. Anyone in the same situation, heres what I did:

make and initial config variable to use in esp_wfi_init():

const wifi_init_config_t wifi_config=WIFI_INIT_CONFIG_DEFAULT();

the all caps portion is a macro to get this initial config structure. once you have that var defined, this is the order for a start or restart:

        esp_wifi_stop();
        esp_wifi_deinit();
        esp_wifi_init(&wifi_config);
        WiFi.mode(WIFI_MODE_STA);
        WiFi.disconnect();
        esp_wifi_start();

I add peers right after that. the disconnect() line prob isnt needed & on restart prints a serial message saying it failed to disconnect, which is of no consequence.

Hope this saves someone the 8 or so hours I spent working it out!