Beginner’s Guide to DIY an STM32-based Remote Control Computer System

Background Knowledge

Computer access and control from a distance are referred to as “remote control” of computers. The history of remote computer control may be traced to the early days of computing when mainframe computers were the first to use it for technical support. With the development of technology, remote computer control has emerged as a crucial tool for IT assistance, as well as for companies, organizations, and people that require remote access to and control over their computers.

As more people work from home or want computer access while on the go, remote computer control has grown in importance. It offers a practical and effective method of controlling a computer from any location with an internet connection, increasing flexibility and productivity.

Introduction

The feasibility of a DIY remote control computer system based on STM32 is quite high. By programming the STM32 microcontroller to act as a server, it is possible to establish a connection with a remote computer via the internet. This connection can then be used to send and receive data, allowing the remote user to control the computer from a distance. In this project, we’ll learn a simple DIY STM32-based remote control computer system.

An easy and simple project for beginners to DIY an STM32-based remote control computer system is to create a simple webpage that allows users to turn on and off an LED connected to an STM32 microcontroller from a remote location.

Hardware Required

Hardware ComponentDescription
STM32 MicrocontrollerAny STM32 microcontroller with Ethernet capability, such as STM32F407 or STM32F429
Ethernet ModuleAny Ethernet module with SPI interface, such as W5500
LEDAny LED with an appropriate resistor for the STM32 microcontroller
BreadboardAny breadboard to connect the components
Jumper WiresA set of male-to-male and male-to-female jumper wires
Power SupplyA 5V power supply to power the STM32 microcontroller

Project design

The STM32 microcontroller needs to be configured with an Ethernet module, such as the W5500 and configured to function as a server before anything else. The LED, which will act as the remote-controlled gadget, should also be linked to the microcontroller.

The user will be able to instruct the STM32 microcontroller to turn on or off the LED by sending a command through a straightforward HTML and JavaScript-created webpage. A remote server or the STM32 microcontroller itself may host the website.

The “on” or “off” button on the homepage should trigger a JavaScript code to send a request to the STM32 microcontroller’s IP address, specifying whether the LED should be switched on or off. This request should be sent to the STM32 microcontroller, which should be programmed to respond by controlling the LED.

Step 1: Configure the STM32 microcontroller

  1. Choose an STM32 microcontroller with Ethernet capability, such as STM32F407 or STM32F429.
  2. Connect the Ethernet module, such as the W5500, to the SPI interface of the STM32 microcontroller.
  3. Install the necessary software tools, such as a toolchain, and an integrated development environment (IDE), such as STM32CubeIDE, to develop and upload the firmware to the STM32 microcontroller.
  4. In the IDE, create a new project, select the appropriate STM32 microcontroller, and configure the project to use the Ethernet peripheral. This can be done by enabling the Ethernet pins, setting up the Ethernet MAC address, and configuring the Ethernet PHY.
  5. Configure the STM32 microcontroller to act as a server by setting up a socket and listening for incoming connections. This can be done using the WIZnet Ethernet library and the lwIP (lightweight IP) stack.

A sample code snippet that sets up a socket and listens for incoming connections:

#include "stm32f4xx_hal.h"
#include "wizchip_conf.h"
#include "socket.h"
#include "lwip/api.h"
// Define the IP address and port number to listen on
#define LISTEN_PORT 80
#define IP_ADDR "192.168.0.100"
// Create a socket handle
static int socket_handle;
// Define the callback function to handle incoming requests
static void request_handler(void *arg)
{
  struct netconn *conn = (struct netconn *)arg;
  struct netbuf *buf;
  // Read the incoming request data
  netconn_recv(conn, &buf);
  // Process the request data
  // ...
  // Close the connection
  netconn_close(conn);
  netbuf_delete(buf);
}
// Main function
int main(void)
{
  // Initialize the WIZnet Ethernet module
  wizchip_init();
  // Configure the STM32 microcontroller to use the Ethernet module
  // ...
  // Create a socket for the server to listen on
  socket_handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  // Bind the socket to the IP address and port number
  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(LISTEN_PORT);
  server_addr.sin_addr.s_addr = inet_addr(IP_ADDR);
  bind(socket_handle, (struct sockaddr *)&server_addr, sizeof(server_addr));
  // Set the socket to listen for incoming connections
  listen(socket_handle, 1);
  // Start an infinite loop to handle incoming requests
  while (1)
  {
    // Wait for an incoming connection
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int conn_handle = accept(socket_handle, (struct sockaddr *)&client_addr, &client_addr_len);
    // Create a new netconn for the incoming connection
    struct netconn *conn = netconn_new(NETCONN_TCP);
    netconn_bind(conn, NULL, LISTEN_PORT);
    netconn_connect(conn, (ip_addr_t *)&client_addr.sin_addr.s_addr, LISTEN_PORT);
    // Start a new thread to handle the incoming request
    sys_thread_new("request", request_handler, conn, 2048, 1);
  }
}

