Particle Photon hangs after http api-request to yahoo weather [Error found]

Hi everyone,

I started working on a small weather display recently using a BMP085 sensor, a ILI9340 Display and the Particle Photon connected to my local wireless network.
Everything was working really good, but then I added a request to the weather-api from yahoo to get some more information for my display:

Current implementation:

void GetDataYahoo(int mode){

    //Serial.println("GetDataYahoo");
    TCPClient client;

    if (client.connect("query.yahooapis.com", 80))
    {

    
        client.println("GET /v1/public/yql?q=select%20atmosphere%20from%20weather.forecast%20where%20woeid%20%3D%2012835849%20and%20u%20%3D%20'c'&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys HTTP/1.0");
        client.println("HOST:query.yahooapis.com\n\n");
      

    String receivedMessage = "";
    int timeout = 1000;
    while ((!client.available()) && (timeout-- > 0))
      delay(1);
    if (timeout <= 0)
      return ;
    while (client.available())
    {
      char c = client.read();
      receivedMessage += c;
    }
    client.stop();
    client.flush();

    //Serial.println(receivedMessage);
    ParseYahooData(mode, receivedMessage);

}

The requests are working good for minutes, sometimes hours but then I only get a flashing cyan led and the Photon never gets back to working again.

After some research I added the following:

if (!Particle.connected()) {
    while(!Particle.connected())  {
      Serial.println("Particle.connecte() in Loop");
      Particle.process();
    }
}

This is called before the Photon hangs, but is not working. It only prints the serial message once and then hangs. If i comment out the yahoo-request the board runs fine with the same firmware. Even the “!Particle.connected()” part is called and working.

I also checked the system memory and let it print it after each loop via the serial terminal and when the “!Particle.connected()” part is run. It always shows the same amount of memory.

Any suggestions?
At the moment I can’t use GitHub, but i uploaded my code as a zipped package to my dropbox account if anyone wants to see the whole code: https://www.dropbox.com/s/as4pp303tenwt80/WeatherMonitor.zip?dl=0

Thank you,
Juergen

You are checking !Particle.connected() but you never attempt to Particle.connect(), so how would you get out of that loop?

What SYSTEM_MODE are you using and are you using SYSTEM_THREAD(ENABLED)?

1 Like

I put the Particle.connect() now in the code. I had this exact code from the forum. I thought that the .process() would automatically call the .connect(). (Doesn’t explain why it was working without the yahoo request).

System_Mode and System_Thread are both not changed in my code. They should be standard as defined.

@ScuffR : Thank you very much!

No idea what’s up with your code, since I can’t download our code.
But you could also try a webhook rather than doing it “manually”.

I have redone my code now and everything seems to be working fine. The problem you found, was the problem which fixed it.

Thank you for your help!

2 Likes

@ScruffR, after two weeks of testing it seems, that the problem still occurs. I came back to my apartment today and found my photon stuck again. The LED is flashing cyan and never reconnects to the cloud.

My current code is as following:

//Connection lost -> wait till reconnected
  if (!Particle.connected()) {
        while(!Particle.connected())  {
          Serial.println("Particle.connecte() in Loop");
          Particle.connect();
          Particle.process();
        }
    }

  //Connected Loop
  if (Particle.connected()) {
  ...
  }

Any ideas?

I will test now with SYSTEM_THREAD(ENABLED) . Maybe that helps!

One thing you definetly should avoid is to retrigger a Particle.connect() while a previous attempt is still running.

You could use this

  if (!Particle.connected()) {
      Serial.println("Particle.connecting");
      Particle.connect();
      waitUntil(Particle.connected);
  }
2 Likes

@ScruffR, a if (!waifFor(Particle.connected, 10000)) could also be used to implement a timeout on the attempt so the code can act accordingly (wait 5 mins before trying again for example). :wink:

2 Likes

Definetly!

I just went by this comment:

and the intended behaviour of the original code.

2 Likes

Will try the new code. Hopefully this works better. Problem is, that it sometimes runs for days without problem.

Can you do some stress testing then by frequently cutting WiFi and/or cloud connection and see how your Photon and your AP cope with that?

Will try to do that later.