极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19163|回复: 1

用DHT11模块上传湿度数据到乐联网

[复制链接]
发表于 2013-6-21 19:34:11 | 显示全部楼层 |阅读模式
本帖最后由 co63oc 于 2013-6-21 19:34 编辑

用DHT11模块上传湿度数据到乐联网

1. 打开http://www.lewei50.com/home/login,输入信息登陆


2. 进入后台管理,点我的网关,右侧点添加新网关,
标识处默认有数字, 类型选对应的类型, 名称处给网关命名,是否可控选是,是否公开选公开,API地址不用输入
点保存提交信息


3. 左侧点我的设备,右侧点新建
输入设备标识,选择一个类型,比如湿度监控
选择刚才新建的网关,名称输入设备名称
是否公开选择公开,这样不用登陆也能查看数据
点保存提交信息


4. 在我的账户,个人信息中查看连接要用到的key,并记录


5. https://github.com/lewei50/LeweiTcpClient/tree/LeweiTcpClientLite下载乐联网客户端连接库, 解压放到arduino libraries目录下


6. 连接arduino uno和W5100模块,直接把W5100模块叠插到arduino板上即可
DHT11温湿度检测模块三个引脚,VCC连接arduino上+5V, GND连接GND, DATA连接模拟输入0端口
W5100上连接上网线,arduino开发板在W5100下面


7. 打开arduino开发环境,输入代码,修改LW_USERKEY,LW_GATEWAY, LW_DEVICE分别为用户key,网关标识,设备标识。UserFunction相关的函数是用来反向连接的,这里没有使用,都注释
上传时间间隔是5秒,如果长时间使用,间隔时间不用这么短
编译并传到arduino开发板上

  1. #define DHT11_PIN 0
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <LeweiTcpClient.h>
  5. byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
  6. #define LW_USERKEY "9861bfff5e844f4e9aab89cd686ce051"
  7. #define LW_GATEWAY "02"
  8. #define LW_DEVICE  "device1"

  9. LeweiTcpClient *client;

  10. byte read_dht11_dat()
  11. {
  12.     byte i = 0;
  13.     byte result = 0;

  14.     for(i=0;i<8;i++)
  15.     {
  16.        while(!(PINC&_BV(DHT11_PIN)));
  17.        delayMicroseconds(30);

  18.        if(PINC&_BV(DHT11_PIN))
  19.            result|=(1<<(7-i));

  20.        while((PINC&_BV(DHT11_PIN)));
  21.     }

  22.     return result;
  23. }

  24. void setup_dht11()
  25. {
  26.     DDRC |= _BV(DHT11_PIN);
  27.     PORTC |= _BV(DHT11_PIN);

  28.     Serial.begin(9600);
  29.     Serial.println("Ready");
  30. }

  31. void setup_tcpclient() {
  32.     // start the Ethernet connection:
  33.     //Ethernet.begin(mac, ip);
  34.     client = new LeweiTcpClient(LW_USERKEY, LW_GATEWAY, mac);
  35.     /*
  36.     UserFunction uf1(test1,"testFunction");
  37.     client->addUserFunction(uf1);
  38.     UserFunction uf2 (test2,"testFunction2");
  39.     client->addUserFunction(uf2);
  40.     //  UserFunction uf3 (test3,"testFunction3");
  41.     //  client->addUserFunction(uf3);
  42.     //  UserFunction uf4 (test4,"testFunction4");
  43.     //  client->addUserFunction(uf4);
  44.     //  UserFunction uf5 (test5,"testFunction5");
  45.     //  client->addUserFunction(uf5);
  46.     UserFunction uf6 (test6,"getAllSensors");
  47.     client->addUserFunction(uf6);
  48.     */
  49. }

  50. void setup()
  51. {
  52.     setup_dht11();
  53.     setup_tcpclient();
  54. }

  55. void loop()
  56. {
  57.     byte dht11_dat[5];
  58.     byte dht11_in;
  59.     byte i;
  60.     PORTC &= ~_BV(DHT11_PIN);
  61.     delay(18);

  62.     PORTC|=_BV(DHT11_PIN);
  63.     delayMicroseconds(40);
  64.     DDRC &= ~_BV(DHT11_PIN);
  65.     delayMicroseconds(40);
  66.     dht11_in = PINC & _BV(DHT11_PIN);

  67.     if(dht11_in)
  68.     {
  69.        Serial.println("dht11 start condition 1 not met");
  70.        return;
  71.     }

  72.     delayMicroseconds(80);
  73.     dht11_in=PINC & _BV(DHT11_PIN);
  74.     if(!dht11_in)
  75.     {
  76.        Serial.println("dht11 start condition 2 not met");
  77.        return;
  78.     }

  79.     delayMicroseconds(80);

  80.     for(i=0;i<5;i++)
  81.        dht11_dat[i]=read_dht11_dat();

  82.     DDRC|=_BV(DHT11_PIN);
  83.     PORTC|=_BV(DHT11_PIN);
  84.     byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];

  85.     if(dht11_dat[4]!=dht11_check_sum)
  86.     {
  87.        Serial.println("DHT11 checksum error");
  88.     }

  89.     Serial.print("humdity= ");
  90.     Serial.print(dht11_dat[0], DEC);
  91.     Serial.print(".");
  92.     Serial.print(dht11_dat[1], DEC);
  93.     Serial.print("%");
  94.     Serial.print("temper = ");
  95.     Serial.print(dht11_dat[2], DEC);
  96.     Serial.print(".");
  97.     Serial.print(dht11_dat[3], DEC);
  98.     Serial.println("C");

  99.     client->keepOnline();
  100.     client->sendSensorValue(LW_DEVICE, dht11_dat[0]);

  101.     delay(5000);  //上传时间间隔
  102. }
复制代码


8. 打开串口查看,能看到获取的ip和正在上传数据


9. http://www.lewei50.com/user/clientIndex实时数据中能看到环境湿度曲线
统计分析及报表->数据导出,能导出上传的数据


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-7-11 20:48:50 | 显示全部楼层
#include <dht.h>
#define DHT_PIN A0
DHT sensor = DHT();
float temp=0.0,hum=0.0;
void setup() {
  Serial.begin(9600);
  sensor.attach(DHT_PIN);
}
void loop() {
  sensor.update();
  uint16_t tempData = (sensor.getTemperatureInt() << 8) | sensor.getTemperatureFrac();
  sensor.update();
  uint16_t humiData = (sensor.getHumidityInt() << 8) | sensor.getHumidityFrac();
  temp=tempData/10.0;
  hum=humiData/10.0;
  Serial.print(temp);
  Serial.print("   ");
  Serial.println(hum);
  delay(1000);
}
你好,这段是关于dht21测试代码,我想问下这段代码有什么问题,为什么一直显示一种值,,
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 17:32 , Processed in 0.058514 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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