reading from tcp stream in WINC1500

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello.  I'm trying to build an MQTT client on the SSL connect example.  I've been able to connect and publish, but have not been successful in implementing recv() as described in the documentation.  I'm hoping someone can summarize how I should go about using the socket_cb to wait for new data from my MQTT broker and get that data from the buffer.  I am currently calling the recv() function after connection as suggested in the documentation, then calling recv() after every call to send().  I'm really at my wit's end here, so any help you can offer would be greatly appreciated.

 

My implementation is as follows:

case SOCKET_MSG_CONNECT:
{
    //point to buffer with recv structure type for use in recv()
	tstrSocketRecvMsg* pstrRcv = (tstrSocketRecvMsg *)pvMsg;
	
	//Point to buffer with connect structure
	tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg *)pvMsg;
	if (pstrConnect && pstrConnect->s8Error >= SOCK_ERR_NO_ERROR) 
	{
		printf("Successfully connected.\r\n");
		
		//call recv() as suggested in documentation
		recv(sock, pstrRcv->pu8Buffer, pstrRcv->s16BufferSize, 10000);
		
		//connect to broker (send() is called on the last line of this function)
		Mqtt_Connect(sock);	
		
		//call recv() to prepare buffer for broker response
		recv(sock, pstrRcv->pu8Buffer, pstrRcv->s16BufferSize, 10000);																					} 
	else 
	{
		printf("Connect error! code(%d)\r\n", pstrConnect->s8Error);
		gu8SocketStatus = SocketError;
	}
}

The following is pretty much directly from the documentation.

case SOCKET_MSG_RECV:
{
	tstrSocketRecvMsg* rcvMsg = &pvMsg;
	if(rcvMsg->pu8Buffer && rcvMsg->s16BufferSize)
	{
		printf("socket_cb: received app message. (%u) \r\n", *rcvMsg->pu8Buffer);
		Mqtt_Callback(sock, rcvMsg->pu8Buffer);
		recv(sock, rcvMsg->pu8Buffer, rcvMsg->s16BufferSize, 10000);
	}
	else
	{
		printf("socket_cb: msg receive error!");
	}
}											
break;

 

This topic has a solution.
Last Edited: Fri. Mar 2, 2018 - 01:40 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got this sorted out. I had a malformed data packet causing the server to disconnect me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jolly good.

 

Now please mark the solution: https://www.avrfreaks.net/comment...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry about that.  I've done so.