Hi,
I have a Mosquitto broker running on a Raspberry Pi. I have some code that is publishing some DAQ data every second but like clockwork I get,
"Failed to connect, return code -1"
when publishing payload 1002, always the same payload count, 1002 fails to connect so never gets published.
As this is repeatable over many runs over many days I suspect I'm missing something in the set up. Anyone any clues?
My code,
// Whole load of includes
#include "MQTTClient.h"
#define MQTT_ADDRSS "tcp://localhost:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "mqtt_test"
#define QOS 0
double pub_count;
int publish(MQTTClient client,
MQTTClient_deliveryToken token,
MQTTClient_connectOptions conn_opts,
double payload[],
uint32_t num_samples)
{
int rc = 0;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
union data{
double f[num_samples + 1];
char s[num_samples + 1];
};
union data_package;
pub_count++;
package.f[0] = pub_count;
for(int i = 1; i < num_samples; i++)
{
package.f[i] = payload[i];
}
int payload_len = (num_samples + 1) * sizeof(double);
MQTTClient_publish(client, TOPIC, payload_len, package.s, QOS, 0, token);
printf("Published: %.0f\n", pub_count);
return rc;
}
int main(void)
{
// MQTT initializers
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_delivertToken = token;
MQTTClient_create(&client, MQTT_ADDRSS, CLIENTID, MQTTCIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
// Whole load of DAQ hardware set up & checking code
do
{
for(device = 0; device < DEVICE_COUNT; device++)
{
// DATA ACQUISTION LOOP
// data in read_buf
}
publish(client, token, conn_opts, read_buf, read_buf_size);
} while (!enter_press());
// Clear up code