MQTT connection from Photon to Raspberry Pi Broker

Hello

I have some strange issues connecting a Photon to Mosquitto MQTT running on a Raspberry Pi. I use the example mqtttest.ino project. The strange thing is that the connection is successfully sometime and the Photon publishes data to the broker and have a running subscription where I can publish from another mqtt client and see the LED change colors.

I have tried other MQTT clients and they always connect successfully to the Raspberry Pi, so i’m thinking issues with the Photon. Does anyone have some insight on this subject?

#include "MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * MQTT client("www.sample.com", 1883, callback);
 **/
//MQTT client("server_name", 1883, callback);

 byte server[] = { 172,20,10,3 };
 MQTT client(server, 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
}


void setup() 
{
    RGB.control(true);

    // connect to the server
    client.connect("spat");

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("outTopic/message","hello world");
        client.subscribe("inTopic/message");
    }
}

void loop() 
{
    if (client.isConnected())
        client.loop();
}