co63oc 发表于 2013-6-21 19:34:11

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

本帖最后由 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开发板上

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

LeweiTcpClient *client;

byte read_dht11_dat()
{
    byte i = 0;
    byte result = 0;

    for(i=0;i<8;i++)
    {
       while(!(PINC&_BV(DHT11_PIN)));
       delayMicroseconds(30);

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

       while((PINC&_BV(DHT11_PIN)));
    }

    return result;
}

void setup_dht11()
{
    DDRC |= _BV(DHT11_PIN);
    PORTC |= _BV(DHT11_PIN);

    Serial.begin(9600);
    Serial.println("Ready");
}

void setup_tcpclient() {
    // start the Ethernet connection:
    //Ethernet.begin(mac, ip);
    client = new LeweiTcpClient(LW_USERKEY, LW_GATEWAY, mac);
    /*
    UserFunction uf1(test1,"testFunction");
    client->addUserFunction(uf1);
    UserFunction uf2 (test2,"testFunction2");
    client->addUserFunction(uf2);
    //UserFunction uf3 (test3,"testFunction3");
    //client->addUserFunction(uf3);
    //UserFunction uf4 (test4,"testFunction4");
    //client->addUserFunction(uf4);
    //UserFunction uf5 (test5,"testFunction5");
    //client->addUserFunction(uf5);
    UserFunction uf6 (test6,"getAllSensors");
    client->addUserFunction(uf6);
    */
}

void setup()
{
    setup_dht11();
    setup_tcpclient();
}

void loop()
{
    byte dht11_dat;
    byte dht11_in;
    byte i;
    PORTC &= ~_BV(DHT11_PIN);
    delay(18);

    PORTC|=_BV(DHT11_PIN);
    delayMicroseconds(40);
    DDRC &= ~_BV(DHT11_PIN);
    delayMicroseconds(40);
    dht11_in = PINC & _BV(DHT11_PIN);

    if(dht11_in)
    {
       Serial.println("dht11 start condition 1 not met");
       return;
    }

    delayMicroseconds(80);
    dht11_in=PINC & _BV(DHT11_PIN);
    if(!dht11_in)
    {
       Serial.println("dht11 start condition 2 not met");
       return;
    }

    delayMicroseconds(80);

    for(i=0;i<5;i++)
       dht11_dat=read_dht11_dat();

    DDRC|=_BV(DHT11_PIN);
    PORTC|=_BV(DHT11_PIN);
    byte dht11_check_sum = dht11_dat+dht11_dat+dht11_dat+dht11_dat;

    if(dht11_dat!=dht11_check_sum)
    {
       Serial.println("DHT11 checksum error");
    }

    Serial.print("humdity= ");
    Serial.print(dht11_dat, DEC);
    Serial.print(".");
    Serial.print(dht11_dat, DEC);
    Serial.print("%");
    Serial.print("temper = ");
    Serial.print(dht11_dat, DEC);
    Serial.print(".");
    Serial.print(dht11_dat, DEC);
    Serial.println("C");

    client->keepOnline();
    client->sendSensorValue(LW_DEVICE, dht11_dat);

    delay(5000);//上传时间间隔
}


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


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


某科学的小白鼠 发表于 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测试代码,我想问下这段代码有什么问题,为什么一直显示一种值,,
页: [1]
查看完整版本: 用DHT11模块上传湿度数据到乐联网