ESP8266基於WebServer實時獲取溫濕度


功能描述:通過在瀏覽器中訪問ESP8266提供的服務來獲取其實時的溫濕度信息;

操作步驟:

第一步、將下面的代碼燒錄到Esp中;

第二步、打開串口調試工具查看該ESP8266的IP地址

第三步、訪問 IP地址/index.html 這個網址來獲取實時的溫濕度

#include <ESP8266WiFi.h>

const char* ssid = "Wi-Fi名稱";
const char* password = "Wi-Fi密碼";
int temp = 0;//溫度
int humi = 0;//濕度
unsigned int loopCnt;
int chr[40] = {0};
unsigned long timer;
#define pin D4

WiFiServer server(80);

/**
 * 獲取溫濕度
 */
void get_humiture() {
  bgn:
  delay(2000);
  //設置2號接口模式為:輸出
  //輸出低電平20ms(>18ms)
  //輸出高電平40μs
  pinMode(pin,OUTPUT);
  digitalWrite(pin,LOW);
  delay(20);
  digitalWrite(pin,HIGH);
  delayMicroseconds(40);
  digitalWrite(pin,LOW);
  //設置2號接口模式:輸入
  pinMode(pin,INPUT);
  //高電平響應信號 
  loopCnt=10000;
  while(digitalRead(pin) != HIGH)
  {
    if(loopCnt-- == 0)
    {
      //如果長時間不返回高電平,輸出個提示,重頭開始。
//      Serial.println("HIGH");
      goto bgn;
    }
  }
  //低電平響應信號
  loopCnt=30000;
  while(digitalRead(pin) != LOW)
  {
    if(loopCnt-- == 0)
    {
      //如果長時間不返回低電平,輸出個提示,重頭開始。
//      Serial.println("LOW");
      goto bgn;
    }
  }
    //開始讀取bit1-40的數值  
    for(int i=0;i<40;i++)
  {
    while(digitalRead(pin) == LOW)
    {}
    //當出現高電平時,記下時間“timer”
    timer = micros();
    while(digitalRead(pin) == HIGH)
    {}
    //當出現低電平,記下時間,再減去剛才儲存的timer
    //得出的值若大於50μs,則為‘1’,否則為‘0’
    //並儲存到數組里去
    if (micros() - timer >50)
    {
      chr[i]=1;
    }else{
      chr[i]=0;
    }
  }
   
  //濕度,8位的bit,轉換為數值
  humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
     
  //溫度,8位的bit,轉換為數值
  temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
}

void setup() {
  pinMode(D4, OUTPUT);
  Serial.begin(115200);
  delay(10);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client connected");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  
  client.flush();
  get_humiture();

  
  // Prepare the response
  String page = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<head>";
  page += "<meta charset='utf-8'>";
//  page += "\r\n<link href='https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet'>";
  page += "\r\n<script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js'></script>";
  page += "\r\n</head>\r\n<body style='padding:10px'>";
  page += "\r\n<h1>實時溫濕度:</h1><br>";

  // 日期
  page += "\r\n<div id='date'></div><br>";

  String humistring = "溫度: ";
  humistring += temp;
  humistring += " 濕度:";
  humistring += humi;

  // 溫濕度
  page += "\r\n<div id='content'>";
  page += humistring;
  page += "</div><br>";
  
  page += "\r\n<script>\r\n";
  page += "var date = new Date(); $('#date').text(date);";
  page += "\r\n";
  page += "setInterval(function(){$.post('/gethumi', {}, function(res) {var date = new Date(); $('#date').text(date); $('#content').html(res);})}, 3000);";
  page += "\r\n";
  page += "console.log('" + humistring + "')";
  page += "\r\n</script>";
  page += "\r\n</body>\r\n</html>\n";

  
  // Match the request
  if (req.indexOf("/index.html") != -1) {
    client.print(page);
    return;
  } else if (req.indexOf("/gethumi") != -1) {
    String humires = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<head>";
    humires += "<meta charset='utf-8'></head><body>";
    humires += humistring;
    humires += "</body></html>";
    client.print(humires);
  } else {
    Serial.println("invalid request");
    client.stop();
    return;
  }
  delay(1);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM