HTTP POST/PUT Requests for ESP32 LEDs Module Control in STM32-based RCCar Project
In this article, we will discuss how to use HTTP POST and PUT requests for controlling ESP32-based LED modules in an STM32-based RCCar project. By the end of this article, you will have a good understanding of the key concepts and be able to implement them in your own project.
Introduction
The Internet of Things (IoT) has opened up new possibilities for controlling devices remotely using HTTP requests. In this project, we will be using STM32 as the main processor and ESP32 as a co-processor to control the LED module. We will be sending HTTP POST and PUT requests to the ESP32 to turn the LEDs on and off.
Prerequisites
Before we begin, it is assumed that you have a basic understanding of:
- C programming language
- STM32 and ESP32 microcontrollers
- HTTP protocol
- LED module
ESP32 Setup
The ESP32 will be responsible for receiving the HTTP requests and controlling the LED module. To set up the ESP32, you will need to:
- Connect the ESP32 to your computer using a USB cable.
- Install the ESP32 board in the Arduino IDE.
- Write a program to receive HTTP requests and control the LED module.
Here is an example code snippet for the ESP32:
#include <WiFi.h>
const char* ssid = "your\_SSID";
const char* password = "your\_PASSWORD";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(LED\_BUILTIN, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL\_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int ledState = LOW;
if (req.indexOf("/LED=ON") != -1) {
ledState = HIGH;
} else if (req.indexOf("/LED=OFF") != -1) {
ledState = LOW;
}
// Set LED state
digitalWrite(LED\_BUILTIN, ledState);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("");
client.println("");
client.println("");
client.println("ESP32 Web Server ");
client.println("ESP32 Web Server
");
client.println("LED state:
");
if (ledState == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("");
delay(1);
Serial.println("Client disconnected");
}
STM32 Setup
The STM32 will be responsible for sending the HTTP requests to the ESP32. To set up the STM32, you will need to:
- Connect the STM32 to your computer using a USB cable.
- Install the STM32CubeMX software.
- Create a new project and generate the code.
- Modify the code to send HTTP requests to the ESP32.
Here is an example code snippet for the STM32:
#include "main.h"
UART\_HandleTypeDef huart1;
void SystemClock\_Config(void);
static void MX\_GPIO\_Init(void);
static void MX\_USART1\_UART\_Init(void);
int main(void)
{
HAL\_Init();
SystemClock\_Config();
MX\_GPIO\_Init();
MX\_USART1\_UART\_Init();
while (1)
{
char data[50];
sprintf(data, "POST /LED=ON HTTP/1.1\r
Host: 192.168.1.2\r
Connection: close\r
\r
");
HAL\_UART\_Transmit(&huart1, (uint8\_t*)data, strlen(data), HAL\_MAX\_DELAY);
HAL\_Delay(1000);
sprintf(data, "POST /LED=OFF HTTP/1.1\r
Host: 192.168.1.2\r
Connection: close\r
\r
");
HAL\_UART\_Transmit(&huart1, (uint8\_t*)data, strlen(data), HAL\_MAX\_DELAY);
HAL\_Delay(1000);
}
}
void SystemClock\_Config(void)
{
RCC\_OscInitTypeDef RCC\_OscInitStruct = {0};
RCC\_ClkInitTypeDef RCC\_ClkInitStruct = {0};
RCC\_OscInitStruct.OscillatorType = RCC\_OSCILLATORTYPE\_HSE;
RCC\_OscInitStruct.HSEState = RCC\_HSE\_ON;
RCC\_OscInitStruct.HSEPredivValue = RCC\_HSE\_PREDIV\_DIV1;
RCC\_OscInitStruct.PLL.PLLState = RCC\_PLL\_ON;
RCC\_OscInitStruct.PLL.PLLSource = RCC\_PLLSOURCE\_HSE;
RCC\_OscInitStruct.PLL.PLLMUL = RCC\_PLL\_MUL9;
if (HAL\_RCC\_OscConfig(&RCC\_OscInitStruct) != HAL\_OK)
{
Error\_Handler();
}
RCC\_ClkInitStruct.ClockType = RCC\_CLOCKTYPE\_HCLK|RCC\_CLOCKTYPE\_SYSCLK
|RCC\_CLOCKTYPE\_PCLK1|RCC\_CLOCKTYPE\_PCLK2;
RCC\_ClkInitStruct.SYSCLKSource = RCC\_SYSCLKSOURCE\_PLLCLK;
RCC\_ClkInitStruct.AHBCLKDivider = RCC\_SYSCLK\_DIV1;
RCC\_ClkInitStruct.APB1CLKDivider = RCC\_HCLK\_DIV2;
RCC\_ClkInitStruct.APB2CLKDivider = RCC\_HCLK\_DIV1;
if (HAL\_RCC\_ClockConfig(&RCC\_ClkInitStruct, FLASH\_LATENCY\_2) != HAL\_OK)
{
Error\_Handler();
}
}
static void MX\_GPIO\_Init(void)
{
GPIO\_InitTypeDef GPIO\_InitStruct = {0};
__HAL\_RCC\_GPIOA\_CLK\_ENABLE();
GPIO\_InitStruct.Pin = GPIO\_PIN\_9;
GPIO\_InitStruct.Mode = GPIO\_MODE\_AF\_PP;
GPIO\_InitStruct.Speed = GPIO\_SPEED\_FREQ\_HIGH;
GPIO\_InitStruct.Alternate = GPIO\_AF1\_USART1;
HAL\_GPIO\_Init(&GPIOA, &GPIO\_InitStruct);
}
static void MX\_USART1\_UART\_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART\_WORDLENGTH\_8B;
huart1.Init.StopBits = UART\_STOPBITS\_1;
huart1.Init.Parity = UART\_PARITY\_NONE;
huart1.Init.Mode = UART\_MODE\_TX\_RX;
huart1.Init.HwFlowCtl = UART\_HWCONTROL\_NONE;
huart1.Init.OverSampling = UART\_OVERSAMPLING\_16;
if (HAL\_UART\_Init(&huart1) != HAL\_OK)
{
Error\_Handler();
}
}
void Error\_Handler(void)
{
while(1)
{
}
}
In this article, we have discussed how to use HTTP POST and PUT requests for controlling ESP32-based LED modules in an STM32-based RCCar project. We have covered the ESP32 setup, STM32 setup, and provided example code snippets for both. By following the steps outlined in this article, you should be able to implement this functionality in your own project.