arduino+传感器+W5100+乐联网=室内环境监测-Arduino中文社区 - Powered by Discuz! Archiver

疯子。 发表于 2013-5-30 12:39

arduino+传感器+W5100+乐联网=室内环境监测

本帖最后由 疯子。 于 2013-6-22 17:31 编辑

让你的微博自动播报乐联网设备数据(用微博定时播报数据)
Arduino+乐联网平台搭建室内环境监测系统(这篇文章中不需要W5100网络模块,用的是串口上传工具)利用乐联网实现短信监控报警设备掉线了或者传感器超过正常值会短信提示

首先,需要如下东西:
Arduino板子一块(必需)
W5100网络模块(必需)
DHT11模块一枚(可选)
LM35温度模块一枚(可选)
PPD42NS空气颗粒物传感器(可选)

除了板子,其他都是可选,你采集的空气相关数据与你的传感器相关,有了就采集数据,没有就不用了。

代码如下:(我注释掉了串口输出的代码,需要的话可以取消掉注释)
DHT11库文件在2楼
/*
   open.lewei50.comsensorclient
*/

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <dht11.h>

#define USERKEY         "dce04945f5044a15a8cfbcd5555fd8bc"//lewei userkey

dht11 DHT11;
#define DHT11PIN 2

int BH1750address = 0x23;//BH1750 I2C地址
byte buff;

// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// initialize the library instance:
EthernetClient client;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121);      // numeric IP for api.cosm.com
char server[] = "open.lewei50.com";   // name address for cosm API

int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

void setup() {
//Serial.begin(9600);
pinMode(8,INPUT);
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
    //Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
}
else{
    //Serial.println("Ethernet configuration OK");
}
Wire.begin();
starttime = millis();
}

void loop() {
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;

if ((millis()-starttime) > sampletime_ms)
{
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);// Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    //Serial.println();
    //Serial.print(lowpulseoccupancy);
    //Serial.print(",");
    //Serial.print(ratio);
    //Serial.print(",");
    //Serial.println(concentration);
    ////////////////////////////////////
    //Serial.println("http post:");
    //lm35
    int lm35temp = 30;
    //int lm35n = analogRead(A0);//读取A0口的电压值
    //int lm35temp = lm35n * (5 / 1023);//温度数据,温度数据由电压值换算得到
    //Serial.print("lm35temp:");
    //Serial.println(lm35temp);

    // DHT11传感器数据
    DHT11.read(DHT11PIN);
    int dht11hum = DHT11.humidity;
    //Serial.print("shidu:");
    //Serial.println(dht11hum);
    int dht11temp = DHT11.temperature;
    //Serial.print("wendu:");
    //Serial.println(dht11temp);

    // BH1750
    int light = BH1750()*100;
    //Serial.print("light:");
    //Serial.println(light);
    sendData(lm35temp,dht11hum,dht11temp,light,concentration);
    ////////////////////////////////////////////////
    //Serial.println("disconnecting.");
    client.stop();
    lowpulseoccupancy = 0;
    starttime = millis();
}
}

// this method makes a HTTP connection to the server:
void sendData(int lm35temp,int dht11hum,int dht11temp,int light,int thisData) {
// if there's a successful connection:
if (client.connect(server, 80)) {
    //Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("POST /api/V1/gateway/Updatesensors/01 "); // 01代表01网关,如果是02网关这里换成02
    client.println("HTTP/1.1");
    client.print("userkey: ");
    client.println(USERKEY);
    client.println("Host: open.lewei50.com ");
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for "sensor1," + number of digits of the data:
    int thisLength = 116 + getLength(lm35temp) + getLength(dht11hum) + getLength(dht11temp) + getLength(light) + getLength(thisData);
    client.println(thisLength);

    // last pieces of the HTTP PUT request:
    //client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    // 这里的用p1,是因为用户在系统里面已经添加了一个传感器缩写叫p1的传感器 (在01网关下面)

    client.print("[{\"Name\":\"t1\",\"Value\":");   
    client.print(lm35temp);
    client.print("},{\"Name\":\"t2\",\"Value\":");
    client.print(dht11hum);
    client.print("},{\"Name\":\"t3\",\"Value\":");
    client.print(dht11temp);
    client.print("},{\"Name\":\"t4\",\"Value\":");
    client.print(light);
    client.print("},{\"Name\":\"t5\",\"Value\":");
    client.print(thisData);
    client.println("}]");

    //Serial.print("[{\"Name\":\"t1\",\"Value\":");   
    //Serial.print(lm35temp);
    //Serial.print("},{\"Name\":\"t2\",\"Value\":");
    //Serial.print(dht11hum);
    //Serial.print("},{\"Name\":\"t3\",\"Value\":");
    //Serial.print(dht11temp);
    //Serial.print("},{\"Name\":\"t4\",\"Value\":");
    //Serial.print(light);
    //Serial.print("},{\"Name\":\"t5\",\"Value\":");
    //Serial.print(thisData);
    //Serial.println("}]");
}
else {
    // if you couldn't make a connection:
    //Serial.println("connection failed");
    //Serial.println();
    //Serial.println("disconnecting.");
    client.stop();
}
// note the time that the connection was made or attempted:
}


// This method calculates the number of digits in the
// sensor reading.Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
    dividend = dividend /10;
    digits++;
}
// return the number of digits:
return digits;
}

double BH1750() //BH1750设备操作
{
int i=0;
doubleval=0;
//开始I2C读写操作
Wire.beginTransmission(BH1750address);
Wire.write(0x10);//1lx reolution 120ms//发送命令
Wire.endTransmission();
delay(200);
//读取数据
Wire.beginTransmission(BH1750address);
Wire.requestFrom(BH1750address, 2);
while(Wire.available()) //
{
    buff = Wire.read();// receive one byte
    i++;
}
Wire.endTransmission();
if(2==i)
{
    val=((buff<<8)|buff)/1.2;
}
return val;
}





dht11库文件



还可以从乐联网API读取数据做个微信。

ARDUINO-Q 发表于 2014-11-10 19:35

怎么连接微信

coloz 发表于 2013-5-30 13:38

以后尽量把内容发在一楼,这样方便生成文章

疯子。 发表于 2013-5-30 13:53

coloz 发表于 2013-5-30 13:38 static/image/common/back.gif
以后尽量把内容发在一楼,这样方便生成文章

嗯统一到一楼了

szchenwei 发表于 2013-5-30 18:43

coloz 发表于 2013-5-30 13:38 static/image/common/back.gif
以后尽量把内容发在一楼,这样方便生成文章

敢不敢爆自己真人

smartmos 发表于 2013-8-29 12:55

疯子。 发表于 2013-5-30 13:53
嗯统一到一楼了

那个微信怎么连接啊

jialao 发表于 2014-6-30 17:56

请问以上的硬件在哪里购买啊?

enneo 发表于 2015-2-13 18:45

最好详细点,这让我等小白没那么吃力

YAOLONG 发表于 2015-3-4 21:09

请问,我加了显示器用了8,9,10,11,12引脚就不能连接乐联网了

lcb163 发表于 2015-3-13 21:19

YAOLONG 发表于 2015-3-4 21:09
请问,我加了显示器用了8,9,10,11,12引脚就不能连接乐联网了

Arduino与W5100 Ethernet扩展板相连时占用了数字引脚11、12和13,引脚10和4是从属选择,因为W5100芯片和SD卡公用SPI口,而在同一时间只能有一个激活。如果要使用W5100,把引脚10设为低电平输出,引脚4设为高电平输出即可。
页: [1] 2
查看完整版本: arduino+传感器+W5100+乐联网=室内环境监测