[Submission] MQTT Library and Sample


#83

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?


#85

@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?


#86

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"

SYSTEM_MODE(MANUAL);

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(topic); 
    Serial.print("] "); 
    for (int i=0;i<length;i++) {  
        char receivedChar = (char)payload[i];  
        Serial.print(receivedChar);  
    }  
}

void setMqtt()
{
    if (client.isConnected()) {
        client.publish("Photon1/Status","ready");
        client.subscribe("Photon1/+");
    }
}


void setup()
{
    Serial.begin(9600);   // open serial over USB
    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
        setMqtt();                      // Perform MQTT pub/sub
        init = false;
	}
    
	if (client.isConnected()) {
        client.loop(); 
    }
    
    //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?


#87

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?


#88

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.


#89

@larusso, add:

  while(!Serial.available()) Particle.process();
  Serial.print("Starting);
  waitUntil(WiFi.ready);

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.


#90

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.


#91

@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.