服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - 手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

2023-10-16 15:01嵌入式悦翔园 服务器知识

今天我们就以ESP8266和STM32来实现一台网络服务器,我们使用 ESP8266 将 STM32F103C8 连接到互联网。 ESP8266 Wi-Fi 模块与 STM32F103C8 板接口,并将数据发送到 ESP8266 网络服务器上托管的网页。

前言

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

随着现在物联网设备的而越来越多,现在市场上出现越来越多的物联网设备,其中 ESP8266 是最受欢迎、价格便宜且易于使用的模块,它可以将您的硬件连接到互联网。

今天我们就以ESP8266和STM32来实现一台网络服务器,我们使用 ESP8266 将 STM32F103C8 连接到互联网。 ESP8266 Wi-Fi 模块与 STM32F103C8 板接口,并将数据发送到 ESP8266 网络服务器上托管的网页。

所需硬件

  • STM32F103C8板
  • ESP8266 无线无线模块
  • 笔记本电脑和无线网络热点

ESP8266 模块

大多数人将 ESP8266 称为 WIFI 模块,但它实际上是一个微控制器。ESP8266 是乐鑫公司(一家总部位于上海的公司)开发的微控制器的名称。该微控制器具有执行WIFI相关活动的能力,因此被广泛用作WIFI模块。

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

引脚说明

  • GND:接地
  • TX:发射传输数据位
  • GPIO2:通用输入/输出 2
  • CH_PD:使能
  • GPIO0:通用输入/输出0
  • RST:复位
  • RX:接收数据位
  • VCC:+3.3 V

常用AT指令

AT 命令用于与 ESP8266 通信。下表显示了一些常用的 AT 命令

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

电路图和连接

下图显示了STM32和ESP8266无线模块之间的连接。

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

SMT32F103C8具有三套UART串行通信。在下图中,您可以看到相同的以下引脚:

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

ESP8266 使用串行通信与STM32进行通讯。所以这里 ESP8266 的 TX 和 RX 都与 STM32 板的串行 2 端口 (PA2 和 PA3) 连接。

代码解释

ESP8266 与 STM32 的接口工作非常简单。您可以在本教程末尾的代码中找到完整的工作。

首先,我们需要按照电路图中所示进行电路连接。上传代码后,打开串行监视器(工具>>串行监视器)以查看发生的情况。您将在串行监视器上看到IP地址,从串行监视器复制IP地址并将其粘贴到浏览器中,然后单击Enter以查看我们的网页。请记住将计算机和 ESP8266 模块连接到同一 Wi-Fi 网络上。

完整的代码在最后给出,并通过注释很好地解释了,在这里我们解释了其中的几个重要部分。

首先,我们使用以下两个语句开始串行监视器和 ESP8266 的串行通信:

Serial.println(cmd);
Serial2.println(cmd);

注意: 我使用过STM32串行2端口的引脚(PA2,PA3),因为它可以承受3.3V。

然后,我们需要让 ESP8266 准备就绪,方法是通过重置任何旧的已连接的 AP 并将其设置为 AP 和 STA 来退出任何旧的 AP

connect_wifi("AT",100);  //Sends AT command with time(Command for Acknowledgement)
connect_wifi("AT+CWMODE=3",100);   //Sends AT command with time (For setting mode of Wi-Fi)
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)

然后将 ESP8266 与无线网络连接。您必须填写您的Wi-Fi详细信息,如下面的代码所示:

connect_wifi("AT+CWJAP="Pramo","pokemon08"",7000);  //provide your WiFi username and password here

然后我们获取 ESP8266 模块的 IP 地址,并使用下面的代码将其显示在串行监视器上

Serial2.println("AT+CIFSR");           //GET IP AT COMMAND
if(Serial2.find("STAIP,"))                 //This finds the STAIP that is the STATIC IP ADDRESS of ESP8266
Serial.print(IP);                                //prints IP address in Serial monitor

接下来,我们将为网页编写 HTML 代码。要将HTML代码转换为Arduino代码,您可以使用此链接。

HTML在线转Arduino代码

  1. webpage = "<h1>Welcome to Circuit Digest</h1><body bgcolor=f0f0f0>"//This is the heading line with black font colour 
  2. String name="<p>Circuit Digest</p><p>A community of electrical and electronics students, engineers and makers</p>"
  3. String data="<p>Data Received Successfully.....</p>";     //These two lines are of two paragraph 
  4. webpage = "<a href=\"http://circuitdigest.com/\""
  5. webpage+="\">Click Here to get into circuitdigest.com</a>"//At last we insert the hyperlink to link the website address 

接下来在void send() 函数中,我们使用发送网络数据函数打印了HTML,并使用AT + CIPCLOSE = 0关闭了服务器连接

完成所有工作后,您可以通过在任何 Web 浏览器中打开 ESP8266 的 IP 并单击网页上显示的链接来测试工作,单击此处进入 circuitdigest.com,如下所示

