Flask-stm32-dataTrans

ESP8266 module is used to realize data transmission between Python server and STM32 client.

This project consists of two projects:

  • flaskProject
  • stm32Project

First, the overall plan

This project uses ESP8266-wifi module to access LAN, and uses C/S model to fulfill requirements.

The specific idea is that the controller and the controlled are in the same network (the experimental environment is the laboratory Intranet)

Using ESP8266 module to establish TCP server, Python socket package to build TCP client

After the server is established, the client can connect to the server using an IP address

The user sends instructions to the controlled object through the Web interface written by Flask. After receiving the instructions, the STM32 controller can complete a series of operations

Two, basic steps

  • Initialize the primary controller
    • Stm32 low-level initialization
    • The I2C display is initialized. Procedure
    • Configure the ESP8266 module
  • Example Initialize the ESP8266 module
    • Start STA mode
    • Connect the WIFI
    • Querying a Local IP Address
    • Setting up Multiple Connections
    • Establishing the TCP Server
  • Example Initialize the TCP client
    • Set the IP address and port number
    • Creating a Socket object
    • Establishing a Socket Connection
  • Initialize the Flask server
    • The front-end interface
    • The backend API
  • The client sends a message to ESP8266
    • Front buttons initiate requests to the back end via AJAX
    • The back end invokes the TCP client to send a message to ESP8266
  • The primary controller receives the message
    • The main controller connected to the ESP8266 through the serial port enters the serial port interrupt callback
    • Turn peripherals on or off according to instructions

Stm32 initializes ESP8266

  • First, stM32 needs to receive data of variable serial length (as outlined in the STM32Driver project).
  • Next, esp8266 is initialized using state transitions
  • Some basic information after initialization can be displayed on the I2C screen (e.g. the IP address assigned to esp, which needs to be connected to later flask pages)
void user_API(uint8_t temp[], uint8_t temp_len){
  if(flag<6){
    OLED_flash(temp, temp_len);
  }
  if(flag == 0){
    str_pos = GetSubStrPos(temp, "WIFI GOT IP");
    if(str_pos ! =- 1){
      DMA_Usart1_Send("AT+CIFSR\r\n".10);
      flag=1; }}else if(flag == 1){
    str_pos = GetSubStrPos(temp, "+CIFSR:STAIP,");
    if(str_pos ! =- 1) {for(uint8_t i=0; i<13; i++){
        ipaddr[i+1] = temp[str_pos+14+i];
      }
      OLED_ShowString_cnt(1.1,ipaddr,8.14);

      DMA_Usart1_Send("AT+CIPMUX=1\r\n".13);
      flag=2; }}else if(flag == 2){
    str_pos = GetSubStrPos(temp, "OK");
    if(str_pos! =- 1){
      DMA_Usart1_Send("AT+CIPSERVER=1\r\n".16);
      flag=3; }}else if(flag == 3){
    str_pos = GetSubStrPos(temp, "OK");
    if(str_pos! =- 1){
      DMA_Usart1_Send("AT+CIPSTO=0\r\n".13);
      flag=4; }}else if(flag == 4){
    str_pos = GetSubStrPos(temp, "OK");
    if(str_pos! =- 1){
      DMA_Usart1_Send("AT\r\n".4);
      flag=5; }}else if(flag == 5){
    str_pos = GetSubStrPos(temp, "OK");
    if(str_pos! =- 1){
      OLED_Clear();
      OLED_ShowString_cnt(1.1,ipaddr,8.14);
      OLED_formatFlash();
      flag = 6; }}else if(flag == 6){
    str_pos = GetSubStrPos(temp, "+IPD");
    if(str_pos! =- 1) {uint8_t p1[] = "0";
      uint8_t p2[] = "3";
      uint8_t p3[10];
      p1[0] = temp[str_pos+5];
      p2[0] = temp[str_pos+7];
      uint8_t p3_len = (uint8_t)(p2[0] -'0');
      for(uint8_t i=0; i<p3_len; i++){
        p3[i] = temp[str_pos+9+i]; } OLED_infoFlash(p1, p2, p3, p3_len); }}// DMA_Usart1_Send(rx1_buffer, rx1_len);
}
Copy the code

Four, debugging ESP8266 may need to use AT instructions

AT+CWJAP=" ssiD ","password" AT+CWJAP=" ssiD "," CIFSR Establishing the TCP ServerCopy the code

Five, complete the effect

1. Web interface

2. The controlled end

From left to right:

  • STM32 development board, I2C display screen
  • Power supply module and ESP8266 module
  • The power supply

3. Project address

Github.com/causehhc/Fl…