[Submission] MQTT Library and Sample

@subh2020, you need to post this question in the Redbear forum.

@peekay123, Thanks for pointing out and input.
I will try to repost.

1 Like

I have a question about the Library and I hope it’s okay if I post it here.

I try to compare and Serial.println the message that the Broker sends me after I subscribed to a Topic.
It works great in AUTOMATIC mode but in SEMI_AUTOMATIC is does not work. Does the Library really need the Particle Cloud?

@larusso, in SEMI_AUTOMATIC mode, you are required to start the WiFi connection with WiFi.connect() prior to using the library. Are you doing this?

Okay, I try to solve the issue and it seems it has nothing to do with the SEMI_AUTOMAIC mode.
What I want to accomplish is to publish and subscribe to a topic and then go into deep sleep. Here is my code:

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

#define CLIENT_NAME "Photon"


byte server[] = { xxx,xxx,x,xx };
MQTT client(server, 1883, callback);

bool init = true;
unsigned long lastConnect;

void callback(char* topic, byte* payload, unsigned int length) {
    // MQTT messages received here in topic and payload
    Serial.print("Message arrived ["); 
    Serial.print("] "); 
    for (int i=0;i<length;i++) {  
        char receivedChar = (char)payload[i];  

void setMqtt()
    if (client.isConnected()) {

void setup()
    Serial.begin(9600);   // open serial over USB

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
        setMqtt();                      // Perform MQTT pub/sub
        init = false;
	if (client.isConnected()) {
    //System.sleep(SLEEP_MODE_DEEP, 8);

If I don’t go into SLEEP_MODE_DEEP, it does work. But when I activate the SLEEP_MODE_DEEP, it does not work.
Does anyone can help me out?

What do you mean “it doesn’t work”? You have deep sleep set for 8 seconds after which it will RESET the Photon (as if you hit the reset button) and start the code over again. So whatever MQTT sessions you had going will be gone or left hanging on the server side. You may need to take this in consideration and close the MQTT session before sleeping. Also, 8 seconds is not much of a deep sleep IMO. It could take more than that just to restart WiFi and connect to the MQTT server! Why so short?

Thank you for your answer.

You are right, that was not a good description. I’m sorry for that!
What I meant by “it doesn’t work” is, that the Serial does not print out anything. If I comment out the SLEEP_MODE_DEEP, it works as expected.

The 8 seconds for deep sleep are only for testing purposes to see if Serial.print works after the deep sleep.
Which means, that client.subscribe("Photon1/+"); does not work. And client.publish("Photon1/Status","ready"); isn’t working either.

@larusso, add:

  while(!Serial.available()) Particle.process();

at the end of setup(), after the WiFi.connect(). The while() will wait on a key to be pressed on your terminal program. On Windows, every time the Photon resets, the virtual port will disconnect! This will make sure you have time to reconnect and hit a button before proceeding.

The last line is to make sure you only proceed if WiFi has truly connected before proceeding. Otherwise, loop() will simply fall through to System_sleep() and NEVER connect.

There was missing a " in your Serial.print("Starting);

Okay, I added the code as you say but the problem still remains. client.subscribe and client.publish does not work.

@larusso, you have to be a bit more descriptive! Did the serial port show the “Starting” text? Add more serial prints to show program progression. Does client.connect() return anything? Perhaps that is failing but you don’t check for that.

would i be able to add this my dallas temp probe sketch? to publish temp data

Just wanted to thank @edwintam for his MQTT code which seems to have vastly stabilised running a MQTT client on a Core …

1 Like

Is this library available in the Particle libraries folder? Thanks

Do you mean this library?

I am currently using that library and it only allows anonymous connections. The library discussed in this thread seems to have support for connections with username/password.

I’d have to (re)test, but I am pretty sure I had managed to get a user/pwd connection in the past.

bool connect(const char *id, const char *user, const char *pass);

Humm. Somehow I missed that. I will give it a try. Thanks

Welcome to the community :+1:
However, I have removed your post in this thread, since you already have also a new thread with exactly the same question and double posting is considered bad practice.

Continue the discussion with @rohan_vamshiG here

1 Like

@ScruffR My bad will continue the discussion on the thread I created. Anyways thanks for the edit on my question :slight_smile:

1 Like

Interestingly I’ve been trying to use the hirotasker MQTT code with a Core and the Core hasn’t been able to connect to the broker at all. I’ve just tried in desperation edwintam’s template code with the same mqtt.h library and it works a treat. I wonder what that’s all about.

(When the Core is using the hirotasker code it’s still connected to the cloud, it doesn’t freeze like I sometimes find when my code goes screwy.)