Master The Pico WiFi: UDP For Speed |
Written by Harry Fairhead & Mike James | |||
Monday, 27 November 2023 | |||
Page 2 of 2
To send data we need to use a PBUF of the correct type and size and store the data in its payload field: char message[] = "Hello UDP World"; struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, strlen(message)+1, PBUF_RAM); There are many ways of transferring the data into the payload field but using snprint has the advantage of being able to incorporate data from different sources. Finally we can send the datagram: err_t er = udp_send(pcb, p); pbuf_free(p); Notice that we have to free the PBUF and you cannot reuse it to send another payload as it has been replaced by the UDP packet that has been sent. The complete program is: #include <stdio.h> #include "pico/stdlib.h" #include "pico/cyw43_arch.h" #include "lwip/pbuf.h" #include "lwip/udp.h" #include "setupWifi.h" int main() { stdio_init_all(); connect(); struct udp_pcb *pcb = udp_new(); udp_bind(pcb, IP_ADDR_ANY, 8080); ip_addr_t ip; IP4_ADDR(&ip, 192, 168, 11, 101); udp_connect(pcb, &ip, 8080); char message[] = "Hello UDP World"; struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, strlen(message) + 1, PBUF_RAM); The lwipopts.h and CmakeLists.txt file are the very simplest. The define: #define LWIP_UDP 1 has to be included in lwipopts.h for the UDP library to be compiled but this is in the default lwipopts.h file from the Pico examples. There is also a udp_sendto(pcb,p,ip,port) function which can be used to send a packet to the machine specified by ip and port without making any changes to the PCB. There is one special use of UDP, DHCP (Dynamic Host Configuration Protocol), which requires sending a datagram to a machine even though the current network connection doesn’t yet have an IP address of its own. To do this you can use the two special functions: udp_sendto_if(pcb,p,dst_ip,dst_port,netif) and udp_sendto_if_src(pcb,p,dst_ip,dst_port,netif, src_ip) These send a datagram to the destination ip and port number using the netif interface. The second function also includes the specified source ip address in the datagram even though the interface might not have an IP address yet. Notice that you can have multiple PCBs active at any given time and can use any of them to send a UDP packet to a given IP address and port. The PCBs are managed in a linked list and you can use: udp_remove(pcb) to remove, and hence effectively deactivate and free, the pcb. A Python UDP ClientIt can be difficult to test UDP programs without a suitable client or server to connect to. Using Python it is very easy to create a UDP client which can be used to test UDP servers: import asyncio class ClientDatagramProtocol(asyncio.DatagramProtocol): def datagram_received(self, data, addr): message = data.decode("utf8") print("Received",message,"from", addr) async def main(): loop = asyncio.get_running_loop() transport, protocol = If you want to know more this program is from Programmer’s Python: Async by Mike James, ISBN: 978-1871962595. In chapter but not in this extract
Summary
Master the Raspberry Pi Pico in C:
|
|||
Last Updated ( Monday, 27 November 2023 ) |