1191 views|6 replies

654

Posts

26

Resources
The OP
 

49 "Ten Thousand Miles" Raspberry Pi Car——PicoW Learning (C Language UDP Communication) [Copy link]

 

UDP communication has the advantage of high speed. There are routines for reference for TCP communication, but there are none for UDP communication.

Target

Create a new "echo" routine project for the PicoW module based on the UDP protocol and STA mode. You can communicate with PicoW through a mobile phone and print out relevant information through the serial port.

New Construction

Refer to the project creation method in the previous post , use the "pico project generato" tool, name the project "Test_IwIP_UDP", check "Console over UART" and "Background IwIP".

Coding

The previous work has completed the construction of the project framework. The main code is posted below.

Add header file

#include"lwip/udp.h"

Add definitions and global variables

Mainly the WIFI username and password, where XX needs to be replaced with your own, communication port number and PCB.

#defineWIFI_SSID "XXXXXXXXXXX"
#defineWIFI_PASSWORD "XXXXXXXXX"
#defineRCV_FROM_PORT 1234
structudp_pcb*rcv_udp_pcb;

Main function writing

The main tasks are to initialize the chip, connect to the WiFi router, bind UDP, register the receive callback function, and flash the LED. UDP does not have as many functions as TCP, such as successful connection, successful sending, etc., and there is no callback function.

intmain()
{
  staticboolLED_State=0;
  stdio_init_all();
  puts("Test IwIP UDP");
  if(cyw43_arch_init()) {
    printf("Wi-Fi init failed");
    return-1;
  }
  cyw43_arch_enable_sta_mode();
  printf("Connecting to Wi-Fi...\n");
  if(cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
    printf("failed to connect.\n");
    return1;
  } else{
    printf("Connected.\n");
  }
  rcv_udp_pcb=udp_new();
  err_terr=udp_bind(rcv_udp_pcb,IP_ADDR_ANY,RCV_FROM_PORT);
  udp_recv(rcv_udp_pcb, RcvFromUDP,NULL);
  while(true) {
    sleep_ms(100);
    if(LED_State){
      LED_State=false;
    }else{
      LED_State=true;
    }
    cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, LED_State);
  }
  return0;
}

Writing receiving callback function and sending function

In the receiving function, the sender's related information is printed out, and the received information is sent. In theory, UDP cannot confirm whether the two are connected successfully, nor can it send a "connection successful" message to the Client after the connection is successful like TCP. Only when the information is received can we know who sent the information and then send it back.

voidSendUDP(constip_addr_t*dst_ip, intport, void*data, intdata_size)
{
   structpbuf*p=pbuf_alloc(PBUF_TRANSPORT,data_size+1,PBUF_RAM);
   char*pt=(char*) p->payload;
   memcpy(pt,data,data_size);
   pt[data_size]='\0';
   cyw43_arch_lwip_begin();
   udp_sendto(rcv_udp_pcb,p,dst_ip,port);
   printf("send -> %s\n", pt);
   cyw43_arch_lwip_end();
   pbuf_free(p);
}

voidRcvFromUDP(void*arg, structudp_pcb*pcb, structpbuf*p, constip_addr_t*addr,u16_tport)
{
char*buffer=calloc(p->len+1,sizeof(char));// use calloc to >
  strncpy(buffer,(char*)p->payload,p->len);
  printf("received from %d.%d.%d.%dport=%d\n",addr->addr&0xff,
                       (addr->addr>>8)&0xff,
                       (addr->addr>>16)&0xff,
                       addr->addr>>24,port);
  printf("Length = %d, Total Length = %d\n",p->len,p->tot_len);
  printf("payload -> %s\n", buffer);
  free(buffer);
  pbuf_free(p);
  SendUDP(addr,port,buffer,p->len);
}

running result

Run the "lb_socket_Android" APP on your phone. This is the UDP communication software I wrote myself. It is introduced in the previous post and downloaded at the end. Use the method in the previous post to know the IP address of PicoW, then enter the port number and click Connect. Edit the data to be sent, click Send, and you will see the same data received.

Connect to the serial debugging assistant. On the serial debugging assistant, there will be information about the entire WiFi connection process, such as the relevant information printed by the receiving callback function.

APP source code and writing: socket learning (Android sending and receiving)

PicoW source code: Test_IwIP_UDP.zip (1.57 MB, downloads: 1)
This post is from Innovation Lab

Latest reply

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.   Details Published on 2023-11-21 14:13
Personal signatureQQ:252669569
 
 

686

Posts

0

Resources
2
 

Thanks for sharing. I see that the serial port debugging assistant we both use is the same.

This post is from Innovation Lab
 
 
 

2

Posts

0

Resources
3
 

Awesome, I learned a lot

This post is from Innovation Lab
 
 
 

6841

Posts

11

Resources
4
 
Dear moderator, this series of updates is so persistent. We need to learn a lot from you!
This post is from Innovation Lab
 
 
 

409

Posts

7

Resources
5
 

Is the TCP test that this module can complete realized by converting UART to TCP?

This post is from Innovation Lab

Comments

This is not the case. The PicoW is composed of a main control chip and a dual-mode Wifi+blue chip. The two are connected through the SPI interface. Compared with the module using UART conversion, this one is faster, more low-level, and requires more configuration.  Details Published on 2023-11-18 21:33
 
 
 

654

Posts

26

Resources
6
 
lansebuluo posted on 2023-11-15 08:49 Is the TCP test that this module can complete realized by converting UART to TCP?

This is not the case. The PicoW is composed of a main control chip and a dual-mode Wifi+blue chip. The two are connected through the SPI interface. Compared with the module using UART conversion, this one is faster, more low-level, and requires more configuration.

This post is from Innovation Lab
Personal signatureQQ:252669569
 
 
 

731

Posts

4

Resources
7
 

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.

This post is from Innovation Lab
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list