創作內容

18 GP

ESP8266 wifi 使用教學 (含韌體更新 以Arduino為範例開發程式

作者:貓貓風 ฅ●ω●ฅ│2022-11-13 01:34:14│巴幣:1,034│人氣:4169
.










市面上 ESP8266 Wifi模組有很多種 Type,但是使用與韌體更新的方式都相同

通常在購買時就可以會知道自己買的種類是哪種 通常代號會是 ESP - XX (XX為編號


剛拿到此模組一定要韌體更新才能使用  以下為韌體更新方式

將ESP8266模組 接上 TTL 轉 USB模組

通常有兩款  CH340 或是 CP2102

如果本身模組有自帶USB頭的就可以直接跟PC對接

如果不是就按照以下接法    示範為使用 ESP8266 + CH340

按照以下接法  每個pin腳都會標示代表的是什麼 用杜邦線 母母對接就可以
ESP8266      CH340
3.3V              3.3v
Rx                 Tx
Tx                 Rx
Gnd              Gnd

ESP8266 pin腳的位置不一定  請依照購買模組的標示進行對接  以下為示意圖

藍色圈起來的部分就是要按照上面說明對接的部分
接好之後從裝置管理員 會看到 CH340的 Comport 如果顯示驚嘆號表示沒裝 Driver

下圖顯示 PC指定的Comport編號為 COM5  之後在韌體更新時要選的編號  要先記下


韌體更新的軟體連結如下 (壓縮檔內含 要升級的檔案


如果是買 ESP8266的uno擴充版 在更新前須要把 Debug模式打開  才可以進行更新

如下圖  把Dip Switch 3 跟 4 往上撥



依照自己手邊的ESP8266 與  TTL to USB接好後 連接至PC  然後打開剛剛下載的韌體更新軟體

會出現以下畫面

一開始為 IDLE 按下Start 如果有出現MAC Address表示連接成功  下一步就可以開始更新

如果出現錯誤有以下原因  

1.連接 CH340的 TX RX相反  

2. switch沒有換成Debug模式

3. COM選擇錯誤 (請到控制台確認CH340出現的編號)

4. BAUD錯誤,一開始預設都要用 115200才可以正常連接



接著在上面 Dowload Path Config選擇 0x00000的位置  然後加入這個檔案

下圖為剛剛下載的韌體更新程式的下一層路徑,選擇紅框處的檔案


加入後再打勾  如下圖


然後按Start進行韌體更新  下面的藍色進度條會漸增  等滿後表示完成  就可以關閉這軟體了



接著使用下面我開發的 ESP8266 AT Command測試程式進行版本確認


開啟後先選擇CH340的COM  如果不知道可以在裝置管理員看

點選第一個 AT Test  如果 ESP8266回傳 AT OK  表示目前已進入 AT模式


接著確認韌體版本  點 AT + GMR

如果出現 SDK Version: 1.5.4.1(39cb9a32)表示韌體更新成功



AT+CWMODE 可以變更 ESP8266的模式  看使用需求進行設定

1. Station Mode  2. AP Mode 3. Station + AP Mode

ESP8266回傳 OK表示成功設定

設定完成後可以按 AT+CWMODE? 讀取設定  確認是否有成功


設定 AP 模式時的 SSID 跟 密碼   (只有在 AP MODE時才可以進行設定)

設定完成一樣回復 OK

用 AT+CWSAP_DEF?可以讀取設定值  確認是否有更改成功

設定 Station模式時的 IP GateWay跟 Mask


完成以上設定就可以使用了  
(後面的 AP 跟 Station設定 如果之後的程式碼有在指定  會被改寫掉  因此可以先不設定)

設定完後切到 AP模式  手機在搜尋會出現剛剛設定的 AP名稱  


然後輸入剛剛的密碼可以成功連接

有些版本的ESP8266模組  會利用預設的SSID跟密碼進入進行內部細項設定

不用在另外下AT Command去改寫那些參數


接著就可以開始進行 ESP8266的程式編寫了

使用Arduino IDE進行編程

打開Arduino IDE  先下載開發版相關組件 ->在偏好設定紅框處輸入下圖網址

https://arduino.esp8266.com/stable/package_esp8266com_index.json


接著在 工具 ->開發版 -> 開發版管理  輸入 ESP8266 找到組件  按安裝


安裝完成後  在開發板就會有 ESP8266的選項  依照自己購買的版本進行選擇

下面選項是我用來演示的這塊型號


選好之後就可以開始 寫CODE了

下面寫一個簡單的功能  將ESP8266當成Web Server

並將ESP8266的兩個IO引腳個別接上LED  之後用WIFI接收指令做控制

手機用wifi連上後  可以進入頁面進行LED的開關操作


  1. // Load Wi-Fi library
  2. #include <ESP8266WiFi.h>
  3.  
  4. // Replace with your network credentials
  5. const char* ssid     = "yourssid";
  6. const char* password = "yourpassword";
  7.  
  8. // Set web server port number to 80
  9. WiFiServer server(80);
  10.  
  11. // Variable to store the HTTP request
  12. String header;
  13.  
  14. // Auxiliar variables to store the current output state
  15. String output5State = "off";
  16. String output4State = "off";
  17.  
  18. // Assign output variables to GPIO pins
  19. const int output5 = 5;
  20. const int output4 = 4;
  21.  
  22. // Current time
  23. unsigned long currentTime = millis();
  24. // Previous time
  25. unsigned long previousTime = 0;
  26. // Define timeout time in milliseconds (example: 2000ms = 2s)
  27. const long timeoutTime = 2000;
  28.  
  29. void setup() {
  30.   Serial.begin(115200);
  31.   // Initialize the output variables as outputs
  32.   pinMode(output5, OUTPUT);
  33.   pinMode(output4, OUTPUT);
  34.   // Set outputs to LOW
  35.   digitalWrite(output5, LOW);
  36.   digitalWrite(output4, LOW);
  37.  
  38.   // Connect to Wi-Fi network with SSID and password
  39.   Serial.print("Connecting to ");
  40.   Serial.println(ssid);
  41.   WiFi.begin(ssid, password);
  42.   while (WiFi.status() != WL_CONNECTED) {
  43.     delay(500);
  44.     Serial.print(".");
  45.   }
  46.   // Print local IP address and start web server
  47.   Serial.println("");
  48.   Serial.println("WiFi connected.");
  49.   Serial.println("IP address: ");
  50.   Serial.println(WiFi.localIP());
  51.   server.begin();
  52. }
  53. int connect_count = 0;
  54. void loop(){
  55.   WiFiClient client = server.available();   // Listen for incoming clients
  56.  
  57.   if (client) {                             // If a new client connects,
  58.     Serial.println("New Client.");          // print a message out in the serial port
  59.     String currentLine = "";                // make a String to hold incoming data from the client
  60.     currentTime = millis();
  61.     previousTime = currentTime;
  62.     while (client.connected() && currentTime - previousTime <= timeoutTime) {
  63.       currentTime = millis();         
  64.       if (client.available()) {             // if there's bytes to read from the client,
  65.         char c = client.read();             // read a byte, then
  66.         Serial.write(c);                    // print it out the serial monitor
  67.         header += c;
  68.         if (c == '\n') {                    // if the byte is a newline character
  69.           // if the current line is blank, you got two newline characters in a row.
  70.           // that's the end of the client HTTP request, so send a response:
  71.           if (currentLine.length() == 0) {
  72.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  73.             // and a content-type so the client knows what's coming, then a blank line:
  74.             client.println("HTTP/1.1 200 OK");
  75.             client.println("Content-type:text/html");
  76.             client.println("Connection: close");
  77.             client.println();
  78.  
  79.             // turns the GPIOs on and off
  80.             if (header.indexOf("GET /5/on") >= 0) {
  81.               Serial.println("GPIO 5 on");
  82.               output5State = "on";
  83.               digitalWrite(output5, HIGH);
  84.             } else if (header.indexOf("GET /5/off") >= 0) {
  85.               Serial.println("GPIO 5 off");
  86.               output5State = "off";
  87.               digitalWrite(output5, LOW);
  88.             } else if (header.indexOf("GET /4/on") >= 0) {
  89.               Serial.println("GPIO 4 on");
  90.               output4State = "on";
  91.               digitalWrite(output4, HIGH);
  92.             } else if (header.indexOf("GET /4/off") >= 0) {
  93.               Serial.println("GPIO 4 off");
  94.               output4State = "off";
  95.               digitalWrite(output4, LOW);
  96.             }
  97.  
  98.             // Display the HTML web page
  99.             client.println("<!DOCTYPE html><html>");
  100.             client.println("<head><meta name=\"viewport\" content=\"width=device-width,
  101.              initial-scale=1\">");
  102.             client.println("<link rel=\"icon\" href=\"data:,\">");
  103.             // CSS to style the on/off buttons

  104.             client.println("<style>html { font-family: Helvetica; display: inline-block; margin:
  105.              0px auto; text-align: center;}");
  106.             client.println(".button { background-color: #195B6A; border: none; color: white;
  107.             padding: 16px 40px; margin-left:5px;");
  108.             client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor:
  109.              pointer;}");
  110.             client.println(".button2 {background-color: #77878A;}</style></head>");
  111.  
  112.             // Web Page Heading
  113.             client.println("<body     
  114.             background=
  115.              \"https://img2.huashi6.com/images/resource/fhlt83f4plfqebsxlduklafeqm1m.jpg?
  116.               imageView2/3/q/75/interlace/1/w/600/h/600\"><p style=\"color:blue\">
  117.               <h1>ESP8266 Web Server</h1></p>");
  118.  
  119.             // Display current state, and ON/OFF buttons for GPIO 5  
  120.             client.println("<p style=\"color:blue\">GPIO 5 - State " + output5State + "</p>");
  121.             // If the output5State is off, it displays the ON button       
  122.             if (output5State=="off") {
  123.               client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a>
  124.                </p>");
  125.             } else {
  126.               client.println("<p><a href=\"/5/off\"><button class=\"button
  127.               button2\">OFF</button></a></p>");
  128.             }
  129.  
  130.             // Display current state, and ON/OFF buttons for GPIO 4  
  131.             client.println("<p style=\"color:blue\">GPIO 4 - State " + output4State + "</p>");
  132.             // If the output4State is off, it displays the ON button       
  133.             if (output4State=="off") {
  134.               client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a>
  135.               </p>");
  136.             } else {
  137.               client.println("<p><a href=\"/4/off\"><button class=\"button
  138.               button2\">OFF</button></a></p>");
  139.             }
  140.             connect_count++;
  141.             client.println("<p style=\"color:blue\"><font size=\"5\">Query
  142.             Count:"+String("")+connect_count+"</font></p>");
  143.             client.println("</body></html>");
  144.  
  145.             // The HTTP response ends with another blank line
  146.             client.println();
  147.             // Break out of the while loop
  148.             break;
  149.           } else { // if you got a newline, then clear currentLine
  150.             currentLine = "";
  151.           }
  152.         } else if (c != '\r') {  
  153.           currentLine += c;      // add it to the end of the currentLine
  154.         }
  155.       }
  156.     }
  157.     // Clear the header variable
  158.     header = "";
  159.     // Close the connection
  160.     client.stop();
  161.     Serial.println("Client disconnected.");
  162.     Serial.println("");
  163.   }
  164. }

接著將程式碼上傳,如果顯則有DIP SWITCH的ESP8266記得的要將3 4開關上撥

按RESET 後再開始上傳


上傳完畢後  如果是有SWITCH的版本  要將 3  4開關往下撥回來 按RESET後  才可以正常使用


開始運行後CONSOLE畫面出現連接手機AP成功  出現IP


手機用瀏覽器輸入那個IP  就會連到 ESP8266產生的網頁

之後就可以進行遠端控制



以下為手機端呈現的畫面

下面的開關會反映現在LED的控制狀態



硬體呈現結果











相關 MCU創作









引用網址:https://home.gamer.com.tw/TrackBack.php?sn=5599866
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:ESP8266|Aduino|WiFi

留言共 2 篇留言

ㄅㄅ(想吃麻糬 mode
喔喔這我大二也有玩過

11-13 08:39

貓貓風 ฅ●ω●ฅ
[e12]11-13 13:41
騙人的封弊者
之前做專題有設定過,結果測試時燒掉了要重設,真的是麻煩的東西

11-13 20:12

貓貓風 ฅ●ω●ฅ
可以直接寫程式自動做設定 把AT包在程式碼裡面 自動執行就會快很多11-13 20:14
我要留言提醒:您尚未登入,請先登入再留言

18喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:PC自製小草神納西妲主題... 後一篇:ESP8266 WiFi...

追蹤私訊切換新版閱覽

作品資料夾

flys8028大家
美食、旅遊相關的部落格,有興趣歡迎走走 https://www.fatnyanya.com/看更多我要大聲說昨天12:58


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】