What's wrong with my Yun sketch?

I think it's my code that keeps making my Yun disconnect from my Openhab and Mosquito server after about 45 minutes. Could it be?

-Running Yun with this sketch:

(Will paste below)

-Running Openhab and Mosquitto servers both running fine on a raspberry pi

It works fine until I leave it for about 45 minutes and I get the message in the Openhab terminal that the Arduino has been disconnected. Mosquitto and Openhab server still running fine after that on the Pi.

------ Yun sketch --------

#include <SPI.h>
#include <PubSubClient.h>
#include <YunClient.h>
#include <Adafruit_NeoPixel.h>

#define PIN 6
#define PIN2 8
#define BRIGHT 255

Adafruit_NeoPixel strip = Adafruit_NeoPixel(144, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(144, PIN2, NEO_GRB + NEO_KHZ800);

void callback(char* topic, byte* payload, unsigned int length){
//listening on the subscribed topic and then converting message to string
//convert byte to char
payload[length] = '\0';
String strPayload = String((char*)payload);
Serial.println(topic); Serial.println(strPayload);

// handle message arrived
if(strPayload == "1") {
digitalWrite(13, HIGH);
strip.show();
strip2.show();
strip.setBrightness(BRIGHT);
strip2.setBrightness(BRIGHT);
fireEffect();
}
else if(strPayload == "0") {
digitalWrite(13, LOW);
strip.show();
strip2.show();
strip.setBrightness(0);
strip2.setBrightness(0);
}
else if(strPayload == "10") {
digitalWrite(13, HIGH);
strip.show();
strip2.show();
strip.setBrightness(3);
strip2.setBrightness(3);
}
else if(strPayload == "100") {
digitalWrite(13, HIGH);
strip.show();
strip2.show();
strip.setBrightness(80);
strip2.setBrightness(80);
}
else{}

}

void fireEffect() {

int r = 255;
int g = r-110;
int b = 10;

for(int x = 8; x <99; x++) {
int flicker = random(0,180);
int r1 = r-flicker;
int g1 = g-flicker;
int b1 = b-flicker;
if(g1<0) g1=0;
if(r1<0) r1=0;
if(b1<0) b1=0;
strip.setPixelColor(x,r1,g1, b1);
strip2.setPixelColor(x,r1,g1, b1);
}

delay(random(50,150));
strip.show();
strip2.show();
}

byte server[] = { 192, 168, 0, 17 };
int port = 1883;
char* pubTopic = "openhab/garage/relay1";
char* subTopic = "openhab/garage/relay1";

YunClient yun;
PubSubClient mqtt(server, port, callback, yun);

void setup() {
Serial.begin(9600);
Bridge.begin();
pinMode(13, OUTPUT);
digitalWrite(13, LOW);

strip.setBrightness(BRIGHT);
strip2.setBrightness(BRIGHT);

if (mqtt.connect("arduinoClient")) {
mqtt.publish(pubTopic,"Connected!");
mqtt.subscribe(subTopic);
}

// Initialize all pixels to 'off'
strip.begin();
strip.show();
strip2.begin();
strip2.show();

mqtt.publish(pubTopic,"0"); //turn off lights on initial reset of arduino
delay(2000);
mqtt.publish(pubTopic,"1");
delay(2000);
mqtt.publish(pubTopic,"0");

}

void loop() {

fireEffect();
mqtt.loop();
delay(50);

}

I was having this issue at one stage also.
Not sure what it was however. I would be interested to know if someone has an answer?

To fix it for me I changed my code a bit so that it would do a quick check in the loop if it was still connected to broker, and if not, it would connect.

Might be a good idea to just throw a publish to a debug topic when it reconnects and use something like MQTTWarn (AMAZING python project) to log the debug topics to a text log file with time stamp so you can see how often it happens to see a pattern?

Something like this at the start of the loop

if (!MQTTClient.connected())
  {
    long lastAttempt = millis();
    if (lastAttempt - lastReconnectAttempt > 5000)
    {
      lastReconnectAttempt = lastAttempt;
      // Attempt to reconnect
      if (reconnect()) //Connect to MQTT method
      {
        lastReconnectAttempt = 0;
      }
    }
  }
  else
  {
      //Loop Code as per normal.
   }

Thanks, I'll try that too. It's definitely something in the code because I loaded a simpler sketch controlling my blinds and has been solid for a whole day now.

Were you using the neopixel library by chance as well?

I'm not sure what it could be. I'm looking at possibly using the Mosquitto client on the Yun itself and using this to pass data back to the Arduino for compatibility reasons with my application but haven't done so yet.

cfry999:
Were you using the neopixel library by chance as well?

Nope, my code is somewhat complex and uses some other libraries but not the neopixel.

Adding the reconnect method should make it reconnect if there is an issue at least. If you wanted to get very tricky, you could look at implementing a form of reset of the arduino and yun if it doesn't reconnect after x amount of tries in like 5 minutes.

Thanks, that simple reconnect worked for me. I've thought about running mosquitto directly on the Yun too. It seems pretty lightweight in terms of processing. For some reason, I have more more confidence in my pi right now in terms of stability. I'm just using the pi zero too. I'm pretty impressed that I can have a $5 home automation server running.