STM32 ETHERNET #3. UDP CLIENT

This it the third tutorial in the STM32 Ethernet series, and today we will see how to create UDP client using STM32. I have already covered a tutorial about UDP SERVER, you can check it out here.

The hardware connection will be similar to the one I have used in the first video, and you can check it out here.

Some Insight into the CODE

Connect the Client to the server

void udpClient_connect(void)
{
	err_t err;

	/* 1. Create a new UDP control block  */
	upcb = udp_new();

	/* Bind the block to module's IP and port */
	ip_addr_t myIPaddr;
	IP_ADDR4(&myIPaddr, 192, 168, 0, 111);
	udp_bind(upcb, &myIPaddr, 8);


	/* configure destination IP address and port */
	ip_addr_t DestIPaddr;
	IP_ADDR4(&DestIPaddr, 192, 168, 0, 102);
	err= udp_connect(upcb, &DestIPaddr, 7);

	if (err == ERR_OK)
	{
		/* 2. Send message to server */
		udpClient_send ();

		/* 3. Set a receive callback for the upcb */
		udp_recv(upcb, udp_receive_callback, NULL);
	}
}
  • We are going to use the following steps to connect the UDP client to a server
  • First of all we will create a new UDP control block, using udp_new ()
  • Next I am binding the control block to the local IP and port, using udp_bind ()
  • Also connect the control block to the server IP and port, using udp_connect()
  • Then we will send some data to the server, and set a receive function, which will be called, when the server sends some data to the client.

The Receive callback

void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
	/* Copy the data from the pbuf */
	strncpy (buffer, (char *)p->payload, p->len);

	/*increment message count */
	counter++;

	/* Free receive pbuf */
	pbuf_free(p);
}

The receive callback is called, when the client receives some data from the server. Let’s see ho am I handling this function

  • here I am copying the data sent by the server into the buffer.
  • Although I am not making any use of this data, but you can utilize it as per your requirement.
  • then increment the counter. This counter will be used when we will send the data.
  • Finally free the packet buffer.

Sending Data to the Server

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	udpClient_send();
}

static void udpClient_send(void)
{
  struct pbuf *txBuf;
  char data[100];

  int len = sprintf(data, "sending UDP client message %d", counter);

  /* allocate pbuf from pool*/
  txBuf = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);

  if (txBuf != NULL)
  {
    /* copy data to pbuf */
    pbuf_take(txBuf, data, len);

    /* send udp data */
    udp_send(upcb, txBuf);

    /* free pbuf */
    pbuf_free(txBuf);
  }
}
  • Here I have also created a periodic timer, which will generate an interrupt every 1 second.
  • The data will be sent to the server in the timer callback, and hence every 1 second.
  • To send the data to the server, first of all we will create a packet buffer, txBuf
  • Then we will allocate the memory for this packet buffer
  • pbuf_take will be used to copy the data into the packet buffer.
  • And finally we will send the data using the udp_send function.





Result

Check out the Video Below










Info

You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

2 Comments. Leave new

  • Hi, thank you for this demo. It works for me fine except the receive callback is not called by the program. It sends udp data to the server once per second, but when I send some data from the server, it even does not go into the callback function. I am using hercules and I disabled all other LAN adapters and firewall. Any idea where could be the problem?

    Thank you.

    Reply
  • Mahmut Kocaman
    August 12, 2021 12:26 PM
    Hi, I've looked through the threads. Again, very informative and instructive. Thanks for your enthusiasm. When will you share TCP server and client topics?
    
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×