Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnection Issue #199

Open
Tommybearrr opened this issue Dec 16, 2019 · 0 comments
Open

Reconnection Issue #199

Tommybearrr opened this issue Dec 16, 2019 · 0 comments

Comments

@Tommybearrr
Copy link

I am struggling to get the client lib to reconnect.
What is the correct sequences of methods to use to disconnect a client, if connection is lost and reconnect the client again to the broker?

Here is an example of code I have been playing around with?

` GeyserStatus thestate;
MQTTMessage message;
int rc=0;
char payload[1000];
MQTTClient Newclient;

message.qos = 1;
message.retained = 0;
message.payload = payload;
sprintf(payload, "%s", SendMessage);
message.payloadlen = strlen(SendMessage);

if(ReconnectMQTT)
{
	if(ReconnectCounter++ >= 10)
	{
		NetworkInit(&network);
			if (0!= ConnectNetwork(&network, (char*)AZURE_HUB_URL, MQTT_BROKER_PORT,0)){
				return;
			}
			MQTTClientInit(&Newclient, &network, 10000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf));

			connectData.MQTTVersion = 4; //use protocol version 3.1.1
			connectData.clientID.cstring = DEVICE_ID;
			connectData.username.cstring = MQTT_USER_NAME;
		if ((rc = MQTTConnect(&Newclient, &connectData)) != 0){
			return;
		}

		if ((rc = MQTTSubscribe(&Newclient, MQTT_SUB_TOPIC, &subQoS, MQTTmessageCallback)) != 0){
			return;
		}
		ReconnectMQTT = false;
		ReconnectCounter = 0;
		client = Newclient;
	}
	return;
}

if ((rc = MQTTPublish(&client, MQTT_PUB_TOPIC, &message)) != 0);
{
	if(MessageFailures++> 3)
	{
		MessageFailures = 0;
		MQTTDisconnect(&client);
		ReconnectMQTT = true;
		ReconnectCounter = 10;
		
	}
	
}
 printf("ERR>> Return code from MQTT publish is %d\n\r", rc);

 printf("> send message:  %s\r\n",SendMessage );`

Would appreciate if anyone can point me in the right direction.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant