用ESP8266实现 手机控制车库门开关

一直想给车库的门加一个手机开关, 最近发发现了ESP8266 arduino 模块在淘宝上只要15人民币.

在ESP8266 2口连接了一个5v继电器控制模块, 实现了手机浏览器控制车库门.

 

用ESP8266实现 手机控制车库门开关_第1张图片

#include
#include
#include
#include

#include

const char* ssid     ="myrouter";
const char* password ="pwd";
const char* server_url = "172.16.1.40";  // server's address
 
ESP8266WebServer server(80);

const int led = 2;

void handleRoot2() {
  String message = "";
   for (uint8_t i=0; i     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(200, "text/plain", "hello from esp8266!" +message);
}

 

void handleJS() {
  server.send ( 200, "text/html", F("function formSubmit(id) {  window.location='/?id='+id;}"));
}
 
void handlecss() {
  server.send ( 200, "text/html", F("body {font:normal 100% Helvetica,Arial,sans-serif;background-color:#cccccc;font-family:Arial,Helvetica,Sans-Serif; Color: #000088;  text-align: center;}      .title_div{float:left;clear:left;padding-left: 10px;width: 20%;text-align:left;}      .row{float:left;clear:left;padding-top:5px;width:100%;}    @media screen and (max-device-width: 600px) {    .body_div{float:left; }            .input_div{float:left;clear:left;padding-left: 10px;}    }@media screen and (max-device-width: 2000px) and  (min-width: 601px) {  .body_div{margin: 0 auto; width: 50%;}            .input_div{float:left; padding-left: 10px;}    } "));
}

void handleRoot() {
  String message = "";
   for (uint8_t i=0; i     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";


if(server.arg(i) =="1"){
 
  digitalWrite(led, 0);
    delay(400);                       // waits 15ms for the servo to reach the position
  digitalWrite(led,1);
}


if(server.arg(i) =="2"){
   
    delay(400);                       // waits 15ms for the servo to reach the position  
}

    
  }
  Serial.println(message);
  server.send ( 200, "text/html", F("Opener

Opener! \u6d4b\u8bd5

"));
}

void handleNotFound(){
  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     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup(void){


  pinMode(led, OUTPUT);
  digitalWrite(led, 1);
  Serial.begin(115200);
   
 
  WiFi.begin(ssid, password);
  IPAddress ip(172, 16, 1, 60); // where xx is the desired IP Address
    IPAddress gateway(172, 16, 1, 254); // set gateway to match your network
    Serial.print(F("Setting static ip to : "));
    Serial.println(ip);
    IPAddress subnet(255, 255, 255, 0); // set subnet mask to match yournetwork
    WiFi.config(ip, gateway, subnet);
  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 ( "/style.css", handlecss );
  server.on ( "/t.js", handleJS );
   
  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();
}

你可能感兴趣的:(用ESP8266实现 手机控制车库门开关)