前言
随着现在物联网设备的而越来越多,现在市场上出现越来越多的物联网设备,其中 ESP8266 是最受欢迎、价格便宜且易于使用的模块,它可以将您的硬件连接到互联网。
今天我们就以ESP8266和STM32来实现一台网络服务器,我们使用 ESP8266 将 STM32F103C8 连接到互联网。 ESP8266 Wi-Fi 模块与 STM32F103C8 板接口,并将数据发送到 ESP8266 网络服务器上托管的网页。
所需硬件
- STM32F103C8板
- ESP8266 无线无线模块
- 笔记本电脑和无线网络热点
ESP8266 模块
大多数人将 ESP8266 称为 WIFI 模块,但它实际上是一个微控制器。ESP8266 是乐鑫公司(一家总部位于上海的公司)开发的微控制器的名称。该微控制器具有执行WIFI相关活动的能力,因此被广泛用作WIFI模块。
引脚说明
- GND:接地
- TX:发射传输数据位
- GPIO2:通用输入/输出 2
- CH_PD:使能
- GPIO0:通用输入/输出0
- RST:复位
- RX:接收数据位
- VCC:+3.3 V
常用AT指令
AT 命令用于与 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 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代码
webpage = "<h1>Welcome to Circuit Digest</h1><body bgcolor=f0f0f0>"; //This is the heading line with black font colour
String name="<p>Circuit Digest</p><p>A community of electrical and electronics students, engineers and makers</p>";
String data="<p>Data Received Successfully.....</p>"; //These two lines are of two paragraph
webpage = "<a href=\"http://circuitdigest.com/\"";
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,如下所示
单击链接后,您会在网页上看到一条文本,上面写着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 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
}
结语
看到这里相信你已经知道了使用共ESP266与STM32通讯的整个思路和流程了