STM32 ETHERNET #3. UDP CLIENT
This is 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