用Arduino将DS18B20的温度上传至xively.com

原文来自:

http://www.instructables.com/id/Arduino-feeding-xivelycom-with-temperature-from-DS/?ALLSTEPS

这个教程会为你讲解:

*连接一个DS18B20温度传感器到Arduino

*用单线接口从传感器读取数据

*直接通过Arduino发送数据至xively.com

*在网络上查看到 结果图

http://xively.com是一个物联网主机,可以基于你提供的数据来生成图片。

在这个例子中我会去监控我房间里的温度。

第一步:材料清单

Arduino测温度1

* 带以太网插板的Arduino 我有一个Arduino Uno clone 和一个WIZnet W5100 插板

* 电源供给(可以通过一个USB口)

我很幸运因为我的路由器上有USB端口可以提供5V的输出,这意味着无论路由器在哪都可以给Arduino供电。

* DS18B20温度传感器

* RJ45 网线连接Arduino和你的路由器

步骤2:原理图 Arduino温度2  * Arduino gnd接地端 接DS18B20两个外部引脚

* Arduino 5V 接4.7k 电阻

* 4.7k电阻的另一个引脚接DS18B20中间引脚

* Arduino 数字引脚2 接DS18B20 中间引脚

* RJ45 电缆连接Arduino和路由器

* Arduino电源线(通过USB或电源插头,我用USB接的路由器)

步骤3:预备知识

Arduino温度4

要求的Arduino的三个库:

onewire 库来自 http://www.pjrc.com/teensy/td_libs_OneWire.html (尤其是 http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip)

httpclient库来自 https://github.com/amcewen/HttpClient(https://github.com/amcewen/HttpClient/archive/master.zi)

Xively来自https://github.com/xively/xively_arduino(https://github.com/xively/xively_arduino/archive/master.zip)

在你的Arduino/库目录中 安装这些库 一个在http://xively.com上申请的账户(之前的pachebe.com和cosm.com)

https://xively.com/get_started/上申请一个免费的开发者账户

选择一个用户名,密码,设置你的地址和时区等等。你会收到一个确认邮件,点击激活链接激活你的账户。 你可以选择test drive测试驱动来学习xively或者跳过,这完全取决于你。

正在做测试驱动?我等待一下~

做完啦?让我们继续

步骤4:在Xively上增加一个新的设备 Arduino温度3  你现在应该在xively的开发设备页。这是兴趣的开始。

点击+ADD Device 给新设备名称如Arduino DS18B20温度记录器。

填写关于这个设备的描述,他的位置如我的创客空间。我喜欢在这加一个我家的网页链接,尽管在之后的指令里有一个专用的web地址区域。 选择数据是否公开。

你真的想让世界知道你的创客空间有多热嘛?我选择空开除非我做的是个闹钟之类的。 点击页面底部的增加设备按钮。

步骤5:你新设备的xively 界面,以及所有的访问代码 Arduino温度5   你现在的页面有大量的信息,不要有压力。

这个页面可以分成8个部分:

* 产品ID,产品秘密,串口号,激活代码

* 状态(已激活),源ID,源网址URL链接(它会显示图片数据),API端点

* “Add channels to your device!” — Arduino会在代码中处理它

* 请求日志–这个是为了调试和确认 Arduino端的运行。

* 位置–地理位置传感器(可选)

* API keys– 我们之后再Arduino代码中需要这个

* 元数据–可编辑 * 触发器– 当一些事情发生时 ping一个网页(例如温度掉到零度以下 )

步骤6:Arduino代码

Arduino温度6

<code>

#include <SPI.h>

#include <Ethernet.h>

#include <HttpClient.h>

#include <Xively.h>

#include <OneWire.h> // MAC address for your Ethernet shield byte

mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Your Xively key to let you upload data char xively

Key[] = “”; // enter the key, under API Keys

unsigned long feedId = 0; // enter your feed ID, under Activated

int frequency = 15000; // delay between updates (milliseconds) // Analog pin which we’re monitoring (0 and 1 are used by the Ethernet shield)

int sensorPin = 2; // Define the strings for our datastream IDs

char sensorId[] = “temp”;

XivelyDatastream datastreams[] = { XivelyDatastream(sensorId, strlen(sensorId),DATASTREAM_FLOAT), }; // Finally, wrap the datastreams into a feed XivelyFeed

