根据板卡原理RGB三色LED对应引脚:
int LEDR=12、int LEDG=14、int LEDB=13;
设置串口波特率为115200
Serial.begin(115200);
源代码如下所示:
#include <ESP8266WiFi.h> // 提供 Wi-Fi 功能的库
#include <ESP8266WebServer.h> // 提供网站服务器功能的库
const char ssid[] ="*************";//你的WIFI名称
const char pass[] ="************";//你的WIFI密码
int LEDR=12;
int LEDG=14;
int LEDB=13;
ESP8266WebServer server(80); // 声明网站服务器对象与端口号
void pageIndex(){
String HTML="<!DOCYPYE html><html><meta charset=\"UTF-8\"/><title>LYU-IOT控制LED</title><body>\
<h1 align=\"center\">网页控制LYU-IOT-ESP12F三色LED亮灭</h1>\
<p><form action=\"/LB\" method=\"POST\" align=\"center\"><input type=\"submit\" value=\"LEDB闪烁控制\"> </form></p>\
<p><form action=\"/LG\" method=\"POST\" align=\"center\"><input type=\"submit\" value=\"LEDG闪烁控制\"> </form></p>\
<p><form action=\"/LR\" method=\"POST\" align=\"center\"><input type=\"submit\" value=\"LEDR闪烁控制\"> </form></p>\
</body></html>";
server.send(200,"text/html",HTML);
}
void LEDBControl(){
digitalWrite(LEDB,!digitalRead(LEDB));
server.sendHeader("Location","/");
server.send(303);
Serial.println("\r\n LEDB is work\r\n");
}
void LEDGControl(){
digitalWrite(LEDG,!digitalRead(LEDG));
server.sendHeader("Location","/");
server.send(303);
Serial.println("\r\n LEDG is work\r\n");
}
void LEDRControl(){
digitalWrite(LEDR,!digitalRead(LEDR));
server.sendHeader("Location","/");
server.send(303);
Serial.println("\r\n LEDR is work\r\n");
}
void PageNULL(){
server.send(404,"text/plain","404 Not Found The Server");
}
void setup() {
pinMode(LEDG, OUTPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDB, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, pass);
// 若要指定 IP 地址,请自行在此加入 Wi-Fi.config() 语句
while (WiFi.status() != WL_CONNECTED)
{
delay(500); // 等待 Wi-Fi 连接
Serial.print("." );
}
Serial.println("");
Serial.print("WiFi connected, IP:");
Serial.println(WiFi.localIP()); // 显示 ESP8266 设备的 IP 地址
server.begin();
server.on("/",HTTP_GET,pageIndex);
server.on("/LB",HTTP_POST,LEDBControl);
server.on("/LG",HTTP_POST,LEDGControl);
server.on("/LR",HTTP_POST,LEDRControl);
server.onNotFound(PageNULL);
Serial.println("HTTP server started.");
}
void loop() {
server.handleClient(); // 处理用户连接
}
运行结果如图所示: