||
然后 重启ide,打开:工具->开发板->开发板管理
稍等一会儿,然后拉到最下面,安装 esp8266 by ESP8266 Community
安装成功后,根据所用的开发板情况,选择合适的开发板,后插入开发板,还要选择合适的端口。
将网页代码压缩
<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><title>Document</title></head><body><a href=\"./pin?light=on\"><input type=\"button\" value=\"开灯\"></a><a href=\"./pin?light=off\"><input type=\"button\" value=\"关灯\"></a></body></html>
编写arduino代码
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "01";//你的wifi名
const char* password = "shshshsh301";//你的wifi密码
ESP8266WebServer server(80);
String html = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><title>Document</title></head><body><a href=\"./pin?light=on\"><input type=\"button\" value=\"开灯\"></a><a href=\"./pin?light=off\"><input type=\"button\" value=\"关灯\"></a></body></html>";//压缩后的网页代码
void pin(){
if(server.arg("light")=="on"){
digitalWrite(D4, LOW);//开发板D4引脚低电平
server.send(200, "text/html",html);
} else if(server.arg("light")=="off"){
digitalWrite(D4, HIGH);//开发板D4引脚高电平
server.send(200, "text/html",html);
}
}
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/html",html);
digitalWrite(led, 0);
}
void handleNotFound(){
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void){
pinMode(D4, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/pin", HTTP_GET, pin);
server.on("/inline", [](){
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
烧进开发板里就可以了。
按开发板rst键,打开arduino的串口监视器
工具》串口监视器
查看esp8266是否连上无线网
连上就可以控制led灯了
在浏览器中esp8266的ip地址,就可以控制led灯的亮和灭