Remote control of WiFi enabled STM32Nucleo with ESP32 using a browser
The following is an explanation of the method of operation. If you can remotely control MCU from a browser, your application will be much more versatile.
Table of contents
When a TCP server with WiFi enabled STM32 is built with ESP32, it can be remotely controlled by TCP communication using terminal software from PCs, smartphones, and other devices. Here I will explain how to access and remotely control Nucleo (STM32 MCU) from a web browser instead of terminal emulator.
This time, ESP32 is set up as a WEB server by sending AT commands from a serial terminal on a PC, so it is almost the same as with a TCP server up to the point where the server is built using AT commands for both sending and receiving. To access a web server on the web, set ESP32 as a client.
WEB Server Setting
When setting up a TCP server, first set it to a mode that can support multiple connections (multi-connection mode). The command AT+CIPMUX=1 is "1": multi-connection, "0": single-connection mode. ESP32 defaults to single connection mode, so be sure to execute this command.
When the IP address of the web server is specified in the web browser and accessed, the serial terminal will display the following message. The port number may be omitted if it is 80.
Transmission operation by AT command
Now execute the AT command to send the string "Hello" to the web browser side.
To the browser, the HTTP protocol HTML string is appended to "<H1>Hello</H1>" for a total 14 characters.
After sending the AT command AT+CIPSEND=0,14, if the command is accepted, "OK" is returned and the > prompt is displayed waiting for your input. Enter the 14 characters <H1>Hello</H1> to be sent to the browser.
Success is indicated when the screen changes in the web browser and the string "Hello" appears as shown below. If you want to continue sending the string, repeat the AT+CIPSEND command in the same way.
To exit, close the browser or execute the AT command "AT+CIPCLOSE=0." 0" is the link ID.
Receive operation by AT command
Let's look at the case where a string sent from a web browser is received by a serial terminal. In this case, simply enter the address of the browser followed by "/" and the string you wish to send.
What is different from TCP communication is that when a browser is connected, if an IP address containing a code string is specified and updated, communication is once again broken and then reconnected with the updated IP address. At that time, ESP32 returns a connection status response and then sends a string starting with +IPD to the terminal side.
For communication via the HTTP protocol, many characters are appended to the response. In the reception by the web server, there is a string of characters immediately after the "/", and this part should be extracted as the reception code.
Remote control of MCU via web browser with ESP32
So far, we have been sending and receiving character strings from serial terminal software on a PC to ESP32 by exchanging AT commands. Let's try remote operation via a web browser by exchanging AT commands with another execution environment, MCU.
The ability to remotely control microcontrollers from a browser will greatly expand the range of applications. This is the very epitome of IoT, so please learn to use it and develop it.
Transmission operation from microcontroller to web browser
Now that we can send AT commands from a terminal emulator on a PC to send strings to a web browser, the next issue is to replace the terminal emulator with MCU (STM32) to send strings to a web browser.
The program contents are basically the same as for TCP communication, with the difference that the port number to generate the server is set to 80 for the WEB server. In addition,<H1></H1> is added before and after the string in the transmission string in accordance with the HTTP protocol.
As for transmission, it is not much different from the case of a TCP server.
This sample displays periodically increasing numbers in the browser at 1.5 second intervals. The program is introduced in "Send from WiFi enabled STM32 with ESP32 to web browser".
Receiving operations on MCU from a web browser
This time, a string is sent from the web browser to the microcontroller (STM32) side.
When a web browser sends the string "ABC@", ESP32 returns the response "0,CLOSED", "0,CONNECT" to connect or reconnect with the browser. This is followed by
+IPD,x,xxx:GET /ABC@ HTTP/1.1xxx… is returned to MCU as a response, and only ABC in this is extracted. Again, @ is appended to the command to indicate the end of the string.
It is important to note that the HTTP protocol communication between ESP32 and the browser generates various responses on occasion.
It is more reliable to use an interrupt rather than a polling process for the receive process in MCU to keep track of the timing of the response from ESP32.
Since ESP32 returns various responses in addition to the code you want to send in the HTTP protocol, it is important to know in advance what kind of response you will receive and at what timing.
MCU operation from a browser is possible by entering a string code registered in advance, including the IP address, and dividing the process by matching or mismatching the code in MCU program.
This sample program compares the code sent from the browser with the registered code and conditionally branches the process according to the match or mismatch. In the code acquisition process, only the necessary codes are extracted from the response string from ESP32.
After processing the conditional branch, USART3 is returned to its initial state to prepare for the next interrupt. If the registers of USART3 are not cleared in some way, the USART may not respond.
This sample outputs an LED (PA5) to confirm when a receive interrupt is received. The program is introduced in the section "Operating the STM32 with ESP32 WiFi from a browser".
In MCU programs for ESP32 operation, the trick is to allow sufficient latency before and after AT commands. The baud rate and other factors will vary depending on the environment, but it is recommended that a large baud rate be set first.
When using the ESP32 with the HTTP protocol, the response is always returned, but sometimes it can be quite annoying: if there is no problem in communication between the ESP32 and the terminal, but it does not work with MCU, it is most likely due to insufficient response processing or waiting time. It is important to check what kind of response you get from ESP32 when you give commands with terminal software, one by one, so that you do not miss anything.