与FTDI上传板下面的代码,加载到ESP8266。你需要改变路由器的SSID和密码的变量,但这应该是它。在你上传的代码,在Arduino IDE打开串口监视器。一旦代码上传,它会写模块的IP地址序列-你要注意。
#包括<< esp8266wifi。H >
#包括<< esp8266webserver。H >
const char* SSID =“yourrouterssid”;
const char *密码=“yourrouterpassword”;
esp8266webserver服务器(80);
int switchpin = 0;
int switchstatecur;
int relaypin = 2;
int wificon() {
/检查我们是否有无线网络连接,如果我们不这样做,
连接!= wl_connected){
系列。println();
系列。println();
串口打印(“连接”);
系列。println(SSID);
WiFi。模式(wifi_sta);
WiFi。开始(SSID,密码!= wl_connected & xcnt<50){
延迟(500);
串口打印(“。”);
xcnt + +;
}
如果(!= WL_CONNECTED){
Serial.println("WiFiCon=0");
return 0; //never connected
} else {
Serial.println("WiFiCon=1");
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
return 1; //1 is initial connection
}
} else {
Serial.println("WiFiCon=2");
return 2; //2 is already connected
}
}
String htmlServe(int doorAction) {
String htmStr;
if (switchStateCur==1 && doorAction ==1){
digitalWrite(relayPin, 0);
delay(400);
digitalWrite(relayPin, 1);
} else if (switchStateCur==0 && doorAction ==0){
digitalWrite(relayPin, 0);
delay(400);
digitalWrite(relayPin, 1);
}
htmStr += "<html>\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0,\">\n";
htmStr += "<meta http-equiv=\"refresh\" content=\"5; URL=/sdoor\"> \n";
htmStr += "<body>\n<center>\n<h1>\n";
if (switchStateCur==1){
htmStr += "Your Door is Open";
} else {
htmStr += "The Door is Closed";
}
htmStr +="</h1>\n<br><a href=\"/";
if (switchStateCur==0){
htmStr += "odoor";
} else {
htmStr += "cdoor";
}
htmStr += "\">\n<button>\n";
if (switchStateCur==0){
htmStr += "Open Door";
} else {
htmStr += "Close Door";
}
htmStr += "</button></a>\n";
htmStr += "<br><br><br><a href=\"/sdoor\"><button>Recheck</button></a>";
htmStr += "</center>\n</body>\n</html>\n";
return htmStr;
}
void setup(){
pinMode(switchPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
Serial.begin(115200);
WiFiCon();
server.on("/odoor", [](){
server.send(200, "text/html", htmlServe(0));
});
server.on("/cdoor", [](){
server.send(200, "text/html", htmlServe(1));
});
server.on("/sdoor", [](){
server.send(200, "text/html", htmlserve(2));});
服务器。begin();
}
无效loop() {
switchstatecur = //读出数字接口的值(switchpin);
服务器。handleclient();