Photon constantly disconnects from cloud

I am having a problem with the photon, where the device constantly disconnects and reconnects from the cloud every few minutes. I am trying to read values from the serial port (rx,tx pins) and pass those back through a function to a server i wrote on the web. Here is my firmware:

String output;

void setup() {
    Serial1.begin(9600);
    Serial1.write("#Z,1\r");
    Serial1.flush();
    Particle.function("startConnection", start);
    Particle.function("getTemp", getTemp);
    Particle.variable("output", output);
}

void loop() {

}

int start(String extra){
    Serial1.write("#Z,1\r");
    Serial1.flush();
    return 1;
}

int getTemp(String extra){
    Serial1.write("#G\r");
    Serial1.flush();
    String content = "";
    content = Serial1.readString();
    if(getValue(content,'G',1)!="" && getValue(content,'G',1)!=" " && getValue(content,'.',1)!=""){
        output=getValue(content,'G',1);
        Particle.publish("Input",getValue(content,'G',1));
    }else{
        return getTemp("");
    }
    return getValue(content,'G',1).toInt();
}

void send(){
    Serial1.write("#G\r");
    Serial1.flush();
    String content = "";
    content = Serial1.readString();
    Serial1.flush();
    output=getValue(content,'G',1);
    Particle.publish("Input",getValue(content,'G',1));
}

void read(){
    String content = "";
    content = Serial1.readString();
    Particle.publish("Input",content);
}

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

I call getTemp from the cloud every 10 seconds. Any ideas?

Hi Peter,

My photons disconnect and connect to the cloud every now and then (depending on my router perhaps?), but not every few minutes.

It is risky business (hence bad practice) to do a publish in a cloud function, since (I am not sure here) the threads running the function and the publish may have esoteric issues. I also do not know if Serial1 runs well in a cloud function.
One way out of this is to set a global variable in the function (used as a flag here) and read that variable in loop(). Once detected set in loop(), you execute the serial read, publish, then reset it and life goes on.

You also may want to add this to the top of your firmware:

SYSTEM_THREAD(ENABLED);

docs: https://docs.particle.io/reference/device-os/firmware/photon/#system-thread

Hope it helps!
Gustavo.

While it’s true that Particle.publish()ing from a Particle.function() callback isn’t best practice it shouldn’t be an issue with the threads as both calls are running on the application thread.
Serial1 should also not be an issue for the same reason.

1 Like

Hey Peter, so in line with what ScruffR said in this post above, forget what I wrote in post 2, please!

1 Like

Update, I tried uploading the standard blink firmware onto the photon and it still experiences the same disconnect issues. Any ideas?

It’s difficult to advise with that external device (unknown to us) potentially being part of the problem.

If this is really the vanilla blink code, can you post a video of these incidents?
It might be an issue with your power supply or your WiFi AP.

However, Gustavos’s advice from above would be worth a try