SoftAP rejects my wifi password always

I have successfully implemented a Wifi checking code that does not block my code without the need for thread enable. =)


void getcreds(){
    WiFiAccessPoint ap[5];
    int found = WiFi.getCredentials(ap, 5);
    for (int i = 0; i < found; i++) {
        Serial.println("wifi credentials: ");
        Serial.print(i);Serial.print(". ");
        Serial.println(ap[i].ssid);
        cred[i]=String(ap[i].ssid);
    }
}
void wificheck(){
    Serial.print("wificheck...");
    //Serial.println("wifi ON");
    WiFi.on();
    
    if (Particle.connected())
    {
        Particle.process();
        if(flagwifi==2) //this is for publishing backups asap
        {
            publishnow=0;    Serial.println(" force publish now");
            flagwifi=0;
        }
    }
    else 
    {  
        flagwifi=1;
        Serial.println("Scanning wifis...");
        WiFiAccessPoint aps[20];
        int found = WiFi.scan(aps, 20);         //list available wifis
        for (int i=0; i<found; i++) {                 //for each found, check if it is known and connect
            WiFiAccessPoint& ap = aps[i];       //no idea what this is
            Serial.print("SSID ");Serial.print(i);Serial.print(": ");
            Serial.println(ap.ssid);            //this is taken from https://docs.particle.io/reference/device-os/firmware/photon/#scan-)

            for(int j=0;j<5;j++){
                if(cred[i].equals(String(ap.ssid)))             //from https://community.particle.io/t/how-to-compare-string-with-char/3929
                {
                    Serial.println (" ^ match!! connecting..."); 
                    j=5; 
                    i=found;
                    WiFi.connect();
                    Particle.connect();
                    publishnow=0; flagwifi=2;
                }
            }
        }
    }//end if !wifi.ready
    
    if (WiFi.ready()){ 
        Particle.connect();//Serial.println("particle connect");
        if (Particle.connected()) {Particle.process();Serial.println("System online");flagwifi=0;}
    }
    else
    {
        WiFi.off();
    }
    
}//end wificheck

thanks @ScruffR

The next step is to optimize the backup section so that it consumes less memory and makes the use of softAP more reliable.

This is what I have now:

#define ARRAYSIZE 200
String backup[ARRAYSIZE];
String payload;


void onlinecheck()
{
    if (Particle.connected()) {             //if internet, send backups every delay until finished
        Particle.process();
        Serial.println("INTERNET OK!!");
        digitalWrite(ledred,LOW);
        digitalWrite(ledgreen,HIGH); //green

        counterstatus++;
        if (counterstatus>9999){counterstatus=0;}
        unsigned int intcounterstatus=counterstatus;
        counterstatus=intcounterstatus;

        if(backupcounter>0){
            for(int o=0;o<ARRAYSIZE;o++){ 
                if(backup[o] != "x"){      //previously the entire array was set to "x"
                    delay(200);
                    String id = System.deviceID();
                    Particle.publish(String(id.c_str()), backup[o] , PRIVATE);
    
                    Serial.println("");Serial.print("backup: ");Serial.print(o);Serial.print(" sent!  Remaining: "); Serial.print(backupcounter-1);  //sorry for bad style, will fix it later
                    freemem = System.freeMemory();
                    Serial.print("  free memory: ");  //checking ram
                    Serial.println(freemem);
                    Serial.println(backup[o]);
                    backup[o]="x";
                    Particle.process();
                    backupcounter--;
                    delay(200);  //is it too fast to send backups every 400ms?
                }
            }
        }
    }

    else {                   //if no internet store backup 
    
     
        Serial.println("");
        Serial.println("NO INTERNET!!");

        updatecurrentTime(); //get time in epoch format
        
        counterstatus++;
        if (counterstatus>9999){counterstatus=0;}
        unsigned int intcounterstatus=counterstatus;
        counterstatus=intcounterstatus+0.1;
                     
        for(int o=0;o<=ARRAYSIZE-1;o++){
            
            if(backup[o]=="x")
            {
                String id = System.deviceID();
                payload = String::format(  "{\"Timestamp_Device\":\"" + String(currentTime) + "\",\"device_id\":\"" + String(id.c_str()) +  "\",\"temp\":\"" + String(flowtemp1) + "\",\"flowshort\":\"" + String(flowtotal) +  "\",\"flowacum\":\"" + String(flowacum) + "\",\"vbat\":\"" + String(vbat) + "\",\"counterstatus\":\"" + String(counterstatus)+"\"}");
                backup[o] = payload;
                backup[o+1]="x";
                Serial.print("backup: ");Serial.print(o);Serial.print(" saved:  "); 
                Serial.print(backup[o]); Serial.println(" ");
                
                if(o==ARRAYSIZE-3){                 //if array full, delete it. this should only replace first entry and so on
                    cleanbackup();   //this sets the entire array to "x"
                    backupcounter=0;
                }
                else { o=ARRAYSIZE+1; }
                backupcounter++;
                
                freemem = System.freeMemory();
                Serial.print("free memory: ");
                Serial.println(freemem);
            }
        }
    }//end else
}//end onlinecheck

So I want to implement your recommendations from: Photon working both online and offline but the coding level is a bit too advanced for me, so I will try it but I will most probably get stuck. Any recommendations on how to proceed? The idea is to have as many backups available as possible without risking the stability of the system.
thanks

1 Like