前言 随着现在物联网设备的而越来越多,现在市场上出现越来越多的物联网设备,其中 ESP8266 是最受欢迎、价格便宜且易于使用的模块,它可以将您的硬件连接到互联网。 今天我们就以ESP8266和ST
随着现在物联网设备的而越来越多,现在市场上出现越来越多的物联网设备,其中 ESP8266 是最受欢迎、价格便宜且易于使用的模块,它可以将您的硬件连接到互联网。
今天我们就以ESP8266和STM32来实现一台网络服务器,我们使用 ESP8266 将 STM32F103C8 连接到互联网。 ESP8266 Wi-Fi 模块与 STM32F103C8 板接口,并将数据发送到 ESP8266 网络服务器上托管的网页。
大多数人将 ESP8266 称为 WIFI 模块,但它实际上是一个微控制器。ESP8266 是乐鑫公司(一家总部位于上海的公司)开发的微控制器的名称。该微控制器具有执行WIFI相关活动的能力,因此被广泛用作WIFI模块。
引脚说明
GND
:接地TX
:发射传输数据位GPio2
:通用输入/输出 2CH_PD
:使能GPIO0
:通用输入/输出0RST
:复位RX
:接收数据位VCC
:+3.3 VAT 命令用于与 ESP8266 通信。下表显示了一些常用的 AT 命令
下图显示了STM32和ESP8266无线模块之间的连接。
SMT32F103C8具有三套UART串行通信。在下图中,您可以看到相同的以下引脚:
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 COMMANDif(Serial2.find("STaiP,")) //This finds the STAIP that is the STATIC IP ADDRESS of ESP8266Serial.print(IP); //prints IP address in Serial monitor
接下来,我们将为网页编写 html 代码。要将HTML代码转换为Arduino代码,您可以使用此链接。
WEBpage = "Welcome to Circuit Digest
"; //This is the heading line with black font colourString name="Circuit Digest
A commUnity of electrical and electronics students, engineers and makers
";String data="Data Received Successfully.....
"; //These two lines are of two paragraphwebpage = "Http://circuitdigest.com/\"";webpage+="\">Click Here to get into circuitdigest.com"; //At last we insert the hyperlink to link the website address
接下来在void send()
函数中,我们使用发送网络数据函数打印了HTML,并使用AT + CIPCLOSE = 0
关闭了服务器连接
完成所有工作后,您可以通过在任何 Web 浏览器中打开 ESP8266 的 IP 并单击网页上显示的链接来测试工作,单击此处进入 circuitdigest.com
,如下所示
单击链接后,您会在网页上看到一条文本,上面写着Data Received Successfully.....
//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 charactersint i = 0, k = 0, x = 0; //integer variablesString readString; //using readString feature to read characters boolean No_IP = false; //boolean variables String IP = ""; //String variable to store datachar temp1 = '0'; //character variableString name = "Circuit Digest
A community of electrical and electronics students, engineers and makers
"; //String with html notationsString data = "Data Received Successfully.....
"; //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 = "Welcome to Circuit Digest
"; sendwebdata(webpage); webpage = name; sendwebdata(webpage); delay(1000); webpage = "; webpage += "\">Click Here to get into circuitdigest.com"; webpage += data; sendwebdata(webpage); Serial2.println("AT+CIPCLOSE=0"); //Closes the server connection}
看到这里相信你已经知道了使用共ESP266与STM32通讯的整个思路和流程了,快拿你的设备来试一下吧,相信你也可以成功的!
👇点击下方公众号卡片获取资料👇来源地址:https://blog.csdn.net/qq_45172832/article/details/127241372
--结束END--
本文标题: 手把手带你使用ESP8266 与 STM32F103C8实现网络服务器
本文链接: https://lsjlt.com/news/500135.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0