Step 2: Connect the LED to the STM32 microcontroller

Connect the positive leg of the LED to a digital output pin of the STM32 microcontroller, and connect the negative leg of the LED to a ground pin of the microcontroller.

In the firmware code, configure the digital output pin to output a logic high or low level to control the LED. This can be done using the HAL (Hardware Abstraction Layer) library for the STM32 microcontroller.

Here is a sample code snippet that turns the LED on and off using the PA5 pin of the STM32F4 microcontroller:

#include "stm32f4xx_hal.h"
// Define the LED pin
#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA
// Main function
int main(void)
{
  // Initialize the STM32 microcontroller
  HAL_Init();
  // Configure the LED pin as a digital output
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitStruct.Pin = LED_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
  // Start an infinite loop to toggle the LED
  while (1)
  {
    // Turn the LED on
    HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
    // Wait for some time
    HAL_Delay(1000);
    // Turn the LED off
    HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
    // Wait for some time
    HAL_Delay(1000);
  }
}

Step 3: Create a simple webpage using HTML and JavaScript

To create a simple webpage using HTML and JavaScript to send a command to the STM32 microcontroller to turn on or off the LED, you will need to do the following:

  1. Connect the STM32 microcontroller to your computer using a USB cable.
  2. Write an embedded C program to control the LED using a GPIO pin on the microcontroller.
  3. Compile the embedded C program using an IDE like Keil or IAR and upload it to the microcontroller using a flash programmer.
  4. Create an HTML form with two buttons, one to turn the LED on and the other to turn it off.
  5. Write a JavaScript function to send the command to the microcontroller when the user clicks on either of the buttons.
  6. Use a serial communication protocol like UART to send the command from the microcontroller to the computer.
  7. Use a JavaScript library like jQuery or Axios to send an HTTP POST request to a server running on the computer, which will then send the command to the microcontroller.

Here’s an example of what the HTML code might look like:

<!DOCTYPE html>
<html>
<head>
  <title>STM32 LED Control</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
  <h1>STM32 LED Control</h1>
  <form>
    <button type="button" onclick="sendCommand('on')">Turn On</button>
    <button type="button" onclick="sendCommand('off')">Turn Off</button>
  </form>
  <script>
    function sendCommand(command) {
      $.ajax({
        type: "POST",
        url: "http://localhost:3000/command",
        data: { command: command },
        success: function() {
          console.log("Command sent: " + command);
        },
        error: function(xhr, status, error) {
          console.error("Error sending command: " + error);
        }
      });
    }
  </script>
</body>
</html>

Here’s an example of what the embedded C code might look like:

#include "stm32f4xx.h"
#define LED_PIN GPIO_Pin_13
#define LED_PORT GPIOA
void init_led() {
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Pin = LED_PIN;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
void turn_on_led() {
  GPIO_SetBits(LED_PORT, LED_PIN);
}
void turn_off_led() {
  GPIO_ResetBits(LED_PORT, LED_PIN);
}
int main(void) {
  init_led();
  while (1) {
    // Do nothing
  }
}
void USART2_IRQHandler() {
  if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
    char command = USART_ReceiveData(USART2);
    if (command == '1') {
      turn_on_led();
    } else if (command == '0') {
      turn_off_led();
    }
  }
}

Step 4: Implement the remote control functionality

