极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 37087|回复: 9

【arduino for esp8266】ESP8266上传温湿度(DHT11)给云服务器

[复制链接]
发表于 2016-12-9 19:20:14 | 显示全部楼层 |阅读模式
本帖最后由 chenyuechi 于 2017-7-18 11:04 编辑

接下来,我就带着大家试着用DHT11检测温湿度并用ESP8266上传到云服务器
大家先根据这个帖子搭好开发环境----基于esp8266的智能家居控制系统-基础篇1介绍arduino ide for esp8266
这里我用的云服务器是酱菜创客(www.jcckiot.com)大家可以去注册申请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脚
代码:
  1. #include <ESP8266WiFi.h>
  2. WiFiClient client;
  3. const char *ssid     = "****";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
  4. const char *password = "*********";//你要连接的wifi密码
  5. const char *host = "www.jcckiot.com";//修改为手机的的tcpServer服务端的IP地址,即手机在路由器上的ip
  6. const int httpPort =8266;
  7. #define pin 2
  8. //void  keeponline();
  9. //void wificonnect();
  10. void wenshidu();
  11. void wenshidu1();
  12. int n=0;
  13. void setup() {
  14.   Serial.begin(115200);
  15.   delay(10);
  16.   //pinMode(relay1,INPUT);
  17.   // We start by connecting to a WiFi network
  18.    
  19.   Serial.println();
  20.   Serial.println();
  21.   Serial.print("Connecting to ");
  22.   //Serial.println(ssid);
  23.    
  24.   WiFi.begin(ssid, password);
  25.    
  26.   while (WiFi.status() != WL_CONNECTED) {
  27.     delay(500);
  28.      //smartConfig();
  29.     Serial.print(".");
  30.   }

  31.   Serial.println("");
  32.   Serial.println("WiFi connected");  
  33.   Serial.println("IP address: ");
  34.   Serial.println(WiFi.localIP());
  35.    
  36.   while (!client.connect(host, httpPort)) {
  37.     Serial.println("connection failed");
  38.     //return;
  39.   }
  40.   Serial.print("connecting to ");
  41.   Serial.println(host);
  42.   client.write("mode=bind&apikey=你自己的apikey&data={ck001000bind}\r\n"); //修改成自己的apikey
  43.   delay(100);
  44. }

  45. void loop() {
  46.   while(client.available()){
  47.     String line = client.readStringUntil('\n');
  48.      Serial.println(line);  }
  49.   n++;
  50.    if(n%1==0)
  51.   {
  52.     wenshidu();
  53.     n=0;
  54.    }
  55.    
  56.    delay(3000);
  57.    client.write("mode=up&apikey=你的apikey&data={ck001002life\r\n");  //修改成自己的apikey
  58.    delay(3000);
  59. }

  60. void wenshidu()
  61. {
  62. int temp;//温度
  63. int humi;//湿度
  64. int tol;//校对码
  65. int j;
  66. unsigned int loopCnt;
  67. int chr[40] = {0};//创建数字数组,用来存放40个bit
  68. unsigned long time1;
  69.   bgn:
  70.   delay(2000);
  71. //设置2号接口模式为:输出
  72. //输出低电平20ms(>18ms)
  73. //输出高电平40μs
  74.   pinMode(pin,OUTPUT);
  75.   digitalWrite(pin,LOW);
  76.   delay(20);
  77.   digitalWrite(pin,HIGH);
  78.   delayMicroseconds(40);
  79.   digitalWrite(pin,LOW);
  80. //设置2号接口模式:输入
  81.   pinMode(pin,INPUT);
  82.   //高电平响应信号
  83.   loopCnt=10000;
  84.   while(digitalRead(pin) != HIGH)
  85.   {
  86.     if(loopCnt-- == 0)
  87.     {
  88. //如果长时间不返回高电平,输出个提示,重头开始。
  89.       Serial.println("HIGH");
  90.       goto bgn;
  91.     }
  92.   }
  93.   //低电平响应信号
  94.   loopCnt=30000;
  95.   while(digitalRead(pin) != LOW)
  96.   {
  97.     if(loopCnt-- == 0)
  98.     {
  99. //如果长时间不返回低电平,输出个提示,重头开始。
  100.       Serial.println("LOW");
  101.       goto bgn;
  102.     }
  103.   }
  104. //开始读取bit1-40的数值  
  105.     for(int i=0;i<40;i++)
  106.   {
  107.     while(digitalRead(pin) == LOW)
  108.     {}
  109. //当出现高电平时,记下时间“time”
  110.     time1 = micros();
  111.     while(digitalRead(pin) == HIGH)
  112.     {}
  113. //当出现低电平,记下时间,再减去刚才储存的time
  114. //得出的值若大于50μs,则为‘1’,否则为‘0’
  115. //并储存到数组里去
  116.     if (micros() - time1  >50)
  117.     {
  118.       chr[i]=1;
  119.     }else{
  120.       chr[i]=0;
  121.     }
  122.   }
  123.    
  124. //湿度,8位的bit,转换为数值
  125. humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
  126.    
  127. //温度,8位的bit,转换为数值
  128. temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
  129.   //校对码,8位的bit,转换为数值
  130. //tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
  131. //输出:温度、湿度、校对码
  132.   Serial.print("temp:");
  133.   Serial.println(temp);
  134.   String str1="mode=up&apikey=674c0a5616458361&data={ck001002";
  135.   str1+=temp;
  136.   str1+=".";
  137.   str1+=humi;
  138.   str1+="}\r\n";
  139.   client.print(str1);
  140.    Serial.print(str1);
  141.   Serial.print("humi:");
  142.   Serial.println(humi);
  143.   
  144. }
复制代码
回复

使用道具 举报

发表于 2016-12-10 22:27:13 | 显示全部楼层
我可不可以直接给esp8266连上加速度传感器,然后上传数据给arduino开发板,让开发板处理信号?
我本来打算用arduino nano接MPU-6050和esp8266,不过看资料8266自带MCU,而且楼主这样做了,我也想试试
回复 支持 反对

使用道具 举报

发表于 2016-12-11 12:43:15 | 显示全部楼层
盗版智能创客的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-11 16:06:01 | 显示全部楼层
LOVE_I 发表于 2016-12-11 12:43
盗版智能创客的

是的,改智能创客的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-14 15:02:13 | 显示全部楼层
丶陪妳看流星 发表于 2016-12-10 22:27
我可不可以直接给esp8266连上加速度传感器,然后上传数据给arduino开发板,让开发板处理信号?
我本来打算 ...

可以试一下噢,加油
回复 支持 反对

使用道具 举报

发表于 2017-4-10 17:22:28 | 显示全部楼层
优化得不错喔!!学习了!谢谢
回复 支持 反对

使用道具 举报

发表于 2017-4-12 14:37:56 | 显示全部楼层
您好,对您的设计很感兴趣,可否联系一下?我的QQ:470053905
回复 支持 反对

使用道具 举报

发表于 2017-5-12 08:25:35 | 显示全部楼层
楼主你用的是那个云端?
回复 支持 反对

使用道具 举报

发表于 2020-9-26 15:36:10 | 显示全部楼层
现在还有没有免费的物联平台?
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 10:31 , Processed in 0.043450 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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