找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 9159|回复: 11
收起左侧

手机通过wifi內网控制ESP12E輸出LED及DHT22温度湿度在手机显示

  [复制链接]
ID:330820 发表于 2018-12-31 17:33 | 显示全部楼层 |阅读模式
這個手機的wifi 小玩意的英文從這個randomnerdtutorials点com  網站下載的裡面有詳細資

我加入了

1. 一個 DHT22
2. 加多一個 LED
3. 更改固定 IP 地圵
4. 更攺按鍵顏色

這是用手機通過無線網絡內網控制 ESP12E 輸出 3 LED 1 DHT22 溫度濕度在手機顯示

LED 輸出可更攺為繼電器輸出控制家電開關

如有須要可加入其它功能曾加或減少

下載用 Arduino 便可以。要 Arduino IDE 中安裝 ESP12 板。

ESP12E 輸出及DHT22

ESP12E 輸出及DHT22

ESP12E 輸出

ESP12E 輸出

手機的wifi

手機的wifi

如何 Arduino IDE 中安裝ESP8266



/*************************************************************
  从这个网更改的,詳细請看这网
  Completeproject details at http://randomnerdtutorials点com  
****************************************************************/

// Load Wi-Fi library
#include <ESP8266WiFi.h>
unsigned long previousMillis = 0;        
unsigned long currentMillis = 0;
const long interval = 4100;            
float temp;
float humi;
float temp1;
float humi1;
#include <Wire.h>
#include <DHT.h>
#define DHTPin 2  
#define DHTTYPE DHT
DHT dht(DHTPin, DHT22);

// Replace with your network credentials
const char* ssid     = "xxxxxxxx";                   //????更攺自巳的wifi名稱
const char* password ="xxxxxxxx";                  //????更攺自巳的密码

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the currentoutput state
String output0State = "OFF";
String output1State = "OFF";
String output2State = "OFF";
// Assign output variables to GPIO pins

const int output0 = 14;
const int output1 = 4;
const int output3 = 5;

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 167);//????更改自已IP,这里是固定IP地址,每次开机都是这个地圵.
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);   //optional 可不用更改
IPAddress secondaryDNS(8, 8, 4, 4);//optional  可不用更改
//----------------------------------------------------------------------------------
void setup() {


  //Initialize the output variables as outputs
pinMode(output0, OUTPUT);
pinMode(output1, OUTPUT);
pinMode(output3, OUTPUT);
  //Set outputs to LOW
digitalWrite(output0, LOW);
digitalWrite(output1, LOW);
digitalWrite(output3, LOW);

//////////Serial.begin(9600);   //要不要都可以,试机时加入
//--------------------------------------------------------------------------------  
  dht.begin();
// Configures static IP address
  if(!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  /////////// Serial.println("STA Failed to configure");
}  
  //Connect to Wi-Fi network with SSID and password
///////////Serial.print("Connecting to ");
///////////Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   ////////////Serial.print(".");
  }
  //Print local IP address and start web server
