This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] CC3200:MQTT 代理断开连接问题

Guru**** 2756835 points

Other Parts Discussed in Thread: CC3200, CC3200SDK, CC3100, SYSBIOS

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/1067051/cc3200-mqtt-broker-disconnection-issue

部件号:CC3200
在“Thread:(线程: )”,“CC3100”, “SysBIOS”中讨论的其它部件

尊敬的团队:

下面是 我正在使用的设置详细信息:  

  1. CC3200 Wi-Fi 芯片。
  2. SDK: CC3200SDK_1.3.0
  3. Service Pack:CC3100_CC3200_ServicePack_1.0.1.15-2.14.0.0

目标:收集振动传感器数据并通过 Wi-Fi 网络将其发布到 MQTT 服务器上

  • 用于连接振动传感器的 SPI 协议  
  • WPA-2安全性

参考示例代码:

  • 我正在使用 MQTT 客户端代码,并根据要求进行更改

问题:2/3天后,MQTT 上的数据未发布

  • 最初,设备已连接,数据正在通过 MQTT 代理发布
  • 该设备在几天内工作正常,例如2/3天,断开连接和重新连接多次,同时发布数据
  • 但在2/3天后,设备保持与 Wi-Fi 的连接,但无法通过 MQTT 代理发布数据

请查看以下日志以供参考

https://shalakacom-my.sharepoint.com/:u:/g/personal/shivaprasad_desai_shalaka_com/ETNSyCSMekVAuV4H6WkqkxMBP9QLU_ILop4ORDkz09mDcg?e=aDfSPG

下面是代理的详细信息

MQTT_HOST ="15.207.157.102"
MQTT_port = 1883
MQTT_keepalive_interval = 45
MQTT_topic ="#"
MQTT_USERNAME ="optiex"

这可能是什么问题?我希望您能尽早与上述问题取得联系。

下面是我使用的 MQTT 客户端代码的代码片段,

//*****************************************************************************
//
//! Task implementing MQTT client communication to other web client through
//!    a broker
//!
//! \param  none
//!
//! This function
//!    1. Initializes network driver and connects to the default AP
//!    2. Initializes the mqtt library and set up MQTT connection configurations
//!    3. set up the button events and their callbacks(for publishing)
//!    4. handles the callback signals
//!
//! \return None
//!
//*****************************************************************************

void MqttClient(void *pvParameters)
{
	long lRetVal = -1;
	int iCount = 0;
	int iNumBroker = 0;
	int iConnBroker = 0;
	unsigned char policyVal;
	UART_PRINT("started MQTTClient \n\r");
//	connect_config *local_con_conf = (connect_config *)app_hndl;
	local_con_conf = (connect_config *)app_hndl;

	// Reset The state of the machine
	Network_IF_ResetMCUStateMachine();

	// Start the driver
	lRetVal = Network_IF_InitDriver(ROLE_STA);
	if(lRetVal < 0)
	{
		LOOP_FOREVER();
	}

	// switch on Yellow LED to indicate Simplelink is properly up
	MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON


	// Start Timer to blink Red LED till AP connection
	LedTimerConfigNStart();

	// Initialize AP security params
	SecurityParams.Key = (signed char *)SECURITY_KEY;
	SecurityParams.KeyLen = strlen(SECURITY_KEY);
	SecurityParams.Type = SECURITY_TYPE;

	//
	// Connect to the Access Point
	//
	lRetVal = Network_IF_ConnectAP(SSID_NAME, SecurityParams);
	if(lRetVal < 0)
	{
		UART_PRINT("Connection to an AP failed\n\r");
		LOOP_FOREVER();
	}
	else
	{
		UART_PRINT("Successfully Connected to an AP \n\r");
	}

	lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&SecurityParams,0,1,0);

	/*
		//set AUTO policy
		lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
				SL_CONNECTION_POLICY(1,0,0,0,0),
				&policyVal, 1 PolicyValLen);
	 */

	//set AUTO policy
	lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
			SL_CONNECTION_POLICY(1,1,0,0,0),
			&policyVal, 1 /*PolicyValLen*/);		//SL_CONNECTION_POLICY is set to auto and fast config connection mode.

	g_uiSimplelinkRole =  sl_Start(NULL,NULL,NULL);

	//waiting for the device to Auto Connect
	while ( (!IS_IP_ACQUIRED(g_ulStatus))&&
			g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT)
	{
		//Turn RED LED On
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
		osi_Sleep(50);

		//Turn RED LED Off
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		osi_Sleep(50);

		g_ucConnectTimeout++;
	}


	// Disable the LED blinking Timer as Device is connected to AP
	LedTimerDeinitStop();

	// Switch ON RED LED to indicate that Device acquired an IP
	MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
	UtilsDelay(20000000);
	MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF

	// Register Push Button Handlers
	Button_IF_Init(pushButtonInterruptHandler2,pushButtonInterruptHandler3);
	ReconnectNow:
	if(boolRestart)
	{
		UART_PRINT("User input Smart Config started \n\r");
//		sl_ExtLib_MqttClientDisconnect((void*)local_con_conf[iCount].clt_ctx);
		/* Clear all stored profiles and reset the policies */
		lRetVal = sl_WlanProfileDel(0xFF);
//		ASSERT_ON_ERROR(lRetVal);
		sl_WlanDisconnect();


		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
		UtilsDelay(100000);
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		UtilsDelay(100000);
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON

		// Start Timer to blink Red LED till AP connection
		LedTimerConfigNStart();
		SetConnectionPolicy();
		// Disable the LED blinking Timer as Device is connected to AP
		LedTimerDeinitStop();
		UART_PRINT("Device is connected to AP after user defined Smart Config \n\r");
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		boolRestart = false;
	}

	connect_to_broker:
	// Initialze MQTT client lib
	lRetVal = sl_ExtLib_MqttClientInit(&Mqtt_Client);
	if(lRetVal != 0)
	{
		UART_PRINT("MQTT client lib initialization failed \n\r");
		// lib initialization failed
		LOOP_FOREVER();
	}
	else
	{
		UART_PRINT("MQTT client lib initialization Successful \n\r");
	}

	/******************* connection to the broker ***************************/
	iNumBroker = sizeof(usr_connect_config)/sizeof(connect_config);
	if(iNumBroker > MAX_BROKER_CONN)
	{
		UART_PRINT("Num of brokers are more then max num of brokers\n\r");
		LOOP_FOREVER();
	}

