I have a wifi connection problem

I’m doing it wrong somewhere but I do not understand. I ask for your help.
From time to time (sometimes 20 minutes, sometimes 3 hours, etc.) when I write the code as below, the wifi connection is disconnected and reconnected. But this is a big problem for me because blynk always gives a warning.

.
.
.
void loop(void) {
    Particle.process();
    Blynk.run();
    
    if (!Particle.connected())
    {
	//// if particle not connected else restart wifi and restrat photon
       if((millis()-sistem_last) > sistem_bekle){
        con_test_count--;
            if(con_test_count > 0){
                WiFi.off();
                WiFi.on();
                sistem_last = millis();
                String mesaj = Time.format(Time.now(), "%H:%M") + " - Wifi Yeniden Başlatıldı";
                terminal.println(F(mesaj));
                terminal.flush();
            }
            else
            {
            System.reset();
            }
    }
    else
    {
    if((millis()-blink_last) > blink_bekle){

        TX_LED.on();
        
        if(status){
        TX_LED.setColor(BLYNK_RED);
        }
        else
        {
        TX_LED.setColor(BLYNK_GREEN);
        }
        status=!status;
		
        // BATTERY FAILURE /////////////////
        if((AKU_DEGERI<23 || AKU_DEGERI>29))
            AKU_HATA = true;
            else
            AKU_HATA = false;
            
        if((AKU_DEGERI>23 && AKU_DEGERI<29))
            AKU_NORMAL = true;
            
        if(AKU_HATA == true && AKU_NORMAL == true){
            String mesaj = Time.format(Time.now(), "%H:%M") + " - " + "SERVICE BATTERY FAULT";
            Blynk.virtualWrite(V7," System Fault ");
            Particle.publish("pushbullet", mesaj, 60, PRIVATE);
            Blynk.notify(mesaj);
            terminal.println(F(mesaj));
            terminal.flush();
            AKU_HATA = false;
            AKU_NORMAL = false;
        }
        /////////////////////////////////////////
		
        // 74165 ERROR SENDING /////////////////
		// pinValues1 AND pinValues2 values read in Timer action
		
        if(pinValues1 != oldPinValues1) //74165 değerlerini eski değerle karşılaştır
            {
                String mesaj = "";
                //74165(1)
                Blynk.setProperty(V7, "color", BLYNK_RED);
                for(int i = 0; i < DATA_WIDTH; i++)
                {
                    switch (i)
                    {
                        case 0:
                            if(!((pinValues1 >> i) & 1)){
                                STARBOARD_RUN = TRUE;
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "STARBOARD GEN RUN";
                                //Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                            else
                            {
                                STARBOARD_RUN = FALSE;
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "STARBOARD GEN STOP";
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 1:
                            if(!((pinValues1 >> i) & 1)){
                            PORT_RUN = TRUE;
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "PORT GEN RUN";
                                //Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                            else
                            {
                            PORT_RUN = FALSE;
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "PORT GEN STOP";
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 2:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "STARBOARD GEN FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 3:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "PORT GEN FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 4:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "SEWAGE TANK FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 5:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "NO SHORE AC";
                                //Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                            else
                            {
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "SHORE AC OK";
                                //Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 6:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "PORT ENGINE BATTERY FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 7:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "STARBOARD ENGINE BATTERY FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
            
                        case 15:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "ENGINE ROOM INTERFACE FAILURE";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                    }
                }
            
            terminal.flush();
            mesaj = "";
            oldPinValues1 = pinValues1;
			
            if(pinValues1 == 65535 && pinValues2 == 65535){
            Blynk.virtualWrite(V7," System NORMAL ");
            Blynk.setProperty(V7, "color", BLYNK_GREEN);
            }
        }
        
        if(pinValues2 != oldPinValues2) //74165 değerlerini eski değerle karşılaştır
            {
                String mesaj = "";
                Blynk.setProperty(V7, "color", BLYNK_RED);
                //74165(2)
                for(int i = 0; i < DATA_WIDTH; i++)
                {
                    switch (i)
                    {
                        case 0:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "BILGE 5 WATER ALARM";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 1:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "BILGE 4 WATER ALARM";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 2:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "BILGE 3 WATER ALARM";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 3:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "BILGE 2 WATER ALARM";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 4:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "BILGE 1 WATER ALARM";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 5:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "FIRE ALARM";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 6:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "RADIO BATTERY FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 7:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "EMERGENCY BATTERY FAULT";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 8:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "D6";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 9:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "D7";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                        case 15:
                            if(!((pinValues1 >> i) & 1)){
                                mesaj = Time.format(Time.now(), "%H:%M") + " - " + "BRIDGE INTERFACE FAILURE";
                                Blynk.virtualWrite(V7," System Fault ");
                                Particle.publish("pushbullet", mesaj, 60, PRIVATE);
                                Blynk.notify(mesaj);
                                terminal.println(F(mesaj));
                            }
                        break;
                    }
                }
            terminal.flush();
            mesaj = "";
            oldPinValues2 = pinValues2;
			
            if(pinValues1 == 65535 && pinValues2 == 65535){
            Blynk.virtualWrite(V7," System NORMAL ");
            Blynk.setProperty(V7, "color", BLYNK_GREEN);
            }
        }

        //delay(POLL_DELAY_MSEC); zaten 500ms tekrar ediyor
        ////////////////////////////////////////
        if(millis() > 864000000) //10 day after restart photon
        System.reset();
        
        sistem_last = millis();
        blink_last = millis();
        con_test_count = 3;
        
            if(!wifidurum){
                wifidurum = true;
                String mesaj = Time.format(Time.now(), "%H:%M") + " - " + "Wifi yeniden bağlandı";
                terminal.println(F(mesaj));
                terminal.flush();
                
            }
        }
    }
    dongu2 = millis()-dongu;
    dongu = millis();
}

Have you tried enabling system threading to see if it corrects your issue?

This might not be your actual issue, but if you use SYSTEM_THREAD(ENABLED) and possibly SYSTEM_MODE(SEMI_AUTOMATIC) there is no immediate need to restart the connection, since the device should take care of that normally - only after an extended periode if not reconnecting you may try that.
Also make sure there is only one set of WiFi creds stored to speed up auto-reconnect.

Also (over)use of dynamic String variables can cause reconnection issues. Either use a pre-reserved, global String for things like your mesaj or a char[] to prevent heap fragmentation.

And as a general tip for coding style, try to pull these copy-paste code blocks with just minor changes in parameters together into only a hand full of “considerably different” sets and use parameter arrays.

Thank you for the answers. I will try them and come back.

1 Like

Unfortunately the problem continues.

Try to comment out all the Blynk stuff, to see if it’s a pure Particle thing or in cinnection with Blynk.
I’ve had issues with Blynk when the app got pushed into the background or my mobile device switched off.

Also if you have some Blynk widgets that pull data from the Photon, try to decrease the refresh rate.