講了那麼多天的理論,現在該來讓各位多動手實作啦,今天主要是會運用Esp32s內建的WiFi來進行實作,並融合兩個LED燈,達到可用手機連結Esp32s就可遠端操控LED的亮與滅。
Esp32s的WiFi有三種運作模式,分別是AP、STA及AP/STA
- AP mode
手機連接Esp32s (Esp32s當基地台)- STA mode
Esp32s去連接AP (手機或WiFi分享器當基地台)
而我這樣文字敘述肯定很多人到現在還是搞不清楚,所以我準備了一張圖片
圖片取自:使用者繪製
其實我覺得最大的區別就是當Esp32s開啟STA mode時,可以去連接手機或AP基地台,而這時候Esp32s也就具備網路,可以上網抓數值,例如抓取PM2.5的數值,並用網頁或LCD去做顯示,可以做出更多不同的應用。
那接下來就直接進入主題吧,上次的實作呼吸燈還記得吧,不記得罰你回去複習『Day6 呼吸燈製作』,那上次只是讓LED燈自己跑程式,跑出呼吸燈的效果,但是今天就比較不同,是自己手動控制LED燈的亮滅狀態,那就直接看接線圖吧。
圖片取自:使用者拍攝
許多人應該會想著,為什麼LED不用接地跟為什麼黃色那條要往下接到下方GND,那是因為A區的LED已經在板中已經牽線至下方GND區塊,所以A區LED要接地只需要把開發板的GND接線至下方GND區即可接地,對了還有LED有4顆,隨便挑兩顆就好。
請修改第四行(ssidAP)及第五行(passwordAP)後方的"xxxx",請將資訊輸入至""中,千萬不可把""刪除,xxxx可刪除,修改完畢後就可進行燒入,如果忘記如何燒入可以回顧『Day4 Arduino測試燒錄』。
#include <WiFi.h>
// Replace with your network credentials
const char* ssidAP = "xxxx"; //Esp32s基地台名稱(隨便都可以,要英文且盡量不要跟附近的WiFi撞名)
const char* passwordAP = "xxxxxxxx";//Esp32s基地台密碼(至少要8碼,例如12345678)
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output0State = "off";
String output2State = "off";
// Assign output variables to GPIO pins
const int output0 = 23;
const int output2 = 22;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output0, OUTPUT);
pinMode(output2, OUTPUT);
// Set outputs to LOW
digitalWrite(output0, LOW);
digitalWrite(output2, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssidAP, passwordAP);
delay(500);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /23/on") >= 0) {
Serial.println("GPIO 23 on");
output0State = "on";
digitalWrite(output0, HIGH);
} else if (header.indexOf("GET /23/off") >= 0) {
Serial.println("GPIO 23 off");
output0State = "off";
digitalWrite(output0, LOW);
} else if (header.indexOf("GET /22/on") >= 0) {
Serial.println("GPIO 22 on");
output2State = "on";
digitalWrite(output2, HIGH);
} else if (header.indexOf("GET /22/off") >= 0) {
Serial.println("GPIO 22 off");
output2State = "off";
digitalWrite(output2, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 23
client.println("<p>GPIO 23 - State " + output0State + "</p>");
// If the output0State is off, it displays the ON button
if (output0State=="off") {
client.println("<p><a href=\"/23/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/23/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 22
client.println("<p>GPIO 22 - State " + output2State + "</p>");
// If the output2State is off, it displays the ON button
if (output2State=="off") {
client.println("<p><a href=\"/22/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/22/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
還記得前面設置的ssid名稱及密碼嗎?
如果燒錄成功,就可開啟手機WiFi,就會看見剛剛設置的ssid名稱,例如我設置"Esp32_Web_LED_AP",那就會跟下方圖片相同
圖片取自:使用者拍攝
成功連結WiFi會發現沒有無網際網路,這是正常現象,切記不要讓手機自動切換至有網際網路的WiFi,後續呢,請打開瀏覽器(chrome或什麼瀏覽器都可),在網址列上輸入"192.168.4.1",連接至該網址後會看見下方畫面
圖片取自:使用者拍攝
那這樣代表連接成功囉,接下來可以玩囉,點擊ON即可讓LED亮起(前提線有插好)。
好了,是不是有進入實作的狀態啦,明天會稍微講解基本操作+程式碼,後續就敢用STA mode執行LED啦,是不是很期待更後面章節的實作呀,如果各位有興趣,也可以趁有空時自己研究此次程式碼中可以做些何種有趣修改,讓本次程式碼不只讓LED亮滅那麼簡單,那今天的部分就這些,各位明天見囉~