本文要介紹如何使用Arduino IDE 與 NodeMCU 互動。
所需硬體:
1. NodeMCU 板子 x 1
2. Micro USB Cable x 1
3. Wifi Ap x 1
範例一:透過Arduino IDE直接控制NodeMCU上的LED
選擇偏好設定
將下列網址複製到額外的板子管理員網址
http://arduino.esp8266.com/stable/package_esp8266com_index.json
開啟板子管理員
安裝 esp8266 by ESP8266 Community 套件
選擇 NodeMCU 1.0 (ESP-12E Module) 板子
選擇ESP8266內建的範例 Blink
上傳程式到NodeMCU之前,請檢查 NodeMCU 的連接埠為多少,在我的電腦上為COM3。
若沒問題的話,可看到如下影片:
範例二:網頁式控制NodeMCU上的LED
將下列程式碼中的your_wifi_ap_ssid與your_wifi_ap_password修改為要使用的Wifi Ap SSID 與 password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
const char* ssid = "your_wifi_ap_ssid"; | |
const char* password = "your_wifi_ap_password"; | |
int ledPin = LED_BUILTIN; | |
WiFiServer server(80); | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(ledPin, HIGH); | |
// 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.print("Use this URL to connect: "); | |
Serial.print("http://"); | |
Serial.print(WiFi.localIP()); | |
Serial.println("/"); | |
} | |
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 request = client.readStringUntil('\r'); | |
Serial.println(request); | |
client.flush(); | |
// Match the request | |
int value = HIGH; | |
if (request.indexOf("/LED=ON") != -1) { | |
digitalWrite(ledPin, LOW); | |
value = LOW; | |
} | |
if (request.indexOf("/LED=OFF") != -1) { | |
digitalWrite(ledPin, HIGH); | |
value = HIGH; | |
} | |
// Return the response | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println(""); // do not forget this one | |
client.println("<!DOCTYPE HTML>"); | |
client.println("<html>"); | |
client.print("Led pin is now: "); | |
if(value == LOW) { | |
client.print("On"); | |
} else { | |
client.print("Off"); | |
} | |
client.println("<br><br>"); | |
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>"); | |
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />"); | |
client.println("</html>"); | |
delay(1); | |
Serial.println("Client disonnected"); | |
Serial.println(""); | |
} | |
上傳程式到NodeMCU後,請開啟Arduino IDE 中的序列埠監控視窗
可看到NodeMCU的IP位址,此例為 192.168.1.110
用瀏覽器開啟此網址,即可透過網頁控制NodeMCU上的LED燈
參考資料:
[1]Quick Start to Nodemcu (ESP8266) on Arduino IDE
沒有留言:
張貼留言