QC

ESP8266 with STM32F103C8 Webserver

  ESP8266 with STM32F103C8 Webserver



Nếu bạn nghĩ về các công nghệ tương lai, thì hai cái tên, ngay lập tức xuất hiện trong đầu bạn, là Trí tuệ nhân tạo (AI) và Internet of Things (IoT). AI vẫn đang trong giai đoạn đầu và còn rất nhiều thứ nữa sẽ được phát triển. Nhưng IoT đang trong giai đoạn Tăng trưởng và rất nhiều sản phẩm dựa trên IoT đã có mặt trên thị trường. Ngoài ra, có rất nhiều công cụ và phần cứng có sẵn trên thị trường để làm cho sản phẩm của bạn có thể nói chuyện với 'mọi thứ' trên internet. Trong số đó ESP8266 là module phổ biến nhất, rẻ và dễ sử dụng, có thể kết nối phần cứng của bạn với Internet.


Chúng tôi đã phát triển rất nhiều Dự án IoT sử dụng ESP8266, không chỉ bao gồm giao diện cơ bản với các bộ vi điều khiển khác như Arduino, PIC, AVR mà còn bao gồm nhiều dự án thông minh như Giám sát ô nhiễm không khí dựa trên IOT, Theo dõi xe trên Google Maps, Tự động hóa nhà điều khiển bằng giọng nói dựa trên IOT Vân vân Hôm nay trong hướng dẫn này, chúng tôi sử dụng ESP8266 để kết nối STM32F103C8 với internet. Ở đây, chúng tôi sẽ giao diện mô-đun Wi-Fi ESP8266 với bo mạch Blue Pill STM32F103C8 và gửi dữ liệu đến trang web được lưu trữ trên máy chủ web ESP8266.

Components Required

  • Blue Pill STM32F103C8 board
  • ESP8266 Wi-Fi module
  • Laptop & Wi-Fi hotspot

ESP8266 Wi-Fi Module

Hầu hết mọi người gọi ESP8266 như một mô-đun WIFI, nhưng nó thực sự là một bộ vi điều khiển. ESP8266 là tên của bộ vi điều khiển được phát triển bởi Espressif Systems, một công ty có trụ sở tại Thượng Hải. Bộ vi điều khiển này có khả năng thực hiện các hoạt động liên quan đến WIFI do đó nó được sử dụng rộng rãi như một mô-đun WIFI.



  1. GND, Ground (0 V)
  2. TX, Transmit data bit X
  3. GPIO 2, General-purpose input/output No. 2
  4. CH_PD, Chip power-down
  5. GPIO 0, General-purpose input/output No. 0
  6. RST, Reset
  7. RX, Receive data bit X
  8. VCC, Voltage (+3.3 V)

Lệnh AT được sử dụng để giao tiếp với ESP8266. Bảng dưới đây cho thấy một số lệnh AT hữu ích


AT COMMANDSUSE
ATAcknowledgement returns ‘OK’
AT+RSTRESTART module
AT+GMRShows FIRMWARE DETAILS
AT+CWMODE=1 or 2 or 3Wi-Fi mode 1-Station, 2- AP ,3-Both
AT+CWLAPList the AP
AT+CWJAP=”SSID”,”PASSWORD”Joins AP
AT+CWQAPQuits AP
AT+CIFSRGets IP address
AT+CIPMUX= 0 or 1Set multiple connection 0- Single ,1-multiple
AT+CIPSTART AT+CIPSTART=<type>,<address>,<port> AT+CIPSTART=<id>,<type>,<address>,<port>Setup up TCP/UDP connection Address- Ip address Port-port address of ip Single Connection :Type-TCP, UDP Multiple Connection: Id-0 to 4,Type-TCP,UDP
AT+CIPSEND AT+CIPSEND=<length> AT+CIPSEND=<id>,<length>Sends data Single Connection , Length - Length of data Multiple connection , Id =0 to 4
AT+CIPSTATUSGets the connection status
AT+CIPSERVER=<mode>,<port>Set as Server 0-Server close, 1-Open port
AT+CIPCLOSEClose TCP or UDP connection

Circuit Diagram and connections


Refer below table to connect ESP8266 pins with STM32 pins:

ESP8266STM32
VCC3.3V
GNDG
CH_PD3.3V
TXPA3
RXPA2

SMT32F103C8 has three sets of UART serial communication. In the below image you can see the following pins for the same:

Serial PortPinsTolerant
Serial1 (TX1,RX1)PA9,PA10 PB6,PB75V
Serial2 (TX2,RX2)PA2,PA33.3V
Serial3 (TX3,RX3)PB10,PB115V


Code

//Interfacing ESP8266 Wi-Fi with STM32F103C8
//CIRCUIT DIGEST
//NOTE: Serial is serial monitor with baud rate(9600)
//NOTE: Serial2 (TX2, RX2)is connected with ESP8266(RX,TX)respectively with baud rate (9600)

String webpage="";                                   //String variable to store characters
int i=0,k=0,x=0;                                         //integer variables
String readString;                                   //using readString feature to read characters                       

boolean No_IP=false;                                 //boolean variables 
String IP="";                                         //String variable to store data
char temp1='0';                                      //character variable

String name="<p>Circuit Digest</p><p>A community of electrical and electronics students, engineers and makers</p>";   //String with html notations
String data="<p>Data Received Successfully.....</p>";     //String with html 
     
void check4IP(int t1)                                     //A function to check ip of ESP8266 
{
  int t2=millis();
  while(t2+t1>millis())
  {
    while(Serial2.available()>0)
    {
      if(Serial2.find("WIFI GOT IP"))
      {
        No_IP=true;
      }
    }
  }
}

void get_ip()                                           //After cheacking ip ,this is a function to get IP address
{
  IP="";
  char ch=0;
  while(1)
  {
    Serial2.println("AT+CIFSR");                   //GET IP AT COMMAND
    while(Serial2.available()>0)        
    {
      if(Serial2.find("STAIP,"))                   //This finds the STAIP that is the STATIC IP ADDRESS of ESP8266
      {
        delay(1000);
        Serial.print("IP Address:");
        while(Serial2.available()>0)
        {
          ch=Serial2.read();                      //Serial2 reads from ESP8266
          if(ch=='+')
          break;
          IP+=ch;
        }
      }
      if(ch=='+')
      break;
    }
    if(ch=='+')
    break;
    delay(1000);
  }
  Serial.print(IP);                                //prints IP address in Serial monitor
  Serial.print("Port:");
  Serial.println(80);
}

void connect_wifi(String cmd, int t)                  //This function is for connecting ESP8266 with wifi network by using AT commands
{
  int temp=0,i=0;
  while(1)
  {
    Serial.println(cmd);                  //Sends to serial monitor
    Serial2.println(cmd);                 //sends to ESP8266 via serial communication
    while(Serial2.available())
    {
      if(Serial2.find("OK"))
      i=8;
    }
    delay(t);
    if(i>5)
    break;
    i++;
  }
  if(i==8)
  Serial.println("OK");
  else
  Serial.println("Error");
}

void wifi_init()                                //This function contains AT commands that passes to connect_wifi()
{
      connect_wifi("AT",100);                   //Sends AT command with time(Command for Achknowledgement)
      connect_wifi("AT+CWMODE=3",100);          //Sends AT command with time (For setting mode of Wifi)
      connect_wifi("AT+CWQAP",100);            //Sends AT command with time (for Quit AP)
      connect_wifi("AT+RST",5000);             //Sends AT command with time (For RESETTING WIFI)
      check4IP(5000);
      if(!No_IP)
      {
        
        Serial.println("Connecting Wifi....");
        connect_wifi("AT+CWJAP=\"Pramo\",\"pokemon08\"",7000);         //provide your WiFi username and password here
   
      }
      else
        {
        }
      Serial.println("Wifi Connected"); 
      get_ip();
      
      connect_wifi("AT+CIPMUX=1",100);                          //Sends AT command with time (For creating multiple connections)
      connect_wifi("AT+CIPSERVER=1,80",100);                    //Sends AT command with time (For setting up server with port 80)
}

void sendwebdata(String webPage)                          //This function is used to send webpage datas to the localserver
{
    int ii=0;
     while(1)
     {
      unsigned int l=webPage.length();
      Serial.print("AT+CIPSEND=0,");
      Serial2.print("AT+CIPSEND=0,");
      Serial.println(l+2);
      Serial2.println(l+2);
      delay(100);
      Serial.println(webPage);                        //sends webpage data to serial monitor
      Serial2.println(webPage);                       //sends webpage data to serial2 ESP8266
      while(Serial2.available())
      {
       
        if(Serial2.find("OK"))
        {
          ii=11;
          break;
        }
      }
      if(ii==11)
      break;
      delay(100);
     }
}

void setup() 
{
   Serial.begin(9600);                //begins serial monitor with baud rate 9600
   Serial2.begin(9600);               //begins serial communication with esp8266 with baud rate 9600 (Change according to your esp8266 module)
   wifi_init();
   Serial.println("System Ready..");
}

void loop() 
{
  k=0;
  Serial.println("Please Refresh your Page");
  while(k<1000)
  {
    k++;
   while(Serial2.available())
   {
    if(Serial2.find("0,CONNECT"))
    {
      Serial.println("Start Printing");
      Send();
      Serial.println("Done Printing");
      delay(1000);
    }
  }
  delay(1);
 }
}

void Send()                                        //This function contains data to be sent to local server
{
      webpage = "<h1>Welcome to Circuit Digest</h1><body bgcolor=f0f0f0>";
      sendwebdata(webpage);
      webpage=name;
      sendwebdata(webpage);
      delay(1000);
      webpage = "<a href=\"http://circuitdigest.com/\"";
      webpage+="\">Click Here to get into circuitdigest.com</a>";
      webpage+=data;
      sendwebdata(webpage);
      Serial2.println("AT+CIPCLOSE=0");                  //Closes the server connection
}






Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...