feed(feedId, datastreams, 1 /* number of datastreams */);

EthernetClient client; XivelyClient xivelyclient(client); // initialize the one-wire interface OneWire ds(sensorPin);  // on pin 2 (a 4.7K resistor is necessary) /* 5v – 4.7k resistor – 18B20 middle pin – D2 gnd – 18B20 both legs (joined together) */

void setup() {

// put your setup code here, to run once: Serial.begin(9600);

Serial.println(“Starting single datastream upload to Xively…”);

Serial.println();

while (Ethernet.begin(mac) != 1) {

Serial.println(“Error getting IP address via DHCP, trying again…”); delay(5000); }

Serial.print(“IP address: “);

for (byte thisByte = 0; thisByte < 4; thisByte++) {

// print the value of each byte of the IP address:

Serial.print(Ethernet.localIP()[thisByte], DEC);

Serial.print(“.”); }

Serial.println(); }

void loop() {

byte present = 0;

byte type_s;

byte data[12];

byte addr[8];

float celsius;

if ( !ds.search(addr)) {

Serial.println(“No more addresses.”);

delay(250);

ds.reset_search();

return; }

if (OneWire::crc8(addr, 7) != addr[7]) {

Serial.print(“CRC is not valid!”); return; } // the first ROM byte indicates which chip

switch (addr[0]) {

case 0×10: Serial.print(“  Chip = DS18S20″);  // or old DS1820 type_s = 1; break; case 0×28:

Serial.print(“  Chip = DS18B20″);

type_s = 0;

break;

case 0×22: Serial.print(“  Chip = DS1822″);

type_s = 0;

break;

default: Serial.println(“Device is not a DS18x20 family device.”);

return;

}

Serial.println();

ds.reset();

ds.select(addr);

ds.write(0×44, 1);        // start conversion, with parasite power on at the end delay(1000);

// maybe 750ms is enough, maybe not

// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();

ds.select(addr);

ds.write(0xBE);         // Read Scratchpad

for ( i = 0; i < 9; i++) {

// we need 9 bytes data[i] = ds.read();

int16_t raw = (data[1] << 8) | data[0];

if (type_s) { raw = raw << 3; // 9 bit resolution default

if (data[7] == 0×10) {

// “count remain” gives full 12 bit resolution

raw = (raw & 0xFFF0) + 12 – data[6];

}

}

else { byte cfg = (data[4] & 0×60); // at lower res, the low bits are undefined, so let’s zero them

if (cfg == 0×00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms

else if (cfg == 0×20) raw = raw & ~3; // 10 bit res, 187.5 ms

else if (cfg == 0×40) raw = raw & ~1; // 11 bit res, 375 ms //

// default is 12 bit resolution, 750 ms conversion time

}

celsius = (float)raw / 16.0; datastreams[0].setFloat(celsius);

Serial.print(“Current temperature: “);

Serial.print(datastreams[0].getFloat());

Serial.println(” celsius”);

Serial.println(“Uploading it to Xively”);

int ret = xivelyclient.put(feed, xivelyKey);

Serial.print(“xivelyclient.put returned “);

Serial.println(ret); Serial.println();

delay(frequency); }

</code>

步骤7:欢迎来到物联网的世界 Arduino温度7  一旦你上传代码到Arduino,你应该能在请求日志部分看到数据,还有在图片上出现的数据点。

恭喜你,你正在上传你创客空间的数据给全世界看

下一步是什么呢?这取决于你

你一天开多少次前车门?

你的秋海棠的水位是多少?

你的洗车器洗完车了么?

你的咖啡机空着呢么?

你在外出的时候孩子玩音乐有多大声?

有数以万计的可能监控的事物,看看你的周围就可以了。 这里可以看我的创客空间<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> 卧室: https://xively.com/feeds/1238358657

当前温度是26.5摄氏度,应该是秋天了。


国内用户推荐使用:

Yeelink

乐为物联

 

感谢阅读!

更多信息与我们交流:

WIZnet邮箱:wiznetbj@wiznet.co.kr

WIZnet主页:http://www.wiznet.co.kr

WIZnet官方企业微博:http://e.weibo.com/wiznet2012

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值