Failure to connect after 1001 publishes

28 views
Skip to first unread message

DrBwts

unread,
Jun 6, 2023, 6:06:10 AM6/6/23
to MQTT
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



Greg Troxel

unread,
Jun 6, 2023, 7:59:43 AM6/6/23
to DrBwts, MQTT
DrBwts <drb...@gmail.com> writes:

> 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,

My understanding is that this list is for protocol issues, and that it
would be more appropriate to find and post on a list for users of
mosquitto.

Also, your code is either very badly formatted or the formatting has
been lost when including it in the email. It therefore requires an
unreasonable amount of effort to read, and thus I didn't try.

You also apparently did not turn on server debugging and run tcpdump
while doing this, and attach those, after looking over the data
yourself. I would suggest doing that.

As I think this is off topic, I will not comment further here.

Greg

DrBwts

unread,
Jun 6, 2023, 8:30:25 AM6/6/23
to MQTT
I am trying to eliminate a potential protocol issue as a source for this problem. Its not my fault that your email lost the formatting.

Thanks for your input Greg

Gambit Communications

unread,
Jun 6, 2023, 1:26:19 PM6/6/23
to MQTT
The problem with your code is that you are establishing a new connection each time
through the loop, and probably hitting some per-process or system limit on the number
of open connections.
If you move that MQTTClient_connect out of the loop it will succeed.
If you are attempting to emulate short-term connections, then you should disconnect
after publishing.

Andy Stanford-Clark

unread,
Jun 7, 2023, 4:58:59 AM6/7/23
to 'Simon Walters' via MQTT
Is the payload the same length every time?
I’m wondering if you’re hitting the max message size of the MQTT client implentation you’re using.

Not really enough info to go on, here.

Andy

--
To learn more about MQTT see https://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mqtt/05bae2eb-7db0-482e-9d87-d9b2e2504d14n%40googlegroups.com.

DrBwts

unread,
Jun 8, 2023, 5:46:39 AM6/8/23
to MQTT
" If you move that MQTTClient_connect out of the loop it will succeed. "

This was the culprit, thanks for spotting that.

Nic

Reply all
Reply to author
Forward
0 new messages