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

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 33856|回复: 19

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

[复制链接]
发表于 2013-5-30 12:39 | 显示全部楼层 |阅读模式
本帖最后由 疯子。 于 2013-6-22 17:31 编辑

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

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

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

代码如下:(我注释掉了串口输出的代码,需要的话可以取消掉注释)
DHT11库文件在2楼
[mw_shl_code=c,true]/*
   open.lewei50.com  sensor  client
*/

#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[2];

// 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;
  double  val=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[0]<<8)|buff[1])/1.2;
  }
  return val;
}


[/mw_shl_code]


dht11库文件 Dht11.rar (2.55 KB, 下载次数: 273)

c260b88fa0ec08fad015771458ee3d6d54fbdacb.jpg

还可以从乐联网API读取数据做个微信。
9e072a4f78f0f736e7c628b20b55b319ebc4132f.jpg
发表于 2014-11-10 19:35 | 显示全部楼层
怎么连接微信
发表于 2013-5-30 13:38 | 显示全部楼层
以后尽量把内容发在一楼,这样方便生成文章
 楼主| 发表于 2013-5-30 13:53 | 显示全部楼层
coloz 发表于 2013-5-30 13:38
以后尽量把内容发在一楼,这样方便生成文章

嗯  统一到一楼了
发表于 2013-5-30 18:43 | 显示全部楼层
coloz 发表于 2013-5-30 13:38
以后尽量把内容发在一楼,这样方便生成文章

敢不敢爆自己真人
发表于 2013-8-29 12:55 | 显示全部楼层
疯子。 发表于 2013-5-30 13:53
嗯  统一到一楼了

那个微信怎么连接啊
发表于 2014-6-30 17:56 | 显示全部楼层
请问以上的硬件在哪里购买啊?
发表于 2015-2-13 18:45 | 显示全部楼层
最好详细点,这让我等小白没那么吃力
发表于 2015-3-4 21:09 | 显示全部楼层
请问,我加了显示器用了8,9,10,11,12引脚就不能连接乐联网了
发表于 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设为高电平输出即可。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 00:55 , Processed in 0.274671 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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