void setup() { pinMode(2,OUTPUT); } void loop() { digitalWrite(2,HIGH); delay(1000); digitalWrite(2,LOW); delay(1000); } ``` 程式裡2的意思是使用GPIO2,因為不需與UNO連接,所以TX.RX這兩個IO也可以拿來使用,這樣板上一共有4個GPIO可用(0、1、2、3),如下圖: ![](http://maker.tn.edu.tw/uploads/tad_book3/image/esp-01-pinout.jpg) 程式寫好後,記得先檢查一下USB的序列埠有沒有選對,再按上傳 ![](http://maker.tn.edu.tw/uploads/tad_book3/image/upload.png) 程式上傳成功後,開始接線,請注意ESP-01上面的**VCC與CH\_PD要接到電源板的3.3V**,若接到5V可能會導致晶片燒燬!! ![](http://maker.tn.edu.tw/uploads/tad_book3/image/blink.png) 線路接好,再把電源板插上變壓器,就可以看到LED燈開始一閃一閃了 ### 搭上網路 ESP-01本身是有連網能力的,接著我們用內建的範例來看看它強大的功能, 開啟Arduino軟體,從檔案>>範例>>ESP8266WiFi>>WiFiWebServer ![](http://maker.tn.edu.tw/uploads/tad_book3/image/esp8266wifi_webserver.png) 程式內容如下,這裡我們只需要修改你所在環境的無線網路SSID與密碼(第12、13行),再上傳即可,有夠簡單吧~ ``` /* * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * http://server_ip/gpio/0 will set the GPIO2 low, * http://server_ip/gpio/1 will set the GPIO2 high * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include const char* ssid = "MY_SSID"; const char* password = "MY_PASSWORD"; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int val; if (req.indexOf("/gpio/0") != -1) val = 0; else if (req.indexOf("/gpio/1") != -1) val = 1; else { Serial.println("invalid request"); client.stop(); return; } // Set GPIO2 according to the request digitalWrite(2, val); client.flush(); // Prepare the response String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now "; s += (val)?"high":"low"; s += "\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed } ``` 上傳完成後,接好線路(電路圖同上面blink範例的接法),接著找出你的ESP-01配發到的IP是多少(進無線AP管理介面查看) 以本例取得的IP是192.168.0.104 只要用在瀏覽器的網址列輸入 http://192.168.0.104/gpio/1 就可以點亮LED燈 輸入 http://192.168.0.104/gpio/0 關閉LED燈 ![](http://maker.tn.edu.tw/uploads/tad_book3/image/DSCF2329.jpg) 把LED改成繼電器,那您就可以透過網路來開關家裡的電扇電燈等電器了
/* * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * http://server_ip/gpio/0 will set the GPIO2 low, * http://server_ip/gpio/1 will set the GPIO2 high * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include const char* ssid = "MY_SSID"; const char* password = "MY_PASSWORD"; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int val; if (req.indexOf("/gpio/0") != -1) val = 0; else if (req.indexOf("/gpio/1") != -1) val = 1; else { Serial.println("invalid request"); client.stop(); return; } // Set GPIO2 according to the request digitalWrite(2, val); client.flush(); // Prepare the response String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now "; s += (val)?"high":"low"; s += "\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed } ``` 上傳完成後,接好線路(電路圖同上面blink範例的接法),接著找出你的ESP-01配發到的IP是多少(進無線AP管理介面查看) 以本例取得的IP是192.168.0.104 只要用在瀏覽器的網址列輸入 http://192.168.0.104/gpio/1 就可以點亮LED燈 輸入 http://192.168.0.104/gpio/0 關閉LED燈 ![](http://maker.tn.edu.tw/uploads/tad_book3/image/DSCF2329.jpg) 把LED改成繼電器,那您就可以透過網路來開關家裡的電扇電燈等電器了