Fix paho issue when server is not configured

This commit is contained in:
fvanroie 2021-05-02 01:26:33 +02:00
parent 81976d6ec9
commit 7cab0c2f5c
2 changed files with 34 additions and 16 deletions

View File

@ -97,7 +97,7 @@ class BaseDevice {
virtual std::string gpio_name(uint8_t pin) virtual std::string gpio_name(uint8_t pin)
{ {
char buffer[8]; char buffer[8];
itoa(pin, buffer, DEC); itoa(pin, buffer, 10); // DEC
return buffer; return buffer;
} }
}; };

View File

@ -265,7 +265,7 @@ static void onConnect(void* context)
connected = 1; connected = 1;
std::string topic; std::string topic;
LOG_VERBOSE(TAG_MQTT, "Successful connection"); LOG_VERBOSE(TAG_MQTT, D_MQTT_CONNECTED, mqttServer.c_str(), haspDevice.get_hostname());
topic = mqttGroupTopic + "command/#"; topic = mqttGroupTopic + "command/#";
mqtt_subscribe(mqtt_client, topic.c_str()); mqtt_subscribe(mqtt_client, topic.c_str());
@ -290,11 +290,13 @@ static void onConnect(void* context)
void mqttStart() void mqttStart()
{ {
printf("%s %d\n", __FILE__, __LINE__);
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer; MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;
int rc; int rc;
int ch; int ch;
printf("%s %d\n", __FILE__, __LINE__);
if((rc = MQTTClient_create(&mqtt_client, mqttServer.c_str(), haspDevice.get_hostname(), MQTTCLIENT_PERSISTENCE_NONE, if((rc = MQTTClient_create(&mqtt_client, mqttServer.c_str(), haspDevice.get_hostname(), MQTTCLIENT_PERSISTENCE_NONE,
NULL)) != MQTTCLIENT_SUCCESS) { NULL)) != MQTTCLIENT_SUCCESS) {
printf("Failed to create client, return code %d\n", rc); printf("Failed to create client, return code %d\n", rc);
@ -308,26 +310,37 @@ void mqttStart()
// return; // return;
// } // }
conn_opts.will = &will_opts; printf("%s %d\n", __FILE__, __LINE__);
conn_opts.will->message = "offline"; if(mqttServer.length() > 0) {
conn_opts.will->qos = 1; conn_opts.will = &will_opts;
conn_opts.will->retained = 1; conn_opts.will->message = "offline";
conn_opts.will->topicName = mqttLwtTopic.c_str(); conn_opts.will->qos = 1;
conn_opts.will->retained = 1;
conn_opts.will->topicName = mqttLwtTopic.c_str();
conn_opts.keepAliveInterval = 20; conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1; conn_opts.cleansession = 1;
conn_opts.connectTimeout = 2; // seconds
conn_opts.retryInterval = 0; // no retry
conn_opts.username = mqttUser.c_str(); conn_opts.username = mqttUser.c_str();
conn_opts.password = mqttPassword.c_str(); conn_opts.password = mqttPassword.c_str();
if((rc = MQTTClient_connect(mqtt_client, &conn_opts)) != MQTTCLIENT_SUCCESS) { printf("%s %d\n", __FILE__, __LINE__);
printf("Failed to connect, return code %d\n", rc); if((rc = MQTTClient_connect(mqtt_client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
rc = EXIT_FAILURE; printf("Failed to connect, return code %d\n", rc);
// goto destroy_exit; rc = EXIT_FAILURE;
// goto destroy_exit;
} else {
onConnect(&mqtt_client);
}
} else { } else {
onConnect(&mqtt_client); rc = EXIT_FAILURE;
printf("Mqtt server not configured\n");
} }
printf("%s %d\n", __FILE__, __LINE__);
// while (!subscribed && !finished) // while (!subscribed && !finished)
// #if defined(_WIN32) // #if defined(_WIN32)
// Sleep(100); // Sleep(100);
@ -367,18 +380,23 @@ void mqttStop()
void mqttSetup() void mqttSetup()
{ {
printf("%s %d\n", __FILE__, __LINE__);
mqttNodeTopic = MQTT_PREFIX; mqttNodeTopic = MQTT_PREFIX;
mqttNodeTopic += "/"; mqttNodeTopic += "/";
mqttNodeTopic += haspDevice.get_hostname(); mqttNodeTopic += haspDevice.get_hostname();
mqttNodeTopic += "/"; mqttNodeTopic += "/";
printf("%s %d\n", __FILE__, __LINE__);
mqttGroupTopic = MQTT_PREFIX; mqttGroupTopic = MQTT_PREFIX;
mqttGroupTopic += "/"; mqttGroupTopic += "/";
mqttGroupTopic += mqttGroupName; mqttGroupTopic += mqttGroupName;
mqttGroupTopic += "/"; mqttGroupTopic += "/";
printf("%s %d\n", __FILE__, __LINE__);
mqttLwtTopic = mqttNodeTopic; mqttLwtTopic = mqttNodeTopic;
mqttLwtTopic += LWT_TOPIC; mqttLwtTopic += LWT_TOPIC;
printf("%s %d\n", __FILE__, __LINE__);
} }
void mqttLoop() void mqttLoop()