//	connect_to_broker:
	while(iCount < iNumBroker)
	{
		//create client context
		local_con_conf[iCount].clt_ctx =
				sl_ExtLib_MqttClientCtxCreate(&local_con_conf[iCount].broker_config,
						&local_con_conf[iCount].CallBAcks,
						&(local_con_conf[iCount]));

		// Set Client ID
		sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
				SL_MQTT_PARAM_CLIENT_ID,
				local_con_conf[iCount].client_id,
				strlen((char*)(local_con_conf[iCount].client_id)));

		// Set will Params
		if(local_con_conf[iCount].will_params.will_topic != NULL)
		{
			sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
					SL_MQTT_PARAM_WILL_PARAM,
					&(local_con_conf[iCount].will_params),
					sizeof(SlMqttWill_t));
		}

		// setting username and password
		if(local_con_conf[iCount].usr_name != NULL)
		{
			sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
					SL_MQTT_PARAM_USER_NAME,
					local_con_conf[iCount].usr_name,
					strlen((char*)local_con_conf[iCount].usr_name));

			if(local_con_conf[iCount].usr_pwd != NULL)
			{
				sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
						SL_MQTT_PARAM_PASS_WORD,
						local_con_conf[iCount].usr_pwd,
						strlen((char*)local_con_conf[iCount].usr_pwd));
			}
		}

		// connectin to the broker
		if((sl_ExtLib_MqttClientConnect((void*)local_con_conf[iCount].clt_ctx,
				local_con_conf[iCount].is_clean,
				local_con_conf[iCount].keep_alive_time) & 0xFF) != 0)
		{
			UART_PRINT("\n\rBroker connect fail for conn no. %d \n\r",iCount+1);
			//delete the context for this connection
			sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);

			break;
		}
		else
		{
			UART_PRINT("\n\rSuccess: conn to Broker no. %d\n\r ", iCount+1);
			local_con_conf[iCount].is_connected = true;
			iConnBroker++;
		}

		// Subscribe to topics
		if(sl_ExtLib_MqttClientSub((void*)local_con_conf[iCount].clt_ctx,
				local_con_conf[iCount].topic,
				local_con_conf[iCount].qos, TOPIC_COUNT) < 0)
		{
			UART_PRINT("\n\r Subscription Error for conn no. %d\n\r", iCount+1);
			UART_PRINT("Disconnecting from the broker\r\n");
			sl_ExtLib_MqttClientDisconnect(local_con_conf[iCount].clt_ctx);
			local_con_conf[iCount].is_connected = false;

			//delete the context for this connection
			sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
			iConnBroker--;
			break;
		}
		else
		{
			int iSub;
			UART_PRINT("Client subscribed on following topics:\n\r");
			for(iSub = 0; iSub < local_con_conf[iCount].num_topics; iSub++)
			{
				UART_PRINT("%s\n\r", local_con_conf[iCount].topic[iSub]);
			}
		}
		iCount++;
	}


	if(iConnBroker < 1)
	{
		// no succesful connection to broker
		UART_PRINT(" no succesful connection to broker \n\r");
		goto end;
	}

	iCount = 0;

	for(;;)
	{
		if(boolSmartConfig)             //Conditional entry check for Force Smart Configuration
		{
			boolRestart=true;
			boolSmartConfig = false;
			goto ReconnectNow;
		}

		//Check If AP is disconnected
		if(boolBrokerDisconnected)
		{
//			iConnBroker--;
//			/* Derive the value of the local_con_conf or clt_ctx from the message */
//			sl_ExtLib_MqttClientCtxDelete(((connect_config*)(RecvQue.hndl))->clt_ctx);
			UART_PRINT("Broker disconnected \n\r");
			UART_PRINT("retry connection \n\r");
			//Jumps to Reconnection Wait state
			goto end;
		}

		ReadData();  //Read the vibration Sensor data



#ifdef RS485_SERIAL
		sprintf(dataBuffer,"{\"Vibration,VID\": \"%s\", \"x\": \"%s\", \"y\": \"%s\", \"z\": \"%s\" }\n\r",charDeviceID,ucXaxisBuffer,ucYaxisBuffer,ucZaxisBuffer);
#endif
		UART_PRINT("{\"Vibration,VID\": \"%s\", \"x\": \"%s\", \"y\": \"%s\", \"z\": \"%s\" }\n\r",charDeviceID,ucXaxisBuffer,ucYaxisBuffer,ucZaxisBuffer);
		sl_ExtLib_MqttClientSend((void*)local_con_conf[iCount].clt_ctx,      //MQTT Send Function
				pub_topic,(char *)dataBuffer,strlen(dataBuffer),QOS2,RETAIN);

		osi_Sleep(50);			//50ms delay
	}

	end:
		lRetVal = CheckInternetConnection();
		//Wait Untill AP/Internet connection is established
		while(lRetVal < 0)
		{

			IS_IP_ACQUIRED(g_ulStatus);
			IS_CONNECTED(g_ulStatus);
			lRetVal = CheckInternetConnection();

			MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
			osi_Sleep(12000);
			MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
			osi_Sleep(3000);

			cReconnectCntr++;		//Count no of attempts of reconnection

			if(boolSmartConfig)
			{
				lRetVal = 1;
				lRetVal = sl_WlanProfileDel(0xFF);
				sl_WlanDisconnect();
				boolRestart=true;
				boolSmartConfig = false;
				goto ReconnectNow;
			}

			if( cReconnectCntr > 8 )		// 1 count = 15 secs therefore 4 count = 1 mins
			{
				cReconnectCntr = 0;
				UART_PRINT("re-connection State \n\r");
//				PowerCycleDevice();	// soft resets the device - data is lost
				goto end;
			}

		}

		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		local_con_conf->is_connected = true;
		boolBrokerDisconnected = false;
		//If AP connection is sucessfull jump to reintiate MQTT broker Connection
