隨著現在物聯網設備的而越來越多,現在市場上出現越來越多的物聯網設備,其中 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?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?=?"Welcome?to?Circuit?Digest
《21世纪爱情指南》剧情介绍,孤男寡女免费看电视剧战狼3,国产免费观看高清电视剧在线,暴躁女孩免费看全集,女战狼10免费观看全部,爱我几何电影完整无删减免费观看
,灭火宝贝2凯登克劳斯结局,牙医姐妹 在线观看";?//This?is?the?heading?line?with?black?font?colour
String?name="Circuit?Digest
A?community?of?electrical?and?electronics?students,?engineers?and?makers
";
String?data="Data?Received?Successfully.....
";?????//These?two?lines?are?of?two?paragraph
webpage?=?";
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?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?=?"Circuit?Digest
A?community?of?electrical?and?electronics?students,?engineers?and?makers
";?//String?with?html?notations
String?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通訊的整個思路和流程了,快拿你的設備來試一下吧,相信你也可以成功的!


推薦閱讀
01 |加入嵌入式交流群 |
02 |嵌入式資源獲取 |
03 |STM32中斷優先級詳解 |
04 |STM32下載程序新思路--使用串口下載程序 |

