Skip to playerSkip to main content
Learn how to make the STM32 board connect to WiFi using the ESP-01 (ESP8266 module) and obtain its IP—quick, clear steps ideal for STM32CubeIDE projects. Full STM32 + ESP8266 wiring, CubeMX setup, and testing included.

► Download resources & code: [https://controllerstech.com/stm32-esp8266-wifi-ip/]

Next videos will show how to build TCP connections and send sensor data to the cloud!

STM32 Playlist :::: https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd

#STM32 #ESP8266 #WiFiModule #STM32CubeIDE #IoT #Embedded

Facebook Page : https://www.facebook.com/controllerstech

Telegram Group : https://t.me/controllerstechdiscuss

Instagram : https://www.instagram.com/controllerstech/

For more info, visit https://controllerstech.com

Category

🤖
Tech
Transcript
00:00Hello, and welcome to Controllers Tech.
00:12Today, we are beginning a brand new mini-series focused on STM32 IoT projects using the ESP8266 Wi-Fi transceiver module.
00:23In this series, we will explore step-by-step how to connect the STM32 to a Wi-Fi network using the ESP8266,
00:32how to send sensor data to a cloud server, and how to control STM32 GPIOs directly from a web dashboard.
00:40Along the way, we will also implement the MQTT protocol, and towards the end,
00:45we will even try out STM32 over-the-air, OTA, firmware updates through the ESP8266.
00:54This is going to be a very exciting set of videos, spread across the next couple of months,
01:00and it will give you a strong foundation in building IoT applications with STM32.
01:05In today's first video of the series, we will start with the basics.
01:09We will look at how to configure STM32Cube, how to make the hardware connection between STM32 and ESP8266,
01:20and finally, how to use simple-it commands to connect the ESP8266 to a Wi-Fi network.
01:27For this entire series, I will be working with the STM32F446 development board from WIAC Studio.
01:35If you are based in India, you can also purchase this board directly from shop.controllertech.com.
01:42On the Wi-Fi side, I will be using the ESP8266-01 serial Wi-Fi module.
01:50This module is quite compact, but it comes with multiple pins,
01:55which can make direct connections to the development board a bit inconvenient.
01:58To solve this issue and simplify the setup, I'll be using an ESP8266 adapter.
02:06This adapter reduces the number of pins from 8 down to just 4,
02:11leaving only TX, RX, VCC, and ground.
02:15This makes it much easier and cleaner to connect the module with the development board.
02:20All the links to the components I am using in this video
02:24are provided in the article mentioned in the description below.
02:27If you would like to support the channel, you can do so by purchasing through these affiliate links.
02:33It really helps us continue making more content like this.
02:36Before we begin working on the STM32 project,
02:40the first step is to make sure that our ESP8266 Wi-Fi module is responding correctly to AT commands.
02:48To test this, we will connect the ESP8266 to any USB-to-TTL converter,
02:56which will then be connected to the computer.
02:59The wiring is very straightforward.
03:01Connect the pins in a cross fashion.
03:04The TX pin of the ESP8266 goes to the RX pin of the converter,
03:09and the RX pin goes to the TX pin of the converter.
03:13If you're using the ESP01 adapter, you should provide it with a 5V power supply,
03:18since the adapter already has an on-bore voltage regulator that steps it down to 3.3 volts,
03:24which is the operating voltage of the ESP8266.
03:29However, if you're connecting directly to the ESP8266 module without the adapter,
03:36make sure you supply 3.3 volts only, otherwise you may damage the module.
03:41Once the connections are ready, open any serial terminal software and select the correct USB-TL port.
03:49Now, the ESP8266 modules usually have different baud rates.
03:55I'm not sure which baud rate this one is set to,
03:58but by default, many modules are programmed with 9,600 BPS, so we will start by trying that.
04:05When sending AT commands, remember, they must be followed by both carriage return and line feed characters.
04:13Some serial terminal programs allow you to automatically add this tail,
04:17but if yours doesn't, you can manually include it in the command itself.
04:22Let's begin by sending a simple AT command.
04:25The command was transmitted successfully.
04:28The send count shows four bytes, two for the command and two for the tail.
04:32However, we did not receive any response from module.
04:37At this stage, the first thing you should do is check your wiring and the power supply to ensure everything is correct.
04:44In my case, the connections are fine, so I'll try changing the baud rate to 38,400 BPS and send the command again.
04:52This time, we receive some data back.
04:55That means the module is alive, but clearly the baud rate is not yet correct.
04:59Next, I'll try another common baud rate, 57,600 BPS.
05:06After sending the AT command at this rate, we received six bytes, but still not the proper response we were expecting.
05:13Now, let's switch the baud rate to 115,200 BPS and test again.
05:19This time, we received the correct response from the ESP module.
05:23It replied with OK for the AT command.
05:27That confirms the module is configured to operate at 115,200 BPS.
05:33It's possible that I had reconfigured this in one of my earlier projects, since most ESP8266 modules originally come preset to 9,600 BPS.
05:45With the baud rate confirmed, let's try out a few more AT commands.
05:50AT plus GM are as used to check the firmware version.
05:54As you can see, the ESP module responds with its AT version and SDK version details.
06:01Notice that every valid response ends with OK.
06:04This is important because later, in our STM32 code, we can use that as a marker to confirm successful command execution.
06:14AT plus CW mode is used to check the current Wi-Fi mode of the ESP module.
06:20The response shows mode 1, which means the module is set to station mode, allowing it to connect to an existing Wi-Fi network.
06:29AT plus reset can be used to restart the module.
06:32As you can see, the ESP module reboots successfully.
06:38Now, in my case, the Wi-Fi is already connected.
06:41That's because during a previous test, I configured the network credentials, and the ESP8266 stored them in its internal flash memory.
06:51Because of that, it automatically reconnects during boot.
06:55So everything looks good.
06:56The module is working, responding properly to AT commands, and is ready to be used in our STM32 IoT project.
07:05I have already written the library files that will handle the processing of AT commands, and we'll be using them in the project later.
07:13You can download these library files from the article linked in the description of this video.
07:19Now, let's move to the STM32CubeIDE and create a new project.
07:25As mentioned earlier, I will be working with the STM32F446RE Development Board.
07:32Let's give the project a suitable name, and then click Finish to create it.
07:36We will begin by setting up the clock configuration.
07:39For this project, I am selecting the internal oscillator, and I'll configure the system to run at its maximum frequency of 180 MHz.
07:50Next, let's configure the UART that will be used to communicate with the ESP module.
07:55I am assigning UART4 for this purpose.
07:58The pins PA0 and PA1 will serve as UART4TX and RX, respectively.
08:04From our earlier test, we already know that the ESP module is communicating at a baud rate of 115,200, so we will configure UART4 with the same baud rate.
08:16Leave the rest of the UART settings at their default values.
08:20In addition to this, we need one more UART to view user interactions and to log debugging information.
08:27For this, I will use UART1.
08:29The pins PA9 and PA10 will act as UART1TX and RX.
08:36I will keep the baud rate for UART1 at 115,200 as well.
08:41That's all the configuration required in CubeMX for now.
08:45I will show the actual hardware connections in just a bit.
08:49For now, click Save to generate the project.
08:52The first thing we'll do inside the project is to copy the library files.
08:56Copy the C source file into the project source directory and a header file into the include directory.
09:04Let's take a closer look at the ESP8266STM32 header file.
09:10Here, we need to specify which UART is being used for the ESP module.
09:15If you're using a different UART than what I've shown, you can configure it here.
09:19I recommend keeping the user logs enabled because they display important information about the ongoing processes.
09:27If you encounter errors, you can then enable the debug logs, which will print detailed step-by-step messages showing exactly what led to the error.
09:36This makes troubleshooting much easier.
09:38Currently, the library provides three global functions.
09:44ESPunder ScoreNet.
09:45This initializes the ESP8266 module.
09:51ESPunder ScoreConnect Wi-Fi.
09:54This connects the module to a Wi-Fi network and retrieves its IP address.
09:58The parameters for this function are SSID and password of the Wi-Fi access point, an IP buffer to store the assigned IP address, and the buffer length, which defines the maximum size of that IP buffer.
10:14ESPunder ScoreGetConnection.
10:16This function allows us to check and retrieve the current connection status of the ESP8266.
10:22Now, let's go through how these functions are written inside the source file.
10:28We'll begin with the Initialization function.
10:31Inside this function, the first step is to restart the ESP module by sending the AT plus reset command.
10:39Just like we did earlier in the serial terminal, this command must end with a carriage return and line feed.
10:46After the command is sent, the module immediately acknowledges it with an OK response.
10:51However, the restart process itself continues for a few seconds in the background.
10:57To allow the module enough time to reboot properly, we add a delay of about 5 seconds after sending this command.
11:05If you do not want to restart the module every time, you can simply skip this part of the Initialization function.
11:12In that case, just add a small delay at the very beginning of the function.
11:16This short pause ensures that the ESP8266 does not receive commands while the STM32 code is still flashing.
11:25Once the restart is handled, we can begin the actual Initialization by sending the basic AT command.
11:32Here, we expect the module to respond with OK, which confirms that communication is working.
11:38If the response is valid, we then proceed to the next step.
11:43The next command we send is ATE0, which disables command echoing.
11:49Again, we wait for the OK response.
11:52Once this response is received, we consider the module successfully initialized.
11:58The second important function is ESP underscore connect Wi-Fi, which connects the ESP8266 to a known Wi-Fi network.
12:07The process starts by setting the module to station mode using the command AT plus CW mode equals 1.
12:16As always, we wait for the OK response to confirm that the command was accepted.
12:21Next, we send the command AT plus CWJP, along with the SSID and password of the Wi-Fi network as parameters.
12:31The module now attempts to connect to the access point.
12:35Since this process takes a little longer, we provide a timeout of 10 seconds.
12:41If the credentials are correct, the ESP8266 will respond with Wi-Fi connected.
12:48In general, the timeout values vary depending on the command being sent.
12:52I have set the default timeout to 2 seconds, but you can adjust this based on your needs.
12:58Once the module is connected to the Wi-Fi, we call a static function called GET IP to retrieve the module's IP address.
13:06Inside the GET IP function, we send the AT plus C IFS or command and wait for the response.
13:15Since my module is already connected to Wi-Fi, let's try this command directly.
13:19As you can see, the module responds with its IP address, MAC address, and an OK at the end.
13:27In this response, the IP address is associated with the label station IP.
13:32So in our code, we search for the keyword STAIP in the incoming buffer.
13:39Once we find it, we extract the IP address that appears immediately after it and store it for use in the project.
13:45Finally, let's look at how the send command function works, since this function is central to all communication.
13:53Inside send command, the command string is first transmitted to the ESP8266.
14:00After that, we begin reading the incoming data from the module one character at a time and store it in the Rx buffer.
14:07While capturing this data, the function continuously checks if the expected acknowledgement string, for example, OK, appears in the buffer.
14:17If the acknowledgement is found, the function returns success.
14:22And throughout this process, if debug logs are enabled, they will print detailed messages at each step of the function.
14:29This makes it very easy to follow what's happening inside the code.
14:34Now, let's move on and write the main file.
14:36First, include the ESP8266 STM32 header file.
14:44Next, we will define a custom write function.
14:47This function will route the printf output to UART1, so that all log messages can be displayed on the serial console.
14:55Inside the main function, the first step is to call the ESPINIT function to initialize the ESP8266 module.
15:04If initialization fails, we will print an error message on the console and call the error handler function to stop further execution.
15:13Once the module is initialized successfully, we call the ESPconnectWi-Fi function and pass the network SSID and password as parameters.
15:24I am connecting the ESP8266 to my home Wi-Fi network, which is also the same network that my computer is connected to.
15:33The IP address assigned to the module will be stored inside a buffer, so we need to define this buffer first.
15:40To make it reusable later in the series, we will define the buffer globally.
15:44If the connection function fails, once again, an error log will be printed and the error handler will be called.
15:52Now, let's build the project.
15:54The code compiles without any errors, which means we are ready to flash it to the development board.
16:00Before flashing, let's double-check the hardware connections.
16:03The Rx pin from the ESP adapter is connected to PA0, UART4TX.
16:10The TX pin from the adapter is connected to PA1, UART4RX.
16:15The ESP adapter is powered with 5V from the STM32 board itself.
16:21Make sure the USB cable is plugged into the development board, since the ST-Link V2 provides only 3.3V for the board.
16:30If you are using an external 5V supply for the ESP adapter, ensure that the ground of the external supply is properly connected to the STM32 board's ground.
16:42For debugging logs, the PA9, UART1TX.
16:47Pin is connected to the RX pin of the FT232 USB to serial adapter.
16:53Since we only need to send logs to the console, connecting just the TX pin is sufficient.
16:58With everything connected, let's flash the project to the board.
17:02Now, open the serial terminal on your computer, select the correct port, and set the baud rate to 115,200.
17:10Here you can see the sequence of events.
17:13The ESP module begins initialization.
17:17Once initialized, it switches to station mode.
17:20It then connects to the Wi-Fi network.
17:22Finally, the module retrieves and displays its IP address.
17:28Everything worked as expected.
17:30The process went smoothly from initialization to Wi-Fi connection and finally obtaining the IP address.
17:37At this point, our STM32 board with ESP8266 is ready to handle network-based protocols.
17:45If you encounter errors in the user logs, you can always enable the debug logs for a more detailed breakdown.
17:52Let me show you an example.
17:54With debug logs enabled, you can see every step clearly.
17:58It logs which command was sent, whether the expected response was received, and even displays the entire buffer received after each command.
18:07So, that completes this part of the tutorial.
18:11I hope this video helps you successfully connect your STM32 to the network using the ESP8266 module.
18:19This is just the beginning.
18:21We will continue building on this series in the upcoming videos.
18:25In the next video, we will focus on logging sensor data to a cloud server.
18:30I will use the BME280 sensor to read temperature, pressure, and humidity, and then log that data to the ThingsSpeak cloud platform.
18:39After that, we will move on to exploring network protocols like the MQTT client.
18:45That's it for this video.
18:46You can download the full project from the link provided in the description.
18:50If you have any questions, or if there is a specific topic you would like me to cover in this series, feel free to leave a comment.
18:58Keep watching, and have a great day ahead!
Be the first to comment
Add your comment

Recommended