/////////////Serial.println("");
////////////Serial.println("WiFi connected.");
////////////Serial.println("IP address: ");
////////////Serial.println(WiFi.localIP());
server.begin();
}
//-------------------------------------------------------------------------------
void loop(){
            //4sec read dhl22 one time
       currentMillis = millis();
       if(currentMillis - previousMillis >= interval)
         {                                             
        previousMillis = currentMillis;   
        temp1 = dht.readTemperature();
        humi1 = dht.readHumidity();                                    
                      if (!(isnan(humi1) ||isnan(temp1)))
                       {
                        temp=temp1;
                        humi=humi1;
                      }
          }   
//-----------------------------------------------------------------------------------------               
   WiFiClient client = server.available();                 // Listen for incomingclients         
  if(client)
{                                                        // If a new client connects,
   //////////////Serial.println("New ClientX1.");          // print a message out in the serialport
   String currentLine ="";                                // make aString to hold incoming data from the client
    while (client.connected())
   {                                                    // loop while the client's connected
                                                         // Serial.println("New ClientX2.");
     if (client.available())
      {                                                  // if there's bytes to read from the client,
       char c = client.read();                           // read a byte, then
     ///////////////Serial.write(c);                     // print it out the serialmonitor
       header += c;
       if (c == '\n')
       {        // if the byte is anewline character
                // if the current line isblank, you got two newline characters in a row.
                // that's the end of the clientHTTP request, so send a response:
         if (currentLine.length() == 0)
         {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
           // and a content-type so the client knows what's coming, then a blankline:
//-----------------------------------------------------------------------------------------------           
           client.println("HTTP/1.1 200 OK");
           client.println("Content-type:text/html");
           client.println("Connection: close");
           client.println("Refresh: 10");
           client.println();
//---------------------------------------------------------------------------------------           
              // turns the GPIOs on and off
           if (header.indexOf("GET/0/on") >= 0)     {
           //////////// Serial.println("GPIO 0 on");
              output0State ="ON";
              digitalWrite(output0, HIGH);        
              }
           else if (header.indexOf("GET /0/off") >= 0){
           ////////////Serial.println("GPIO 0 off");
              output0State ="OFF";
              digitalWrite(output0, LOW);
           }
           else if (header.indexOf("GET /1/on") >= 0) {
           //////////// Serial.println("GPIO 1 on");
              output1State ="ON";
              digitalWrite(output1, HIGH);
           }
           else if (header.indexOf("GET /1/off") >= 0) {
          //////////// Serial.println("GPIO 1 off");
              output1State ="OFF";
            digitalWrite(output1, LOW);
           }
           else if (header.indexOf("GET /2/on") >= 0) {
             ////////////Serial.println("GPIO 3 on");
              output2State ="ON";
              digitalWrite(output3, HIGH);
           }
           else if (header.indexOf("GET /2/off") >= 0) {
          ////////////// Serial.println("GPIO 3 off");
              output2State ="OFF";
            digitalWrite(output3, LOW);
           }

//------------------------------------------------------------------------------           
              // Display the HTML web page
           client.println("<!DOCTYPE html><html>");
           client.println("<head><meta name=\"viewport\"content=\"width=device-width, initial-scale=1\">");
           client.println("<link rel=\"icon\"href=\"data:,\">");
              // CSS to style the on/offbuttons
              // Feel free to change thebackground-color and font-size attributes to fit your preferences
           client.println("<style>html { font-family: Helvetica;display: inline-block; margin: 0px auto; text-align: center;}");
           client.println(".button { background-color: #195B6A; border-radius:8px; color: white; padding:16px 40px;");
           client.println("text-decoration: none; font-size: 14px; margin:2px; cursor: pointer;}");
           client.println(".button2 {background-color:#c3352b;}</style></head>");

              // Web Page Heading
           client.println("<body><h4>CONTROL 1 DHT 3 LED</h4>");

           client.println("Temp: ");
           client.println(temp);
           client.println("C");
           client.println("<br>");
           client.println("Humi: ");
           client.println(humi);
           client.println("%");
//--------------------------------------------------------------------------------------------------------------------         
           // Display current state, and ON/OFF buttons                                                                        
          // If the output0State is off, it displays the ON button      
           if (output0State=="OFF")
           {
             client.println("<p><ahref=\"/0/on\"><button class=\"button\">LED0_OFF</button></a></p>");
           }
           else
           {
             client.println("<p><ahref=\"/0/off\"><button class=\"buttonbutton2\">LED0_ON</button></a></p>");
           }     
//-----------------------------------------------------------------------------------------------------------------------                     
            // Display current state, and ON/OFF buttons  
            // If the output1State is off, it displays the ON button                     
         if (output1State=="OFF")
            {
           client.println("<p><ahref=\"/1/on\"><buttonclass=\"button\">LED1_OFF</button></a></p>");
           }
           else
              {
           client.println("<p><a href=\"/1/off\"><buttonclass=\"buttonbutton2\">LED1_ON</button></a></p>");
              }
//---------------------------------------------------------------------------------------------------------------------              
           // Display current state, and ON/OFF buttons
           // If the output1State is off, it displays the ON button      
            if (output2State=="OFF")
            {
               client.println("<p><ahref=\"/2/on\"><buttonclass=\"button\">LED2_OFF</button></a></p>");
               }
           else
            {
           client.println("<p><ahref=\"/2/off\"><button class=\"buttonbutton2\">LED2_ON</button></a></p>");
              }
//------------------------------------------------------------------------------------------------------------              
           client.println("</body></html>");           
            // The HTTP response ends with another blank line
           client.println();
           // Break out of the while loop
           break;
         }
         else
         {  // if you got a newline, thenclear currentLine
           currentLine = "";
         }
       }
       else if (c != '\r')
       {   // if you got anything elsebut a carriage return character,
         currentLine += c;     // add it tothe end of the currentLine
       }
     }
    }
     // Clear the header variable
   header = "";
     // Close the connection
   client.stop();
   //////////////Serial.println("Client disconnected.");
}  
}

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:63317 发表于 2019-1-25 20:59 | 显示全部楼层
谢谢分享资料
回复

使用道具 举报

ID:359281 发表于 2019-4-3 14:20 | 显示全部楼层
谢谢分享资料!!!!!!!!!!!!!
回复

使用道具 举报

ID:553016 发表于 2019-6-2 15:05 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:412814 发表于 2019-6-4 19:05 | 显示全部楼层
请问我想学习这方面的,该怎样入手呢
回复

使用道具 举报

ID:416108 发表于 2019-6-16 10:49 | 显示全部楼层
不错,感谢分享
回复

使用道具 举报

ID:282095 发表于 2019-7-17 09:14 | 显示全部楼层
楼主真厉害
回复

使用道具 举报

ID:236710 发表于 2019-10-8 19:56 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:93341 发表于 2020-2-8 12:14 | 显示全部楼层
小白物联网入门教程,2步把传统节能灯改为智能物联网节能灯    [复制链接]   
回复

使用道具 举报

ID:184813 发表于 2020-2-15 23:08 | 显示全部楼层
谢谢分享资料
回复

使用道具 举报

ID:458472 发表于 2021-5-20 16:13 | 显示全部楼层
app哪里下载的
回复

使用道具 举报

ID:137736 发表于 2021-8-2 18:20 | 显示全部楼层
工程文件做成个压缩包吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表