MQTT Subscribe not working with Photon

I am attempting to integrate openHAB with a Particle Photon using Mosquitto MQTT server to ultimately open/close a relay but the subscribe function does not want to work in the Photon code. I have adapted code published by hirotakaster on github. I have 2 topics configured in my openHAB item one called “relaystatus” and the other “setrelaystatus” and my sitemap is setup to display the “relaystatus” value (via subscribe) and toggle “setrelaystatus” (publish) between on and off via a toggle button. When testing I can see a value for “relaystatus” on my sitemap so this seems to be working. The problem seems to be with the “setrelaystatus” topic and specifically that the Photon either does not get the on/off message or it does receive them but it doesn’t actually “write” to the specified interface. What should happen if all was working correctly is when a move the sitemap toggle to the on position openHAB should publish the message “on” to MQTT topic “setrelaystatus”; my photon subscribes to this topic so it should receive this message, and its code should tell it to send a digitalWrite HIGH command to led1 (A0) or LOW if the message recieved is “off”. Using MQTTlens to also subscribe to the “setrelaystatus” topic I can see the message “on” being published by openHAB so it seems my OpenHAB and MQTT config are correct and able to communicate. Additionally the Photon can communicate with my MQTT broker and ultimately openHAB since I see messages in the topic “relaystatus” which can only come from the Photon. As I type this I am becoming more and more convinced that the issue is the Photon specifically what it does or in this case doesn’t do in response to the “setrelaystatus” topic messages. Any help would be greatly appreciated. Below is my Photon code please let me know if further info is needed.

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

// This #include statement was automatically added by the Particle IDE.
#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);
 **/
byte server[] = {172,30,10,5};
MQTT client(server, 1883, callback);
NCD1Relay controller;
int relayStatus;
int setrelaystatus;
int led1 = A0; 
unsigned long lastSend = 0;
unsigned long sendInterval = 2000;

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);
    String t = String(topic);
    Serial.println(t);
    Serial.println(message);
 if(message.equals("on"))
        digitalWrite(led1, HIGH); 
 else if(message.equals("off"))
        digitalWrite(led1, LOW); 
  
}


void setup() {
    controller.setAddress(0,0,0);
    relayStatus = controller.readRelayStatus();
    
    // connect to the server
    client.connect("NCD1 Relay controller");

    // publish/subscribe
        if (client.isConnected()) {
        client.subscribe("setrelaystatus");
    }
        if (client.isConnected()) {
        client.publish("outTopic","hello world");
    }
        if (client.isConnected()) {
        client.subscribe("outTopic");
    }
     
    delay(1000);
}

void loop() {
    if (client.isConnected()){
        client.loop();
        if(millis() >= lastSend+sendInterval){
            lastSend = millis();
            client.publish("relaystatus",String(relayStatus));
        }
        
    }
    relayStatus = controller.readRelayStatus();
}

Hi @ruxpin, I would suggest you post this issue as a Github issue on the library’s GitHub page as this is a 3rd party library not maintained by Particle.

@ruxpin, try testing the loop() method first. Mine is working this way, and never using mq.isConnected(). Try to avoid the String class. I am on Redbear duo, but mqtt code is the same.

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    Serial.println(topic);
    Serial.println(p);
 if (!strcmp(p, "on"))
        digitalWrite(led1, HIGH); 
 else if (!strcmp(p, "off"))
        digitalWrite(led1, LOW); 
  
}

void setup()
{
    bool mqConnected;

    mqConnected = mq.connect("photon_xxx");

// No broker ? No party.
    if (!mqConnected) {
        System.reset();
    } else {
        mq.publish("photon/log", "alive"); 
        mq.subscribe("photon/cmd"); 
    }
}

void loop()
{
    bool mqConnected;
    
    mqConnected = mq.loop();

    if (!mqConnected) {
        mqConnected = mq.connect("photon_xxx");

// No broker ? No party.
        if (!mqConnected) {
            System.reset();
        } else {
            mq.publish("photon/log", "alive again"); 
            mq.subscribe("photon/cmd"); 
        }
    }
}