4331| 1
|
[入门] NodeMCU1.0 简单创建HTTP服务器 |
通过Arduino IDE里的ESP8266库文件,创建一个简单的HTTP服务器。程序里输入本地WiFi名称及密码,会分配到一个IP地址,通过浏览器登陆这个IP地址,进入到创建在ESP8266内的HTTP服务器页面。 [mw_shl_code=c,true]#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> const char ssid[] = "SSID"; const char pass[] = "密码"; ESP8266WebServer server(80); void rootRouter(){ server.send(200, "text/html", "Hello from <b>ESP8266</b>!"); } void setup() { Serial.begin(115200); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("WiFi connected, IP: "); Serial.println(WiFi.localIP()); server.on("/index.html", rootRouter); server.on("/", rootRouter); server.onNotFound([](){ server.send(404, "text/plain", "File NOT found!"); }); server.begin(); Serial.println("HTTP server started."); } void loop() { server.handleClient(); }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed