Particle not responding to MQTT topic

That’s seems to be normal with system thread enabled that’s why, as @ScruffR mentioned:

I’m using Particle.function() to be sure if MQTT client is connected and if not, so let’s connect it :wink:

UPDATE
Now when I speak that loudly I have an idea :grin: which we can try, because is not hurting anyone :grin: namely, let’s check for

WiFi.ready();

Before

1 Like

Thanks, I’ll give that a try. The device ran for about 10 hours before ceasing to send MQTT messages. Dissapointing.
I put the WiFi.ready(); before client.connect(“sparkclient”)’ in setup as suggested. We’ll see how that goes.

void setup() {
  Serial1.begin(9600);
  WiFi.ready();
  client.connect("sparkclient");  // connect to the server
    if (client.isConnected())  // test publish/subscribe
   {
        client.publish("outTopic/message","hello world");  // Send "hello world" to broker
        client.subscribe("inTopic/message");
    }
    else {
    Serial.println("Connection failed ");  //  Indicate connection failure
  }

}  //  END SETUP

I’m still not clear of the purpose of SYSTEM_THREAD(ENABLED) and the “true” settings. What are they doing and why do you think they break the sketch?
As usual, Thanks for your time

No no no you have to check what WiFi.ready(); is returning. When you just add

this doesn't do nothing so in your setup I'll try something like this:

if(WiFi.ready()){
    client.connect("sparkclient");
    if (client.isConnected()){
        client.publish("outTopic/message","hello world");  // Send "hello world" to broker
        client.subscribe("inTopic/message");
        Particle.publish("connected_success_S", "ok", PRIVATE); // for debug
     }else{
        Particle.publish("Not_connected_S", "nok", PRIVATE); // for debug
    }
 }

and then add function reconnect() and global bool rec = true; on the top, then in the loop:

if(!client.isConnected()){
      if(rec){
          Particle.publish("not_connected_retrying_L", "nok", PRIVATE); // for debug
          reconnect();
          rec = false;
         }
      }
if(client.isConnected()){
          client.loop();
          if(!rec){
              Particle.publish("success_connected_L", "ok", PRIVATE);// for debug
              rec = true;
            }
      }

The Particle.publish is just for some simple debug purpose you can removed them after all will work correct
The function reconnect(); can looks like this:

void reconnect(){
  if(WiFi.ready()){
    client.connect("sparkclient");
    if (client.isConnected()){
        client.publish("outTopic/message","hello world");  // Send "hello world" to broker
        client.subscribe("inTopic/message");
        Particle.publish("connected_success_rec", "ok", PRIVATE);
     }else{
        Particle.publish("Not_connected_rec", "nok", PRIVATE);
    }
 }
}

By the way which os ver. you are running ? 2.0.1 ?

SYSTEM_THREAD(ENABLED) and the "true" setting allows your client to run in separate thread and honestly I don't have a clue why it's not working in your case :man_shrugging: