Send and Receive data from the WebServer using STM32
I have already covered how to create a webserver using ESP8266 and STM32. In that tutorial, we controlled the LED on STM32 using the webserver on ESP8266. This tutorial will take a step forward on the same aspect, and today we will see how to send and receive actual data from the webserver.
Although I will demonstrate a small use of it, but you can use it for a wide range of purposes. For eg- sending the sensor values, or receiving some form data, etc. This tutorial will use the Ringbuffer and you should take a look at that article first.
************* UPDATE 2 ***************
L4 series and G series MCUs have some issues with UART_ISR_TXE. If you are having issues even after making changes according to UPDATE 1, then Replace the Uart_isr with the file https://controllerstech.com/wp-content/uploads/2020/08/Uart_Isr_single_new.c
************* UPDATE 1 ***************
Those, who have errors in the Uart_isr function, eg- F7 series MCU, replace the Uart_isr functon with the one provided in this file https://controllerstech.com/wp-content/uploads/2020/04/Uart_Isr_single.c
The connection is simple. Just connect the Rx of the MCU to the Tx of the ESP, and vice versa.
All we need to do is just enable a UART. Below is my Cubemx setup. I am using baud rate of 115200 and make sure that the interrupt is enabled.
Next, copy the required header files and source files in the inc and src directories respectively. These files are located under /src and /inc folders of the code attached below. You should also copy them to the same locations in your project.
Once copied, open the stm32….it.c file and copy
extern void Uart_isr (UART_HandleTypeDef *huart); in the file. And at last we need to replace the default ISR with the one we have. So browse down the file to the
void USART1_IRQHandler(void) and replace the default ISR just as shown in the picture below
This completes the setup part Now let’s take a look inside some functions available.
Some Insight into the CODE
ESP_Init (“SSID”, “PASSWD”, “STAIP”) Initializes the ESP8266. Its parameter are the @SSID and @PASSWD of the Access point that you want to connect to. @STAIP is the static IP that you want to assign to this connection.
This function sends some AT commands in a sequence, and than waits for the expected response (OK\r\n) after every command. These commands are given below
- AT –> to check if the ESP is responding
- AT+CWMODE=1 –> to set the ESP into the station mode
- AT+STAIP = “STAIP” –> to set the static IP for the connection
- AT+CWJAP=”SSID”, “PASSWD” –> Join the access point with the SSID, and PASSWD
- AT+CIPMUX=1 –> Set the multiple connection as TRUE
- AT+CIPSERVER=1,80 –> Starts a server at port 80
Next, we will connect to this IP address, but make sure the device and the ESP should be connected to the same network. The Webserver page will look like as shown below
- Server_Start() checks the incoming connection, and look for the request made by the browser. If the request is about page1, or page2, or the home.
- If the request is made for the page1 (submit), it will first store the data entered into the structure (user), and then call the server_handle to handle that request
- if the request is about page2 (View Data) or home, then the server handle is called directly to handle these requests
- Server_Handle is to handle the requests made by the browser. It sends the required HTML code to the server
- In order to handle the request for page2 (View Data), it will first format the data from the structure (user) along with some HTML table syntax, and then send this data to the server
- For any other request, it will simply send the respective HTML code (defined in the beginning of ESP_DATA_HANDLER.c file) to the server.
The main function
- In the main function, we will simply initialize the ESP with the SSID, PASSWORD, and the IP address that we need to set.
- And call the server_start in the while loop
Below are the pictures for all the three pages