搜索
查看: 5769|回复: 1

esp8266(nodemcu) +DHT11温湿传感器

[复制链接]

714

主题

354

回帖

2669

积分

管理员

本论坛第一帅

积分
2669

热心会员推广达人宣传达人突出贡献优秀版主荣誉管理论坛元老

QQ
发表于 2019-7-12 10:25:08 | 显示全部楼层 |阅读模式 来自 甘肃
DHT11+WEB版我们先看看效果

将DHT11的数据通过web服务器反馈过来。
开发思路
我们首先要安装两个库DHT和Adafruit Unified Sensor
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include "DHT.h"

  4. // Uncomment one of the lines below for whatever DHT sensor type you're using!
  5. #define DHTTYPE DHT11   // DHT 11
  6. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  7. //#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

  8. /*Put your SSID & Password*/
  9. const char* ssid = "kali";  // Enter SSID here
  10. const char* password = "12345678900";  //Enter Password here

  11. ESP8266WebServer server(80);

  12. // DHT Sensor
  13. uint8_t DHTPin = D4;
  14.                
  15. // Initialize DHT sensor.
  16. DHT dht(DHTPin, DHTTYPE);               

  17. float Temperature;
  18. float Humidity;

  19. void setup() {
  20.   Serial.begin(115200);
  21.   delay(100);
  22.   
  23.   pinMode(DHTPin, INPUT);

  24.   dht.begin();              

  25.   Serial.println("Connecting to ");
  26.   Serial.println(ssid);

  27.   //connect to your local wi-fi network
  28.   WiFi.begin(ssid, password);

  29.   //check wi-fi is connected to wi-fi network
  30.   while (WiFi.status() != WL_CONNECTED) {
  31.   delay(1000);
  32.   Serial.print(".");
  33.   }
  34.   Serial.println("");
  35.   Serial.println("WiFi connected..!");
  36.   Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

  37.   server.on("/", handle_OnConnect);
  38.   server.onNotFound(handle_NotFound);

  39.   server.begin();
  40.   Serial.println("HTTP server started");

  41. }
  42. void loop() {
  43.   
  44.   server.handleClient();
  45.   
  46. }

  47. void handle_OnConnect() {

  48. Temperature = dht.readTemperature(); // Gets the values of the temperature
  49.   Humidity = dht.readHumidity(); // Gets the values of the humidity
  50.   server.send(200, "text/html", SendHTML(Temperature,Humidity));
  51. }

  52. void handle_NotFound(){
  53.   server.send(404, "text/plain", "Not found");
  54. }

  55. String SendHTML(float Temperaturestat,float Humiditystat){
  56.   String ptr = "<!DOCTYPE html> <html>\n";
  57.   ptr +="<head> <meta name="viewport" content="width=device-width,  initial-scale=1.0, user-scalable=no">\n";
  58.   ptr +="<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600" rel="stylesheet">\n";
  59.   ptr +="<meta charset="UTF-8">\n";
  60.   ptr +="<title>Priess的小窝—智能温湿计</title>\n";
  61.   ptr +="<style>html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n";
  62.   ptr +="body{margin-top: 50px;}\n";
  63.   ptr +="h1 {margin: 50px auto 30px;}\n";
  64.   ptr +=" .wd {margin: 50px auto 30px;width: auto;color: #f39c12}\n";
  65.   ptr +=" .wd1 {margin: 50px auto 30px;width: auto;color: #3498db}\n";
  66.   ptr +=".side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n";
  67.   ptr +=".humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n";
  68.   ptr +=".humidity-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n";
  69.   ptr +=".humidity{font-weight: 300;font-size: 60px;color: #3498db;}\n";
  70.   ptr +=".temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n";
  71.   ptr +=".temperature-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n";
  72.   ptr +=".temperature{font-weight: 300;font-size: 60px;color: #f39c12;}\n";
  73.   ptr +=".superscript{font-size: 17px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n";
  74.   ptr +=".data{padding: 10px;}\n";
  75.   ptr +="</style>\n";
  76.   ptr +="</head>\n";
  77.   ptr +="<body>\n";
  78.   
  79.    ptr +="<div id="webpage">\n";
  80.    
  81.    ptr +="<h1>室内温室检测系统</h1>\n";
  82.    ptr +="<div class="data">\n";
  83.    ptr +="<div class="side-by-side temperature-icon">\n";
  84.    ptr +="<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"\n";
  85.    ptr +="width="9.915px" height="22px" viewBox="0 0 9.915 22" enable-background="new 0 0 9.915 22" xml:space="preserve">\n";
  86.    ptr +="<path fill="#FFFFFF" d="M3.498,0.53c0.377-0.331,0.877-0.501,1.374-0.527C5.697-0.04,6.522,0.421,6.924,1.142\n";
  87.    ptr +="c0.237,0.399,0.315,0.871,0.311,1.33C7.229,5.856,7.245,9.24,7.227,12.625c1.019,0.539,1.855,1.424,2.301,2.491\n";
  88.    ptr +="c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038-2.276,2.594c-1.056,0.583-2.331,0.743-3.501,0.463\n";
  89.    ptr +="c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26-0.115,17.104,0.1,16.022c0.296-1.443,1.274-2.717,2.58-3.394\n";
  90.    ptr +="c0.013-3.44,0-6.881,0.007-10.322C2.674,1.634,2.974,0.955,3.498,0.53z"/>\n";
  91.    ptr +="</svg>\n";
  92.    ptr +="</div>\n";
  93.    ptr +="<div class="side-by-side temperature-text">室内温度:</div>\n";
  94.    ptr +="<div class="side-by-side temperature">";
  95.    ptr +=(int)Temperaturestat;
  96.    ptr +="<span class="superscript">°C</span></div>\n";
  97.    ptr +="</div>\n";
  98.    ptr +="<div class="data">\n";
  99.    ptr +="<div class="side-by-side humidity-icon">\n";
  100.    ptr +="<svg version="1.1" id="Layer_2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"\n"; width="12px" height="17.955px" viewBox="0 0 13 17.955" enable-background="new 0 0 13 17.955" xml:space="preserve">\n";
  101.    ptr +="<path fill="#FFFFFF" d="M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n";
  102.    ptr +="c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217"></path>\n";
  103.    ptr +="</svg>\n";
  104.    ptr +="</div>\n";
  105.    ptr +="<div class="side-by-side humidity-text">室内湿度:</div>\n";
  106.    ptr +="<div class="side-by-side humidity">";
  107.    ptr +=(int)Humiditystat;
  108.    ptr +="<span class="superscript">%</span></div>\n";
  109.    ptr +="</div>\n";
  110.    //定义温度变量,并且赋值,用于逻辑判断。
  111.     int wd = Temperaturestat ;
  112.      
  113.    if (wd>=30){
  114.     ptr +="<div class="wd">Hi~ 宝贝今天天气贼热,注入防暑哦!</div>\n";
  115.    }
  116.    if (29>=wd&&wd>20){
  117.     ptr +="<div class="wd1">Hi~ 宝贝今天天气不错吆,尽情的玩耍吧!</div>\n";
  118.    }
  119.    if (wd<10){
  120.     ptr +="<div class="side-by-side humidity-text">Hi~ 宝贝今天天气比较冷,多穿点衣服,小心着凉!</div>\n";
  121.    }
  122.   ptr +="</div>\n";
  123.   ptr +="</body>\n";
  124.   ptr +="</html>\n";
  125.   return ptr;
复制代码



有志者,事竟成,破釜沉舟,百二秦关终属楚. 苦心人,天不负, 卧薪尝胆 ,三千越甲可吞吴
回复

使用道具 举报

0

主题

7

回帖

8

积分

新手开车

积分
8
发表于 2022-7-3 10:36:59 | 显示全部楼层 来自 陕西咸阳
大佬,这个帖子在微信公众号或者别的地方有吗?这上面的图片看不到了
回复 支持 反对

使用道具 举报

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

本版积分规则

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