To implement the remote control functionality, you can create a web page that contains two buttons, “on” and “off”, which the user can click to turn the LED on or off remotely. When the user clicks on one of the buttons, a JavaScript function can be called to send an HTTP request to the IP address of the STM32 microcontroller with a specific URL that indicates the desired LED state.

Here’s an example of how to create a web page with the two buttons:

<!DOCTYPE html>
<html>
<head>
  <title>Remote Control</title>
</head>
<body>
  <h1>Remote Control</h1>
  <button onclick="turnOn()">On</button>
  <button onclick="turnOff()">Off</button>
  
  <script>
    function turnOn() {
      sendRequest("on");
    }
    
    function turnOff() {
      sendRequest("off");
    }
    
    function sendRequest(state) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "http://<IP_ADDRESS>/led/" + state);
      xhr.send();
    }
  </script>
</body>
</html>

To implement the remote control functionality, you can create a web page that contains two buttons, “on” and “off”, which the user can click to turn the LED on or off remotely. When the user clicks on one of the buttons, a JavaScript function can be called to send an HTTP request to the IP address of the STM32 microcontroller with a specific URL that indicates the desired LED state.

Here’s an example of how to create a web page with the two buttons:

php
Copy code
<!DOCTYPE html>
<html>
<head>
  <title>Remote Control</title>
</head>
<body>
  <h1>Remote Control</h1>
  <button onclick="turnOn()">On</button>
  <button onclick="turnOff()">Off</button>
  
  <script>
    function turnOn() {
      sendRequest("on");
    }
    
    function turnOff() {
      sendRequest("off");
    }
    
    function sendRequest(state) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "http://<IP_ADDRESS>/led/" + state);
      xhr.send();
    }
  </script>
</body>
</html>

In this example, the turnOn() and turnOff() functions are called when the user clicks on the corresponding buttons, and they send an HTTP GET request to the IP address of the STM32 microcontroller with a URL that contains the desired LED state (“on” or “off”).

On the STM32 microcontroller side, you can program it to listen for HTTP requests on a specific URL, and control the LED according to the requested state. Here’s an example of how to do this using the lwIP TCP/IP stack and the STM32CubeMX code generation tool:

  1. Open STM32CubeMX and create a new project for your STM32 microcontroller.
  2. Configure the Ethernet module (e.g., W5500) and the GPIO pin for the LED in the Pinout & Configuration tab.
  3. Generate code for the project and open it in your favorite IDE.
  4. In the main() function, initialize the Ethernet module and set up a TCP/IP connection using the lwIP TCP/IP stack. This can be done using the HAL library and the netconn_* functions provided by lwIP.
  5. Implement a callback function that handles incoming HTTP requests. This function should check the requested URL and control the LED according to the requested state.

Here’s an example of how to do this:

void http_server_serve(struct netconn *conn)
{
  struct netbuf *inbuf;
  char *buf;
  u16_t buflen;
  err_t err;
  const char *led_on = "/led/on";
  const char *led_off = "/led/off";
  
  err = netconn_recv(conn, &inbuf);
  
  if (err == ERR_OK)
  {
    netbuf_data(inbuf, (void**)&buf, &buflen);
    
    if (buflen >= strlen(led_on) && strncmp(buf, led_on, strlen(led_on)) == 0)
    {
      // Turn the LED on
      HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
    }
    else if (buflen >= strlen(led_off) && strncmp(buf, led_off, strlen(led_off)) == 0)
    {
      // Turn the LED off
      HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
    }
    
    netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
    netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY);
  }

Conclusion

In conclusion, DIYing a remote control computer system based on STM32 is a feasible project for beginners with some basic programming and electronics knowledge. The system can be implemented using the STM32 microcontroller, an Ethernet module, a few basic electronic components, and some programming languages such as C, HTML, and JavaScript. By using lwIP and HTTP protocols, the system can enable remote control of an LED from a web page, allowing users to turn on or off the LED from any device connected to the same local network. This project is an excellent way for beginners to gain practical experience with microcontroller programming, electronics, and network communication while building a fun and useful gadget.

2 thoughts on “Beginner’s Guide to DIY an STM32-based Remote Control Computer System”

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
Scroll to Top