Skip to content

Contributed by WangChong, curated by Ai-Thinker

Hello everyone. Today I'll mainly share how to connect to WiFi and how to send GET and POST requests.

I've been researching this for many days. Today I finally made a little progress. The main problem is that I haven't studied FreeRTOS. I don't understand how to set up task scheduling or how to queue tasks so the next task runs after the previous one completes. First, today's example project is at examples\wifi\sta\wifi_http. Let's open wifi_http_client.c first, as shown below.

What is this shell command about? When does it accept user commands? To understand this, we first need to look at the methods in main.c.

In main.c, the UART0 peripheral is obtained and the serial input is bound to the shell command input. That way, whenever the serial port receives data, the custom command in the first screenshot is called.

Let's go back to wifi_http_client.c. Here, after receiving a serial command, it parses it. If the length of the passed command array is less than 2, the command is invalid, and it responds with a prompt telling you the command format.

So how are POST and GET requests sent? Please refer to the figure below. I have personally tested that POST and GET requests can be sent. The command to send GET or POST is wifi_http_test jsonplaceholder.typicode.com [wifi_http_test host address]; the path is defined in the code below.

If you can't find a public API, you can use this website:

  1. https://jsonplaceholder.typicode.com/

Connecting to WiFi OK, now let's talk about how to connect to WiFi. The first way is like above: connect to WiFi by receiving data over the serial port. The WiFi connection command is wifi_sta_connect ywlim22@unifi v6%+Nt8M. Another way is to use shell_exec in the code to send the WiFi connection command to itself to connect WiFi.

This way works, but I couldn't find where the wifi_sta_connect command is bound to the command line.

After connecting to WiFi, you can send requests.There is one problem: you cannot use another shell_exec to call the above command for sending POST and GET requests.If you call it, the shell_exec will execute incorrectly, treating the POST/GET request command below as a WiFi connection command. I can't find the reason for now.

Another important point: in wifi_http_client.c, the wifi_test_http_client_init() method is only used to send POST and GET requests (it may support other request types, but I haven't tried).Don't try to use this method to call the WiFi connection command.What I haven't solved here is how to send POST and GET requests programmatically based on this example. I have a solution, but I can't implement it because I haven't learned FreeRTOS.The solution is to write a task scheduler that calls the request after connecting to WiFi. I tried it myself, but the request is always sent as soon as the WiFi connection succeeds; since there's no IP address yet, it fails.I hope this demo gives everyone something to think about, and I also hope someone can share a better way with me. Thanks!

Please adapt the code to your own environment, or just copy Main.C and wifi_http_client.c.

Attachments:

wifi_http.zip(11.06 KB, downloads: 15)

Have questions?

For other questions, please visit the unified discussion area: Ai-Thinker Discussions

Released under the MIT License. Build Time 2026-09-11 14:52:23