一乐电子

一乐电子百科

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

快捷登录

手机号码,快捷登录

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

[其他综合] 用esp8266和dht11利用双精度计算上传温湿度到酱菜创客物联网

[复制链接]
发表于 2016-12-24 11:07 | 显示全部楼层 |阅读模式
本帖最后由 g54188 于 2016-12-24 15:31 编辑

这个程序稍微修改下,可以改成8路温度控制;这个程序比酱菜网站示例精确,也不会调线
首先去酱菜创客(jcck.online)注册申请apikey和添加设备
硬件:
        1、ESP8266           一块
        2、DHT11      一个
        3、导线若干
      当然上面三个是最主要的,除了这两个大家还需要准备3.3V电源,因为8266和DHT11的工作电压是3.3V,电源问题应该对大家来说不是难事吧

烧写模式接线方法:(用USB转TTL串口连接模块与PC)
esp8266-01         u转串
VCC-------------3.3
GND------------GND
GPIO0----------GND
CH_PD---------3.3
RX---------------TX
TX---------------Rx
运行模式ESP8266接线:
VCC---------------3.3
CH_PD-----------3.3
GND-------------GND
GPIO2----------DHT11 data脚
软件要求:arduino1.6.5版本以上,可以编译esp8266的就可以了.esp-01选择烧写设备如图http://image.geek-workshop.com/forum/201612/24/101853oc1c0lozbbszevlk.jpg

http://image.geek-workshop.com/forum/201612/24/101916jx2lb2b24czvmwzw.jpg

esp-12模块烧写选择如图:
http://image.geek-workshop.com/forum/201612/24/102031wj4zoqpadvpoambs.jpg


这样基本就可以了

上传到酱菜创客的温湿度如下:

http://image.geek-workshop.com/forum/201612/24/102451pb9nqbntqj4b3nbt.jpg
http://image.geek-workshop.com/forum/201612/24/102536qz2troge3vehsq1s.jpg

http://image.geek-workshop.com/forum/201612/24/102516stio8i35a76i3mmt.jpg



代码来了
///---------------------------------------------
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}    //摄氏温度度转化为华氏温度

double Kelvin(double celsius)
{
        return celsius + 273.15;
}     //摄氏温度转化为开氏温度

// 露点(点在此温度时,空气饱和并产生露珠)
// 参考: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// 快速计算露点,速度是5倍dewPoint()
// 参考: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

#include <dht11.h>
dht11 DHT11;
#include <ESP8266WiFi.h>
WiFiClient client;
const char *ssid     = "*****";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "****";//你要连接的wifi密码
const char *host = "jcck.online";
const int httpPort =8266;
String line ="";
int m=0;

#define DHT11PIN 2   //温湿度输入

void setup()
{
  Serial.begin(115200);
  Serial.println();
pinMode(DHT11PIN,INPUT);
Serial.print("Connecting to ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
     //smartConfig();
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  while (!client.connect(host, httpPort)) {
    Serial.println("connection faiLED");
    //return;
  }
  Serial.print("connecting to ");
  Serial.println(host);

  delay(150);
  client.write("mode=bind&apikey=xxx&data={ck001000bind}\r\n"); //修改成自己的apikey
  delay(100);


  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);   
  
}

void loop()
{
  
while(client.available()){   String line = client.readStringUntil('\r');
   Serial.println(line);
   Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case DHTLIB_OK:
                Serial.println("OK");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.println("Checksum error");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.println("Time out error");
                break;
    default:
                Serial.println("Unknown error");
                break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

  /*Serial.print("Temperature (oF): ");
  Serial.println(Fahrenheit(DHT11.temperature), 2);

  Serial.print("Temperature (K): ");
  Serial.println(Kelvin(DHT11.temperature), 2);

  Serial.print("Dew Point (oC): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

  Serial.print("Dew PointFast (oC): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));/*/


String str1="mode=up&apikey=f5a4a83d00b08870&data={ck001002";
  str1+=DHT11.humidity;      //湿度
  str1+=".";
  str1+=DHT11.temperature;   //温度
  str1+="}\r\n";
  client.print(str1);
  Serial.print(str1);
  delay(60000);
//1分钟上传一次;
String line ="";
  m++;
  if(m%40==0)
  {
    client.write("mode=up&apikey=xxx &data={ck002001life}\r\n");//发送心跳消息,xxx修改成你自己的apikey
    m=0;
   }
  delay(2000);
}






补充内容 (2017-12-10 09:16):
http://www.jcckiot.com/现在是这个地址
发表于 2016-12-24 13:41 | 显示全部楼层
不错,直接对8266进行编程,连单片机都省了,而且省去了对DNS解析的代码,arduino果然很简单,可惜我不会。
发表于 2016-12-24 14:49 | 显示全部楼层
请教楼主,能不能用esp-12模块做这个之余,其他的io口做开关呢?
 楼主| 发表于 2016-12-24 15:22 | 显示全部楼层
duanyz 发表于 2016-12-24 14:49
请教楼主,能不能用esp-12模块做这个之余,其他的io口做开关呢?

当然可以,这个简单改下可以做风扇的自动温度控制,用IF判断温度值
 楼主| 发表于 2016-12-24 15:24 | 显示全部楼层
40560335 发表于 2016-12-24 13:41
不错,直接对8266进行编程,连单片机都省了,而且省去了对DNS解析的代码,arduino果然很简单,可惜我不会。 ...

很简单的(我也是小白一个),照到程序改,ARDUINO好多还是用C可以看懂的,一般都是调用库函数
发表于 2016-12-24 16:43 | 显示全部楼层
g54188 发表于 2016-12-24 15:24
很简单的(我也是小白一个),照到程序改,ARDUINO好多还是用C可以看懂的,一般都是调用库函数

是的,方便的是有强大又好用的库,貌似还能自建库,我看已经有牛人让ARDUINO支持STM8/32了
发表于 2016-12-24 18:50 | 显示全部楼层
只会一点C,arduino不会咋玩啊?
发表于 2016-12-24 20:33 | 显示全部楼层
lising 发表于 2016-12-24 18:50
只会一点C,arduino不会咋玩啊?

刷nodemcu固件,用LUA语言写程序,改改别人的例程直接用。

参考网址
http://www.geek-workshop.com/thread-12689-1-1.html

我抄的
http://www.yeelink.net/devices/352994


发表于 2016-12-25 02:01 | 显示全部楼层
lising 发表于 2016-12-24 18:50
只会一点C,arduino不会咋玩啊?

使用 arduino 的环境编译,实际不需要 arduino 硬件的
发表于 2016-12-25 02:02 | 显示全部楼层
40560335 发表于 2016-12-24 13:41
不错,直接对8266进行编程,连单片机都省了,而且省去了对DNS解析的代码,arduino果然很简单,可惜我不会。 ...

arduino 的库是挺齐全


其实,使用 arduino 的环境编译,实际不需要 arduino 硬件的

本版积分规则

QQ|一淘宝店|手机版|商店|电子DIY套件|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2024-4-19 20:27 , Processed in 0.079801 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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