Extend Firmware with additional IoT Protocols

Hi,

I’m currently playing around with my Photon and the MQTT library. Unfortunately the library is rather unstable and results in constant reboots.

For my current project I would like to rely solely on a non cloud approach which is why I can’t use the Cloud API.
On the ESP8266 the options are advancing quite well (there is aREST, MQTT, CoAP), unfortunately the Photon is lacking other options.

Are there any plans to extend the functionality of stable protocols in the near future? I really like the Build Environment, Community Support and all that. But a stable solution is even more important for me :confused:

Which MQTT Library are you using?

I’m getting very good stability with MQTT & the Photon.

I’m using the library in the Build Env.

Could your instability be rooted in your own code or the interaction between your code and the lib?
Can you say what kind of reboot you see (e.g. SOS hard fault)?

Hi,

I removed nearly everything besides a simple publish/subscribe. So I don’t expect any issues from my code.
The issue I experience is the following: I upload the firmware successfully, the device restarts blinks green but never connects and after a few seconds restarts again (and boots along the whole cycle).

Can you provide a code that I could try on my own devices?

That’s good the hirotakaster library is the best and most updated MQTT library for the Particle platform, there are a few older libraries still floating around.

Without seeing your code any other suggestions are going to be a stab in the dark. If you could post your code we might be able to solve your stability problem.

Hi, as far as I know the library is based on the PubSubClient which is rather funky, so I actually expected the instability…

This is my “minimal” code:

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

#define RECONNECT 20*1000
#define CLIENT_NAME "TW3200"

byte server[] = { 192,168,178,44 };
MQTT client(server, 1883, callback);

bool init = true;
unsigned long lastConnect;

void callback(char* topic, byte* payload, unsigned int length) {
        char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    client.publish("/home/livingroom/TW3200/control",p);
}

void setMqtt()
{
    if (client.isConnected()) {
        client.publish("/home/livingroom/TW3200/status","ready");
        client.subscribe("/home/livingroom/TW3200/control");
    }

}


SYSTEM_MODE(MANUAL);

void setup()
{
    WiFi.on();
    WiFi.connect();
}

void loop() {
    if (WiFi.ready() && init) {         // Wait it out until we have a network to talk to
        client.connect(CLIENT_NAME);    // connect to the broker as CLIENT_NAME
        delay(500);
        setMqtt();                      // Perform MQTT pub/sub
        delay(200);
        init = false;
    }

    if (client.isConnected()) {
        client.loop();                  // As freq as possible to keep the realtimeness
    } else if (WiFi.ready()) {          // Sum Ting Wong ... network's there but server's gone :(
        if (millis() - lastConnect > RECONNECT) {   // try not to stress the network
            lastConnect = millis();
            WiFi.off();                 // Restart the WiFi part
            delay(1000);
            WiFi.on();
            delay(500);
            WiFi.connect();
            init = true;                // Reconnect when network's available again
        }
    }
}

It is based on a snippet from one of the community members. Actually I would prefer to have something simpler, but the last time I tried it without all the Wifi stuff the Photon received 1-3 commands and lost the MQTT connection.

This is what I would actually like to work:

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

#define RECONNECT 20*1000
#define CLIENT_NAME "TW3200"

byte server[] = { 192,168,178,44 };
MQTT client(server, 1883, callback);

bool init = true;
unsigned long lastConnect;

void callback(char* topic, byte* payload, unsigned int length) {
        char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    client.publish("/home/livingroom/TW3200/events",p);
    String command = String(p);
    if(command.equalsIgnoreCase("on"))
    {
        Serial1.write("PWR ON\n\r"); 
        
    }
    if(command.equalsIgnoreCase("off"))
    {
        Serial1.write("PWR OFF\n\r"); 
        
    }
}

void setMqtt()
{
    client.connect(CLIENT_NAME);
    if (client.isConnected()) {
        client.publish("/home/livingroom/TW3200/status","ready");
        client.subscribe("/home/livingroom/TW3200/control");
    }

}

void setup()
{
    Serial1.begin(9600);
   setMqtt();
}

void loop() {

    if (client.isConnected()) {
        client.loop();                  // As freq as possible to keep the realtimeness
    } else {
        setMqtt();
    }
    
}