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?
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?
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?
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.
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.
@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.
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.
Thanks
Welcome to the community
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.
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.)