Photon working both online and offline

photon
Tags: #<Tag:0x00007fe21ffbc8c8>

#1

Sometimes my photon loses wifi connection or the internet ISP fails. I need my photon to keep working.

When internet fails I want it to store the data it is collecting.
When internet returns I want it to publish that data.

My problem so far is that when I turn the wifi router off (simulating internet failure), the photon blinks green but does not continue working… for example no Serial.print(“no internet”);

I tried MANUAL and SEMI_AUTOMATIC modes and neither work.

void onlinecheck()
{
Particle.connect();

    if (Particle.connected()==false)                    //if no internet store backup 
    {   
        Serial.println("NO INTERNET!!!!!");     //  <<<<< this is never printed, why????
        for(int o=0;o<ARRAYSIZE;o++){
            if(backup[o]=="x")
            {
                backup[o] = "content of string to be backed up";
                Serial.print("backup: ");Serial.print(o);Serial.println(" saved!");
                o=100;
            }
        }
    }
    
    if (Particle.connected()) {                         //if internet, send backups every delay until finished
    Serial.println("");
    Serial.println("INTERNET OK!!");
        for(int o=ARRAYSIZE;o<0;o--){
            if(backup[o]!="x"){
                Particle.publish("measure", backup[o], PRIVATE);
                backup[o]="x";
                delay(1000);
                Particle.process();
            }
        }
    }
}

thanks


#2

You should try SYSTEM_THREAD(ENABLED)


#3

I now added SYSTEM_THREAD(ENABLED)

same behaviour… green flashing and stopped running.

I removed my first line of the script: Particle.connect(); and now it stays solid yellow but wont connect back when the wifi returns and it still does not print “no internet”.

What happens if the photon has no wifi and it hits the line Particle.process(); ?


#4

I changed a few things here and there and it seems to work better now:

void onlinecheck()//checa que pueda subor datos a internet, si no entonces guarda en ram cada medicion hasta que pueda subir los datos pendientes
{
//Particle.connect();

    if (Particle.connected()) {                         //if internet, send backups every delay until finished
    Serial.println("");
    Serial.println("INTERNET OK!!");
    if(backupcounter>0){
        for(int o=ARRAYSIZE;o>=0;o--){
            if(backup[o] != "x"){
                    //backup[o] = ("blablabla")
                   // Particle.publish("measure", backup[o], PRIVATE);
                    Serial.println("");Serial.print("backup: ");Serial.print(o);Serial.println(" sent!");
                    Serial.println(backup[o]);
                    backup[o]="x";
                    delay(1000);
                    Particle.process();
                    backupcounter--;
                }
            }
        }
    }
    
    else {                   //if no internet store backup    
        Serial.println("NO INTERNET!!!!!");
        for(int o=0;o<ARRAYSIZE;o++){
            if(backup[o]=="x")
            {
                backup[o] = ("blablabla");
                Serial.print("backup: ");Serial.print(o);Serial.println(" saved!");
                Serial.print(backup[o]);
                o=100;
                backupcounter++;
            }
        }
    }
}//end onlinecheck

when I say “better” it is because sometimes ti flashes red SOS for a few seconds and then back to breathing cyan…

Could it be that it is running out of RAM? What commands are there to store these Strings into a non volatile memory or how can I know how much RAM i have left WHILE running the code?
thanks


#5

One thing, since we can’t see how frequently your original code called onlinecheck(), you should not call Particle.connect() repeatedly.
Once you call it you should use waitFor(), waitUntil() or do other stuff till a connection is successfully established but not call Particle.connect() again till any ongoing attempts to connect have completed (successfully or not).

When you talk about SOS flashes it’s always important to also state the number slow blinks between two SOS signals. This code will give you a hint about the prossible cause.

System.freeMemory()
But this does not tell you how fragmented your heap may have become.

What datatype is backup?
But using lots of String objects is a good candidate for causing heap fragmentation.