8266网页控制LED/舵机/ 除了网页控制还有app控制. APP 支持编译 win/linux/mac/ios/安卓

本地连接只需要局域网IP即可.

外网控制要IPV6或者借助IOT或者你家里有公网IP.

 

我自己是IPV6远程控制.

 

APP是delphi编写,一套代码可以编译win/linux/mac/ios/安卓

 

 

8266硬件代码

核心代码,之前我在另外一个博文讲了8266通用两种通过网页控制方案,

一种是websocket.另外一种就是通过post/get.这里就是通过get获取网页传递进来的参数,然后进行处理.

 

  void loop() {
  WiFiClient client = server.available();   
    if (client) {                             
      Serial.println("New Client.");          
      String currentLine = "";                
      while (client.connected()) {            
        if (client.available()) {             
          char c = client.read();             
          Serial.write(c);                    
          header += c;
          if (c == '\n') {
            if (currentLine.length () == 0) {
              client.println("HTTP/1.1 200 OK");
              client.println("Content-type:text/html");
              client.println("Connection: close");
              client.println();
  
              //URL是 /5/on 则 5打开
              //URL是 /5/off 则5关闭 
              //其他几个类似
              if (header.indexOf("GET /5/on") >= 0) {
                Serial.println("GPIO 5 on");
                output5State = "on";
                digitalWrite(output5, HIGH); //打开
              } else if (header.indexOf("GET /5/off") >= 0) {
                Serial.println("GPIO 5 off");
                output5State = "off";
                digitalWrite(output5, LOW);  //关闭
              }
              
              if (header.indexOf("GET /4/on") >= 0) {
                Serial.println("GPIO 4 on");
                output4State = "on";
                digitalWrite(output4, HIGH);
              } else if (header.indexOf("GET /4/off") >= 0) {
                Serial.println("GPIO 4 off");
                output4State = "off";
                digitalWrite(output4, LOW);
              }
                           
              if (header.indexOf("GET /3/on") >= 0) {
                Serial.println("GPIO 3 on");
                output3State = "on";
                digitalWrite(output3, HIGH);
              } else if (header.indexOf("GET /3/off") >= 0) {
                Serial.println("GPIO 3 off");
                output3State = "off";
                digitalWrite(output3, LOW);
              }


              //舵机控制
              
              if (header.indexOf("GET /2/on") >= 0) {
                Serial.println("GPIO 2 on");
                output2State = "on";
                myservo.write(pos1);  //舵机转到指定角度(pos1) 
              } else if (header.indexOf("GET /2/off") >= 0) {
                Serial.println("GPIO 2 off");
                output2State = "off";
                myservo.write(pos2);  //舵机转到指定角度(pos2) 
              }        
              

              //输出网页内容
              client.println("");
              client.println("");
              client.println("");
              client.println("");
              client.println("html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
              client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
              client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
              client.println(".button2 {background-color: #77878A;}"); client.println("Y.A.K.E 8266网页开关测试"); client.println("
8266接线 D2 接舵机, D3-D5接LED或其他开关设备
"); client.println("LED 5 - 状态 " + output5State + ""); if (output5State == "off") { client.println("开"); } else { client.println("关"); } client.println("LED 4 - 状态 " + output4State + ""); if (output4State == "off") { client.println("开"); } else { client.println("关"); } client.println("LED 3 - 状态 " + output3State + ""); if (output3State == "off") { client.println("开"); } else { client.println("关"); } client.println("舵机 - 状态 " + output2State + ""); if (output2State == "off") { client.println("开"); } else { client.println("关"); } client.println(""); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } } } header = ""; client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }

 

上位机代码

 

上位机程序实际也是通过网页控制

核心代码也是获取网页源代码,通过判断某个开关状态

procedure TForm1.UpLedStatus(web_data:string) ;
begin




  //如果网页代码包含/5/on 就表示5处于关闭状态
  if (Pos('href="/5/on"',web_data)>0) then
  begin
      led5:=false;
  end
    else
    begin
      led5:=true;
    end;


   Switch1.IsChecked:=  led5 ; //灯5开关状态




  //如果网页代码包含/4/on 就表示4处于关闭状态
  if (Pos('href="/4/on"',web_data)>0) then
  begin
      led4:=false;
  end
    else
    begin
      led4:=true;
    end;


   Switch2.IsChecked:=  led4 ; //灯4开关状态



  //如果网页代码包含/3/on 就表示3处于关闭状态
  if (Pos('href="/3/on"',web_data)>0) then
  begin
      led3:=false;
  end
    else
    begin
      led3:=true;
    end;


   Switch3.IsChecked:=  led3 ; //灯3开关状态


  //如果网页代码包含/2/on 就表示舵机处于关闭状态
  if (Pos('href="/2/on"',web_data)>0) then
  begin
      led2:=false;
  end
    else
    begin
      led2:=true;
    end;


   Switch4.IsChecked:=  led2 ; //舵机开关状态


end;

 

 

 

使用

 

网页

 

安卓

windows

 

 

 

 

注意

IPV6

如果使用IPV6

那么IP那里,是要用方括号 把IPV6地址包起来.

包括你浏览器也是

http://[  IPV6地址   ] 

 

 

另外8266烧录默认是不支持IPV6,要手动打开开关

 

 

 

 

整套源代码

https://github.com/Y-A-K-E/8266_web_led_ipv6