Photon flashes red then reboots about every 6-10mins

Hi,

I’ve been trying to figure out why the photon is crashing and rebooting, I cant for the life of me figure out what is causing it.

My code pulls some weather data using a webhook:

   {
  "event": "weatherU_hook",
  "url": "http://api.wunderground.com/api/mykeyhere/conditions/q/AZ/Tucson.json",
  "requestType": "POST",
  "headers": null,
  "query": null,
  "responseTemplate": "{{#current_observation}}{{wind_dir}}~{{wind_mph}}~{{wind_gust_mph}}~{{precip_1hr_in}}{{/current_observation}}",
  "json": null,
  "auth": null,
  "mydevices": true
}

My device code is:

#define HOOK_RESP	"hook-response/weatherU_hook"
#define HOOK_PUB	"weatherU_hook"

bool weatherGood;
int badWeatherCall;
String currentWind_dir;
float currentWind_mph = 0;
float currentWind_gust_mph = 0;
float currentPrecip_1hr_in = 0;
unsigned long uptime;


Timer timer(150000,  getWeather);

void setup() {
  Time.zone(-7);

  delay(2000);

  Spark.subscribe(HOOK_RESP, gotweatherData, MY_DEVICES);
  Spark.publish(HOOK_PUB);
  timer.start();

}



void loop(){

}


void getWeather() {

  weatherGood = false;
 
  Spark.publish(HOOK_PUB);
  unsigned long wait = millis();
  while (!weatherGood && (millis() < wait + 5000UL))
  
    Spark.process();
    delay(500);
  if (!weatherGood) {
    badWeatherCall++;
    if (badWeatherCall > 2) {

    }
  }
  else
    badWeatherCall = 0;
}

void gotweatherData(const char *name, const char *data) {

      if (strlen(data)+1 > 0){
        String str = String(data);
        char strBuffer[strlen(data)+1] = "";
        str.toCharArray(strBuffer, strlen(data)+1); // example: "\"North~0~1.3~0.00\""
        strtok(0,0);
        String wind_dir = strtok(strBuffer, "\"~");
        float wind_mph = atof(strtok(NULL, "~"));
        float wind_gust_mph = atof(strtok(NULL, "~"));
        float precip_1hr_in = atof(strtok(NULL, "~"));

            currentWind_dir = wind_dir;

            currentWind_mph = wind_mph;

            currentWind_gust_mph = wind_gust_mph;

            currentPrecip_1hr_in = precip_1hr_in;
   
      bool weatherGood = true;
      }
}

If someone could help point me in the right direction I’d be super grateful.

Both software timers and subscription should be treated as interrupts, and should thus be kept as short as possible. Use them to set a flag which you can act on in the loop, but don’t delay either of those functions.
Furthermore, you’re still using the old ‘Spark’ syntax. While they might still work, it’s best to update those.

If data is nil, strlen() will be 0, so your if statement will always be true.

Sometimes String objects can cause heap fragmentation. I don't think this is your problem, but why convert a const char* to a String, and then back again to a char*? You should just use strncpy() to make a copy of the received data, and leave String out of it,

void gotweatherData(const char *name, const char *data) {
    int len = strlen(data) + 1;
    char strBuffer[len];
    strncpy(strBuffer, data, len);
    // etc.

I have no idea what this is about.

2 Likes

@Moors7 As soon as I removed the interrupts the problem went away.
@Ric Thanks for the advice, I did as you suggested and cleaned up my code.