步驟1:組裝
我們組裝的電路非常簡(jiǎn)單,并且組裝非常緊湊。我們?cè)诤竺娴膮^(qū)域中使用聚苯乙烯板將所有東西固定在原位。該板還用于輔助電源箱內(nèi)部的裝配,并避免暴露于組件中,因?yàn)樗捎糜诳刂谱≌械母鞣N設(shè)備,例如空調(diào),燈具等。
然后,我們使用開(kāi)關(guān)電源,我將其從110或220伏轉(zhuǎn)換為5伏。我們還有一個(gè)3v3穩(wěn)壓器AM1117。我們使用了兩個(gè)GPIO,并插入了中繼板輸入。重要的是要記住,使用ESP8266,我們必須采取一些預(yù)防措施,例如將引腳接地。
步驟2:Arduino IDE中的ESP8266
重要的是要記住,在編寫ESP8266時(shí),需要將此設(shè)備的庫(kù)加載到Arduino中。為此,您應(yīng)該使用1.6.4版的IDE。現(xiàn)在轉(zhuǎn)到首選項(xiàng)和“其他Board Manager URL”并添加URL:http://arduino.esp8266.com/stable/package_esp8266com_index.json
然后,轉(zhuǎn)到Tools》 Boards》 Boards Manager。 。.
在搜索中,輸入esp8266并安裝“ esp8266 by ESP8266 Community”軟件包。
現(xiàn)在,您可以從卡列表中選擇ESP8266
***在今天的安裝中,ESP866將是一臺(tái)服務(wù)器。因此,您將拿起智能手機(jī),它將連接到設(shè)備的IP中,這意味著您可以訪問(wèn)它,并且它將為您提供一個(gè)頁(yè)面。
在視頻中,您可以看到有關(guān)以下內(nèi)容的演示:
步驟3:源代碼
第一步是包含一個(gè)供我們控制ESP8266 WiFi的lib。之后,我們將創(chuàng)建一個(gè)變量,該變量將保存對(duì)將在端口80上運(yùn)行的服務(wù)器的引用。我們選擇端口80的原因是,這是http協(xié)議的默認(rèn)端口,并且我們將使用瀏覽器連接到
//Includes the lib for Wifi
#include
//Creates a server on port 80 (this is the default port for http requests)
WiFiServer server(80);
步驟4:設(shè)置
在設(shè)置中,我們將僅初始化Serial,以便使用
我們將使用GPIO0和GPIO2作為輸出,并使用LOW初始化初始狀態(tài)。
void setup()
{
//Initializes the Serial just for logging
Serial.begin(115200);
//Sets GPIO0 and GPIO2 as output, so we can change their value
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
//Puts the GPIO0 and GPIO2 in LOW output
digitalWrite(0, LOW);
digitalWrite(2, LOW);
我們現(xiàn)在將其稱為WiFi.begin(“ ssid”, “ password”)將ESP8266連接到路由器。在該示例中,我們具有ssid“ TestESP”和密碼“ 87654321”,但是必須將其替換為將要使用的網(wǎng)絡(luò)。
Serial.print(“Connecting”);
//Connects to your WiFi network. In this example the SSID is TestESP and the password is 87654321
WiFi.begin(“TestESP”, “87654321”);
我們將每100毫秒檢查一次查看ESP8266是否已連接到網(wǎng)絡(luò)(連接后返回WL_CONNECTED狀態(tài))。
When you leave the “while”, it means
that you have connected.
//While our ESP is trying to connect
while (WiFi.status() != WL_CONNECTED)
{
//Waits for 100 milliseconds
delay(100);
Serial.print(“。”);
}
//Here it‘s already connected, so we’ll just show a feedback on Serial Monitor
Serial.println(“”);
Serial.println(“Connected”);
這是我們放置網(wǎng)絡(luò)設(shè)置的位置。 IP,網(wǎng)關(guān)和掩碼設(shè)置必須根據(jù)您的網(wǎng)絡(luò)進(jìn)行更改。
//Settings for static ip
IPAddress ip(192, 168, 2, 8);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
Serial.print(“Static IP is: ”);
Serial.println(ip);
//Sends the settings to the WiFi router
WiFi.config(ip, gateway, subnet);
現(xiàn)在,我們可以初始化服務(wù)器并在串行監(jiān)視器上查看是否鏈接到ESP8266的IP與我們配置的相同。這是設(shè)置的結(jié)束。
//Starts the server we created on port 80
server.begin();
//Shows the IP for the server
Serial.print(“Server is on: ”);
Serial.println(WiFi.localIP());
}
步驟5:循環(huán)
在程序主循環(huán)中,我們會(huì)檢查是否有任何客戶端正在嘗試連接,如果連接成功,我們會(huì)等到他們返回他們的請(qǐng)求。
void loop()
{
//Checks if there is any client trying to connect
WiFiClient client = server.available();
if (!client)
{
//If there isn‘t, we just return
return;
}
Serial.println(“New Client Connected!”);
我們將請(qǐng)求存儲(chǔ)在變量“ req”中,以便以后知道該怎么做
//Reads the request
String req = client.readStringUntil(’ ‘);
Serial.print(“Request: ”);
Serial.println(req);
最后,我們關(guān)閉與客戶端的連接。這樣便完成了循環(huán)和代碼。
//Closes the connection
client.stop();
Serial.println(“Client disconnected!”);
}
測(cè)試
要進(jìn)行測(cè)試,只需打開(kāi)瀏覽器并輸入將出現(xiàn)在串行監(jiān)視器上的ip。單擊操作,然后查看相應(yīng)的GPIO是否正在更改。
-
繼電器
+關(guān)注
關(guān)注
132文章
5363瀏覽量
149682 -
ESP8266
+關(guān)注
關(guān)注
50文章
962瀏覽量
45415
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
繼電器在自動(dòng)化控制中的作用
時(shí)間繼電器在自動(dòng)化中的應(yīng)用
ESP8266 通過(guò) MQTT 協(xié)議實(shí)現(xiàn) LED 的遠(yuǎn)程控制
![<b class='flag-5'>ESP8266</b> 通過(guò) MQTT 協(xié)議<b class='flag-5'>實(shí)現(xiàn)</b> LED 的遠(yuǎn)程控制](https://file1.elecfans.com/web2/M00/08/AB/wKgaombykEOAJs4pAALJ0N47550043.png)
esp32和esp8266代碼共用嗎
esp8266和esp32區(qū)別是什么
esp8266不燒錄可以使用嗎
esp8266wifi模塊怎么連接手機(jī)
ESP8266控制繼電器,芯片會(huì)頻繁復(fù)位的原因?
如何讓W(xué)indows 8自動(dòng)連接到ESP8266?
請(qǐng)問(wèn)esp8266如何擴(kuò)展pwm到8路?
繼電器在工業(yè)自動(dòng)化領(lǐng)域的應(yīng)用
國(guó)產(chǎn)低成本W(wǎng)i-Fi SoC解決方案芯片ESP8266與ESP8285對(duì)比差異
![國(guó)產(chǎn)低成本W(wǎng)i-Fi SoC解決方案芯片<b class='flag-5'>ESP8266</b>與<b class='flag-5'>ESP</b>8285對(duì)比差異](https://file1.elecfans.com/web2/M00/E6/5C/wKgZomZG0KiASP_yAAELgTM0cRs433.png)
評(píng)論