//		PowerCycleDevice();
		goto connect_to_broker;
}

如果我错过了一些东西,请告诉我。 请提前感谢。

   

正在寻找快速响应。

Shivaprasad。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    请检查 SL_ExtLib_MqttClient...的返回代码 API。 同时检查 MQTT 断开回拨是否被触发。

    您是否收到任何 Simplelink 异步事件(例如通过 SimpleLinkSockEventHandler)?

      

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好,

    我观察到 SL_ExtLib_MqttClientSend ()返回-7值,之后在重新连接状态下连续返回-4,请帮助我了解这些返回代码,此外,任何链接或文档引用都将有帮助,因为我没有获得任何引用。 为了供您参考,我还附上了下面的 UART 日志。  

    链接: https://shalakacom-my.sharepoint.com/:u:/g/personal/shivaprasad_desai_shalaka_com/EdOo7qD2KNNGgwgAAOdEGHcBww2Co6zuLGqPzpBzxfFqHQ?e=yMjWZ7

    请提前感谢。

    Shivaprasad

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    检查 mqtt_common.h 中的错误代码(另请参阅下文)。

    -7表示 在呼叫“发送”时,我们没有连接。

    -4表示参数有问题。 这可能是一个错误(如果您使用的参数与以前相同),但您可以在断开回拨中将 MQTT 上下文(或其他参数)设置为空(即  在 SlMqttClientCbs_t 结构中的 SL_ExtLib_MqttHttDisconn,您可以在 SL_ExtLib_ClientCtxCreate 中注册)。  

    根据日志,您似乎由于 MQP_ERR_NETWORK (套接字错误)而断开连接。

    如果您提供 NWP 日志(请参阅 /CS-file/__key/communityserver-discussion-components-files/968/CC3100-_2600__CC3200-Capture-NWP-Logs__2D00__德州 仪器(TI)- Wiki .pdf)或空气嗅探器日志(例如 Wireshark),我们将能够帮助找到根本原因。

    #define MQP_ERR_NETWORK (-1)/**<网络问题(sock err)*/
    #define MQP_ERR_TIMEOUT (-2)/**<网络事务已超时*/
    #define MQP_ERR_NET_OPS (-3)/**<平台网络操作不可用*/
    #define MQP_ERR_FNPARAM (-4)/**<提供的参数无效*/
    #define MQP_ERR_PKT_AVL (-5)/**<池中没有可用的端口*/
    #define MQP_ERR_PKT_LEN (-6)/**< pkt 中的可用缓冲区不足*/
    #define MQP_ERR_NOTCONN (-7)/**< Lib 未连接到服务器*/
    #define MQP_ERR_BADCALL (-8)/**< LIB 状态的不相关呼叫*/
    #define MQP_ERR_content (-9)/**< MSG /数据内容有错误*/
    #define MQP_ERR_LIBQUIT (-10)/**<需要重新启动库退出*/
    #define MQP_ERR_REMLSTN (-11)/**<套接字*/无远程监听程序

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好,

    将获得 NWP 日志并尽早更新您。

    我遇到了一个理想的函数,代码在该函数中连续执行此函数。

    该函数为 void idle_loop();

    当代码执行此空闲循环时,我是否错过了某些内容或任何特定原因?

    请参阅以下 UART 日志以供参考。   

    链接: https://shalakacom-my.sharepoint.com/:u:/g/personal/shivaprasad_desai_shalaka_com/EdOo7qD2KNNGgwgAAOdEGHcBww2Co6zuLGqPzpBzxfFqHQ?e=eJUktf

    Shivaprasad。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我不确定您使用“idle_loop”是指什么。

    我会等待您的 NWP 日志。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好,

    我的意思是,Idle.c 文件中的 Idle_run()函数将持续运行,当设备未在 MQTT 服务器上发布数据但连接到 Wi-Fi 时,就会发生这种情况。 您是否知道为什么会发生这种情况?

    请在下面找到 NWP 日志。

    链接: https://shalakacom-my.sharepoint.com/:u:/g/personal/shivaprasad_desai_shalaka_com/Efl8E3h1f75NhfBw9iCYBCcBhE6lKJ-KqPJlxcsaOCQmwA?e=eyGwr3 

    请提前感谢。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    是否应该在记录 NWP 日志时发生套接字错误?

    因为我没有发现任何问题。

    我确实看到堆栈以非常高的频率从插槽读取。 您真的每秒收到数百条消息还是超时? 您在  SL_ExtLib_MqttClientInit()中设置的“res_time”是什么?

    在哪里可以找到 idle.c? 我在 SDK 中找不到这样的文件。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我确实看到堆栈以非常高的频率从插槽读取。 您真的每秒收到数百条消息还是超时?  

    ->这是设备连接到网络时连续发送数据的应用程序要求

    您在  SL_ExtLib_MqttClientInit()中设置的“res_time”是什么?

    -> 30 (秒)

     

    在哪里可以找到 idle.c? 我在 SDK 中找不到这样的文件。

    ->这与蒂尔托斯有关。  

    • 版本: tirtos_cc32xx_2_15_00_17
    • 空闲文件位置: C:\ti\tirtos_cc32xx_2_15_00_17\products\BIOS_6_45_00_20\packages \ti\SysBIOS\KNL

    请查看以下最新日志以供参考

    链接: https://shalakacom-my.sharepoint.com/:u:/g/personal/shivaprasad_desai_shalaka_com/EYq7DlTWNJJFjmK4LNvgtV8BFBldlrInp0MHR4H_YUJzxw?e=fb1LtA

    Shivaprasad  

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    好的,所以你指的是 tirtos 空闲任务。  

    基本上,您不应该使用此功能(您可以创建自己的低优先级任务)。 我仍然不理解您在这项任务中要实现的目标。

    事件频率可能表明 MQTT 不是使用案例的首选协议,

    无论如何,我们仍然没有找到发布失败的原因。 当您记录 NWP 日志时,是否会发生这种情况?

    MQTT 重新连接是否可以解决此问题?

    巴西,

    科比