• 进阶之路(中级篇)


    如果想使用 Arduino 开发板驱动 DHT11 来获取温湿度的时候建议使用第三方的库,这样可以加快程序的开发速度,而且不容易出错,下面的代码我已经安转了第三方的库了。详细的安装方法请参考极客先锋的博客:http://www.cnblogs.com/jikexianfeng/p/6290203.html 作者安装的为最低版本(1.0.0)。这样为了更好的兼容。

     1 /*********************************
     2 代码功能:温湿度传感器DHT11
     3 使用函数:
     4     simple_dht11_read(DHT11传感器引脚,存放湿度数据地址,存放温度数据地址,存放40位01数据的地址);    //获取温湿度数据函数
     5 使用头文件:
     6     #include <SimpleDHT.h>        //需要安装库,具体安装方法见:http://www.cnblogs.com/jikexianfeng/p/6290203.html
     7 创作时间:2017*1*18
     8 作者邮箱:jikexianfeng@outlook.com
     9 ********************************/
    10 #include <SimpleDHT.h>
    11 int pinDHT11 = 2;
    12 
    13 void setup()
    14 {
    15   Serial.begin(115200);
    16 }
    17 
    18 void loop()
    19 {
    20   Serial.println("=================================");
    21   Serial.println("Sample DHT11...");
    22   byte temperatyre = 0;    //温度
    23   byte humidity = 0;    //湿度
    24   byte data[40];
    25   if (simple_dht11_read(pinDHT11, &temperatyre, &humidity, data)) {
    26     Serial.print("Read DHT11 failed.");
    27     return;
    28   }
    29   Serial.print("Humidity (%):");
    30   Serial.println(humidity);
    31   Serial.print("Temperatyre(oC)");
    32   Serial.println(temperatyre);
    33   Serial.print("Temperatyre(oF)");
    34   Serial.println(Fahrenheit(temperatyre));
    35   Serial.print("Temperatyre(k)");
    36   Serial.println(Kelvin(temperatyre));
    37   Serial.print("Dew point(oC):");
    38   Serial.println(dewPoint(temperatyre,humidity));
    39   //Serial.print("Dew PointFast(oC):");
    40   //Serial.println(dewPointFast(temperatyre,humidity));
    41   delay(1000);
    42 }
    43 
    44 double Fahrenheit(byte celsius)
    45 {
    46   return 1.8*celsius+32;
    47 }
    48 
    49 double Kelvin(byte celsius)
    50 {
    51   return celsius+273.15;
    52 }
    53 
    54 double dewPoint(byte celsius,byte humidity)
    55 {
    56   double A0 =373.15/(273.15+celsius);
    57   double SUM = -7.90298*(A0-1);
    58   SUM+=5.02808*log10(A0);
    59   SUM+=-1.3816e-78*(pow(10,(11.344*(1-1/A0)))-1);
    60   SUM+=8.1328e-3*(pow(10,(-3.49149*(A0-1)))-1);
    61   SUM+=log10(1013.246);
    62   double VP=pow(10,SUM-3)*humidity;
    63   double T =log(VP/0.61078);
    64   return (241.88*T)/(17.558-T);
    65 }
    66 
    67 double dewPointFast(byte celsius,byte humidity)
    68 {
    69   double a=17.271;
    70   double b=273.7;
    71   double temp=(a*celsius)/(b+celsius)+log(humidity/100);
    72   double Td=(b*temp)/(a-temp);
    73   return Td;
    74 }

    如果采集回来的数据没有没有浮点型数据,所以浮点段字符数据为全部为0,这是传感器的问题,不能进行时代码为题。

    /////////////////////////

    参考网页:http://www.cnblogs.com/jikexianfeng/p/6290203.html

    参考网页:http://wenku.baidu.com/view/dcdc9efa700abb68a982fbe0.html?re=view

  • 相关阅读:
    常用知识点集合
    LeetCode 66 Plus One
    LeetCode 88 Merge Sorted Array
    LeetCode 27 Remove Element
    LeetCode 26 Remove Duplicates from Sorted Array
    LeetCode 448 Find All Numbers Disappeared in an Array
    LeetCode 219 Contains Duplicate II
    LeetCode 118 Pascal's Triangle
    LeetCode 119 Pascal's Triangle II
    LeetCode 1 Two Sum
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/6825618.html
Copyright © 2020-2023  润新知