手把手带你使用ESP8266 与 STM32F103C8实现网络服务器

单击链接后,您会在网页上看到一条文本,上面写着Data Received Successfully.....

完整代码

  1. //Interfacing ESP8266 Wi-Fi with STM32F103C8 
  2.  
  3. //CIRCUIT DIGEST 
  4.  
  5. //NOTE: Serial is serial monitor with baud rate(9600) 
  6.  
  7. //NOTE: Serial2 (TX2, RX2)is connected with ESP8266(RX,TX)respectively with baud rate (9600) 
  8.  
  9.  
  10. String webpage = ""//String variable to store characters 
  11.  
  12. int i = 0, k = 0, x = 0; //integer variables 
  13.  
  14. String readString; //using readString feature to read characters                        
  15.  
  16.  
  17. boolean No_IP = false//boolean variables  
  18.  
  19. String IP = ""//String variable to store data 
  20.  
  21. char temp1 = '0'//character variable 
  22.  
  23.  
  24. String name = "<p>Circuit Digest</p><p>A community of electrical and electronics students, engineers and makers</p>"//String with html notations 
  25.  
  26. String data = "<p>Data Received Successfully.....</p>"//String with html  
  27.  
  28.  
  29.  
  30. void check4IP(int t1) //A function to check ip of ESP8266  
  31.  
  32.  
  33.     int t2 = millis(); 
  34.  
  35.     while (t2 + t1 > millis()) 
  36.  
  37.     { 
  38.  
  39.         while (Serial2.available() > 0) 
  40.  
  41.         { 
  42.  
  43.             if (Serial2.find("WIFI GOT IP")) 
  44.  
  45.             { 
  46.  
  47.                 No_IP = true
  48.  
  49.             } 
  50.  
  51.         } 
  52.  
  53.     } 
  54.  
  55.  
  56.  
  57. void get_ip() //After cheacking ip ,this is a function to get IP address 
  58.  
  59.  
  60.     IP = ""
  61.  
  62.     char ch = 0; 
  63.  
  64.     while (1) 
  65.  
  66.     { 
  67.  
  68.         Serial2.println("AT+CIFSR"); //GET IP AT COMMAND 
  69.  
  70.         while (Serial2.available() > 0) 
  71.  
  72.         { 
  73.  
  74.             if (Serial2.find("STAIP,")) //This finds the STAIP that is the STATIC IP ADDRESS of ESP8266 
  75.  
  76.             { 
  77.  
  78.                 delay(1000); 
  79.  
  80.                 Serial.print("IP Address:"); 
  81.  
  82.                 while (Serial2.available() > 0) 
  83.  
  84.                 { 
  85.  
  86.                     ch = Serial2.read(); //Serial2 reads from ESP8266 
  87.  
  88.                     if (ch == '+'
  89.  
  90.                         break
  91.  
  92.                     IP += ch; 
  93.  
  94.                 } 
  95.  
  96.             } 
  97.  
  98.             if (ch == '+'
  99.  
  100.                 break
  101.  
  102.         } 
  103.  
  104.         if (ch == '+'
  105.  
  106.             break
  107.  
  108.         delay(1000); 
  109.  
  110.     } 
  111.  
  112.     Serial.print(IP); //prints IP address in Serial monitor 
  113.  
  114.     Serial.print("Port:"); 
  115.  
  116.     Serial.println(80); 
  117.  
  118.  
  119.  
  120. void connect_wifi(String cmd, int t) //This function is for connecting ESP8266 with wifi network by using AT commands 
  121.  
  122.  
  123.     int temp = 0, i = 0; 
  124.  
  125.     while (1) 
  126.  
  127.     { 
  128.  
  129.         Serial.println(cmd); //Sends to serial monitor 
  130.  
  131.         Serial2.println(cmd); //sends to ESP8266 via serial communication 
  132.  
  133.         while (Serial2.available()) 
  134.  
  135.         { 
  136.  
  137.             if (Serial2.find("OK")) 
  138.  
  139.                 i = 8; 
  140.  
  141.         } 
  142.  
  143.         delay(t); 
  144.  
  145.         if (i > 5) 
  146.  
  147.             break
  148.  
  149.         i++; 
  150.  
  151.     } 
  152.  
  153.     if (i == 8) 
  154.  
  155.         Serial.println("OK"); 
  156.  
  157.     else 
  158.  
  159.         Serial.println("Error"); 
  160.  
  161.  
  162.  
  163. void wifi_init() //This function contains AT commands that passes to connect_wifi() 
  164.  
  165.  
  166.     connect_wifi("AT", 100); //Sends AT command with time(Command for Achknowledgement) 
  167.  
  168.     connect_wifi("AT+CWMODE=3", 100); //Sends AT command with time (For setting mode of Wifi) 
  169.  
  170.     connect_wifi("AT+CWQAP", 100); //Sends AT command with time (for Quit AP) 
  171.  
  172.     connect_wifi("AT+RST", 5000); //Sends AT command with time (For RESETTING WIFI) 
  173.  
  174.     check4IP(5000); 
  175.  
  176.     if (!No_IP) 
  177.  
  178.     { 
  179.  
  180.  
  181.  
  182.         Serial.println("Connecting Wifi...."); 
  183.  
  184.         connect_wifi("AT+CWJAP=\"Pramo\",\"pokemon08\"", 7000); //provide your WiFi username and password here 
  185.  
  186.  
  187.  
  188.     } else 
  189.  
  190.     { 
  191.  
  192.     } 
  193.  
  194.     Serial.println("Wifi Connected"); 
  195.  
  196.     get_ip(); 
  197.  
  198.  
  199.  
  200.     connect_wifi("AT+CIPMUX=1", 100); //Sends AT command with time (For creating multiple connections) 
  201.  
  202.     connect_wifi("AT+CIPSERVER=1,80", 100); //Sends AT command with time (For setting up server with port 80) 
  203.  
  204.  
  205.  
  206. void sendwebdata(String webPage) //This function is used to send webpage datas to the localserver 
  207.  
  208.  
  209.     int ii = 0; 
  210.  
  211.     while (1) 
  212.  
  213.     { 
  214.  
  215.         unsigned int l = webPage.length(); 
  216.  
  217.         Serial.print("AT+CIPSEND=0,"); 
  218.  
  219.         Serial2.print("AT+CIPSEND=0,"); 
  220.  
  221.         Serial.println(l + 2); 
  222.  
  223.         Serial2.println(l + 2); 
  224.  
  225.         delay(100); 
  226.  
  227.         Serial.println(webPage); //sends webpage data to serial monitor 
  228.  
  229.         Serial2.println(webPage); //sends webpage data to serial2 ESP8266 
  230.  
  231.         while (Serial2.available()) 
  232.  
  233.         { 
  234.  
  235.  
  236.  
  237.             if (Serial2.find("OK")) 
  238.  
  239.             { 
  240.  
  241.                 ii = 11; 
  242.  
  243.                 break
  244.  
  245.             } 
  246.  
  247.         } 
  248.  
  249.         if (ii == 11) 
  250.  
  251.             break
  252.  
  253.         delay(100); 
  254.  
  255.     } 
  256.  
  257.  
  258.  
  259. void setup() 
  260.  
  261.  
  262.     Serial.begin(9600); //begins serial monitor with baud rate 9600 
  263.  
  264.     Serial2.begin(9600); //begins serial communication with esp8266 with baud rate 9600 (Change according to your esp8266 module) 
  265.  
  266.     wifi_init(); 
  267.  
  268.     Serial.println("System Ready.."); 
  269.  
  270.  
  271.  
  272. void loop() 
  273.  
  274.  
  275.     k = 0; 
  276.  
  277.     Serial.println("Please Refresh your Page"); 
  278.  
  279.     while (k < 1000) 
  280.  
  281.     { 
  282.  
  283.         k++; 
  284.  
  285.         while (Serial2.available()) 
  286.  
  287.         { 
  288.  
  289.             if (Serial2.find("0,CONNECT")) 
  290.  
  291.             { 
  292.  
  293.                 Serial.println("Start Printing"); 
  294.  
  295.                 Send(); 
  296.  
  297.                 Serial.println("Done Printing"); 
  298.  
  299.                 delay(1000); 
  300.  
  301.             } 
  302.  
  303.         } 
  304.  
  305.         delay(1); 
  306.  
  307.     } 
  308.  
  309.  
  310.  
  311. void Send() //This function contains data to be sent to local server 
  312.  
  313.  
  314.     webpage = "<h1>Welcome to Circuit Digest</h1><body bgcolor=f0f0f0>"
  315.  
  316.     sendwebdata(webpage); 
  317.  
  318.     webpage = name; 
  319.  
  320.     sendwebdata(webpage); 
  321.  
  322.     delay(1000); 
  323.  
  324.     webpage = "<a href=\"http://circuitdigest.com/\""
  325.  
  326.     webpage += "\">Click Here to get into circuitdigest.com</a>"
  327.  
  328.     webpage += data; 
  329.  
  330.     sendwebdata(webpage); 
  331.  
  332.     Serial2.println("AT+CIPCLOSE=0"); //Closes the server connection 
  333.  

结语

看到这里相信你已经知道了使用共ESP266与STM32通讯的整个思路和流程了,快拿你的设备来试一下吧,相信你也可以成功的!

到此这篇关于手把手带你使用ESP8266 与 STM32F103C8实现网络服务器的文章就介绍到这了,更多相关内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/qq_45172832/article/details/127241372

延伸 · 阅读

精彩推荐