ESP8266 物联网小demo之 - 使用物联网的力量来点灯

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>






const char *ssid = "PDCN";
const char *passwd = "1234567890";
ESP8266WebServer esp8266_server(80);




void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(LED_BUILTIN, OUTPUT);
  

  //WiFi.softAP(ssid, passwd);
  WiFi.begin(ssid, passwd);
  Serial.print("Connect to ");
  Serial.print(ssid);
  Serial.println("....");

  int i = 0;
  while(WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(i++); Serial.print(' '); 
  }

    delay(1000);
    Serial.println(" "); 
    
    Serial.println("Connect OK...");
    Serial.println("IP address:{}");
    Serial.println(WiFi.localIP());


    esp8266_server.begin();
    esp8266_server.on("/", HTTP_GET, handleRoot);
    esp8266_server.on("/LED", HTTP_POST, handleLED);
    esp8266_server.onNotFound(handleNotFound);
    
    Serial.println("HTTP Server started ...");
    
    
  
  
}


void handleRoot() {
  esp8266_server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><button type=\"submit\" value=""> Switch the LED</button></form>");
}


void handleLED() {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  esp8266_server.sendHeader("Location", "/");
  esp8266_server.send(302);
}

void handleNotFound() {
  esp8266_server.send(404, "text/plain", "404 The Resource was not found on ESP8266 Web Server~~~");
}

void loop(void) {
  esp8266_server.handleClient();
  // put your main code here, to run repeatedly:

}
上一篇:ESP8266+OLED屏实现天气预报+温度显示+NTP时间同步(三)


下一篇:智能WIFI灯