Photon going offline, code fixes?

Hello, all …

I’ve a Photon controlling my garage door, but it regularly goes offline - like now, resulting in me typing this from my car outside of said garage. I’m thinking it could be either:

  1. My WIFI going offline for a bit before returning - is there a way to make my Photon handle this better if this is the case?

  2. The code on the Photon controlling the garage door - I have Particle.process() in the loop function, but perhaps there’s more I need to do?

Any help would be appreciated. The code running on the Photon is very, very simple, and follows below.

Regards,

John


// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

void setup() { 
    Serial.begin(9600); 
    pinMode(D0, OUTPUT);
    pinMode(D1, OUTPUT);
    pinMode(D3, OUTPUT);
    
    Particle.function("trigger", trigger);
}

int trigger(String command) {
    digitalWrite(D0, LOW);
    delay(100);
    digitalWrite(D0, HIGH);
    delay(100);
    digitalWrite(D0, LOW);
    return 1;
}

int trigger_door(String command) {
    digitalWrite(D1, LOW);
    delay(100);
    digitalWrite(D1, HIGH);
    delay(100);
    digitalWrite(D1, LOW);
    return 1;
}

void loop() {
    Particle.process();
}

Might not have anything to do with the issues your facing, but I’d start with removing unused code. You set three pins as output and use only one. Then you’ve got a function you never call, not to mention that entire library that doesn’t seem to get used.

1 Like

I’d drop the unused library to start.
@Moors7 - beat me to it!

Personally, I’d also just set a flag in the Particle.function, then inside the loop() set the pins with an if statement that checks the flag.

With Particle.process being called each time the loop runs, I think you could drop that command.

2 Likes

Thanks for the tips :slight_smile: Before making optimizations, though, i’d rather start by trying to understand why said Photon sometimes is inaccessible. This happens once / twice per month perhaps, and it usually recovers by itself - read, responds to the trigger - after an hour or so (like this time), though very occasionally it will require that I power-cycle the Photon before it recovers. Possible reasons I can think of:

  1. My WIFI occasionally flakes out and the Photon doesn’t always reconnect, or takes a long while, when the WIFI returns.

  2. My ISP has issues. However, if this were the case, when my Photon loses the connection, other devices I have would as well, and they don’t. For instance, when my Photon becomes inaccessible, the smartphone app I created is still able to, through the same WIFI router, login to the Particle cloud, enumerate my devices and find this particular Photon.

  3. The Photon occasionally loses the connection to the Particle cloud and doesn’t always create it again. This is why, the last time this happened about a month ago, I added the Particle.process() call in the loop function.

  4. The Particle cloud sometimes isn’t accessible for other reasons. However, i’m on the mailing list for outages / maintenance / etc. and instances when my Photon does this don’t correlate with outage / maintenance emails I receive.

So, i’m wondering if there is a way to make the Photon reconnect to the WIFI or Particle cloud when it determines that said connection is gone. My understanding was that this is automatically handled by the firmware, but if there are ways I can improve this, i’d like to explore them first.

Regards,

John

What is the RGB LED doing in these cases?
What device OS versino are you running?

Particle.process() is not required in this case as it’s called between iterations of loop() anyhow.
You could try SYSTEM_THREAD(ENABLED) and regularly check Particle.connected() to detect a prolonged connection loss and try to recover by pulling down the connection WiFi.disconnect() or WiFi.off() and reestablish it yourself WiFi.on(), WiFi.connect(), Particle.connect(), …

1 Like

ScruffR,

The Photon is running 0.7.0.

I could not see the RGB LED activity when this happened yesterday, as I was essentially locked out of my house, having forgotten my house key (which i’ve taken measures to insure won’t happen again). The last time this happened - about a month ago I think - the LED was breathing green (cloud connection lost, I believe). I power cycled the Photon and it breathed cyan, but after a minute or so, it started breathing green again. That’s when I added the Particle.process() call inside the loop function, which seemed to fix the issue until yesterday.

I quite like your idea of programmatically checking for connection loss and bouncing the WIFI and reconnecting to the cloud if detected. Many thanks! I’m going to try that :slight_smile: I could publish an event to the cloud upon reconnection so i’ll have a log of these things, which might help to identify the cause.

Regards,

John

P.S. I originally had a Spark Core doing this and was experiencing similar issues. After reading about the WIFI issues that the original Core had, I replaced it with the Photon a year or so ago, but this issue persists.

I have a similar issue; although not as critical. I am simply monitoring temperature and humidity in my office while I am away from the office. The photon will go offline aperiodically and then come back online after a time. At this point I have not timed the on and off times. Some of the time when it appears offline, the diagnostic program at the console will run and indicate all is well. Any diagnostic process suggestions are appreciated. I probably won’t be able to work this until I return to my office next week.
Thanks,
Stan