极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11483|回复: 5

麻瓜学习笔记:01_基于ERxPachube.h库的COSM多传感器数据上传

[复制链接]
发表于 2012-12-2 10:08:07 | 显示全部楼层 |阅读模式
物联网的发展,给了大家更多的学习机会。以前如果要管理数据,一定要自己开发平台,或者使用GooglePower一样的固定平台。有了COSM和Yeelink,大家可以更多地发挥自己的想法,像搭积木一样搭载硬件和软件。

这里有个不成熟的例子,给大家拍砖引玉吧。。。

特点:
1. 使用ERxPachube.h库,方便的上传多个数据
2. 使用DallasTemperature库,使用地址选码方式,获取指定的传感器输出
3. 集成3xDS18B20数字温度传感器,1xDHT11数字湿度传感器,1xCsd模拟光敏传感器

COSM链接: https://cosm.com/feeds/89080

程序:
  1. /*
  2. Pachube Data Out

  3. Demonstrates use of the ERxPachube library.
  4. Push local sensor data to Pachube server.
  5. If you don't have a Pachube account, register one first ([url]http://www.pachube.com/[/url]).

  6. To run this sketch, you need:
  7. 1. Create a same feed as [url]http://www.pachube.com/feeds/23408[/url]
  8.     (A manual feed with three data streams with ids 0, 1, 2.)
  9. 2. Use your API key to replace the space holer PACHUBE_API_KEY below.
  10. 3. Use your feed id to replace the space holer feed id 23408 below.

  11. Circuit:
  12. * Ethernet shield attached to pins 10, 11, 12, 13

  13. * Created 22 April 2011
  14. * By Jeffrey Sun
  15. * [url]http://code.google.com/p/pachubelibrary/[/url]

  16. Arduino学习笔记:2560+W5100试验实时室温对Pachubbe.com推送 - Powered by Discuz!
  17. [url]http://www.geek-workshop.com/forum.php?mod=viewthread&tid=544[/url]
  18. */
  19. #include <Arduino.h>
  20. #include <HardwareSerial.h>
  21. #include <ERxPachube.h>
  22. #include <Ethernet.h>
  23. #include <SPI.h>
  24. #include <OneWire.h>
  25. #include <DallasTemperature.h>
  26. #include <dht11.h>

  27. byte mac[] = { 0xCC, 0xAC, 0xBE, 0xEF, 0xFE, 0x91 }; // make sure this is unique on your network
  28. byte ip[] = { 192, 168, 2, 150   };                  // no DHCP so we set our own IP address

  29. #define PACHUBE_API_KEY                                "PACHUBE_API_KEY" // fill in your API key PACHUBE_API_KEY
  30. #define PACHUBE_FEED_ID                                "xxxxx" // fill in your feed id

  31. // 定义DS18B20数据口连接arduino的2号IO上
  32. #define ONE_WIRE_BUS 4

  33. dht11 DHT11;
  34. #define DHT11PIN 2

  35. // 初始连接在单总线上的单总线设备
  36. OneWire oneWire(ONE_WIRE_BUS);
  37. DallasTemperature sensors(&oneWire);
  38. DeviceAddress Thermometer01 = { 0x28, 0xF9, 0xE0, 0xC9, 0x02, 0x00, 0x00, 0xC5 };
  39. DeviceAddress Thermometer02 = { 0x28, 0x09, 0xE8, 0xC9, 0x02, 0x00, 0x00, 0x89 };
  40. DeviceAddress Thermometer03 = { 0x28, 0x84, 0xD6, 0xC9, 0x02, 0x00, 0x00, 0x0F };

  41. char SensorDataTemp01[20];
  42. char SensorDataTemp02[20];
  43. char SensorDataTemp03[20];
  44. char SensorDataTemp04[20];
  45. uint16_t Tc_100;
  46. uint8_t i,whole, fract;

  47. ERxPachubeDataOut dataout(PACHUBE_API_KEY, PACHUBE_FEED_ID);

  48. void PrintDataStream(const ERxPachube& pachube);

  49. void setup() {

  50.         Serial.begin(9600);
  51.         Ethernet.begin(mac, ip);

  52.         dataout.addData(0);
  53.         dataout.addData(1);
  54.         dataout.addData(2);
  55.         dataout.addData(3);
  56.         dataout.addData(4);

  57.         // 初始库
  58.         sensors.begin();
  59. }

  60. void loop() {
  61.   
  62.         Serial.print("Requesting temperatures...");
  63.         sensors.requestTemperatures(); // 发送命令获取温度
  64.         Serial.println("DONE");  
  65.         Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++");

  66. //        int SensorDataTemp01 = sensors.getTempC(Thermometer01);
  67.         Tc_100 = sensors.getTempC(Thermometer01)*10;
  68.         whole = Tc_100/10 ;  // separate off the whole and fractional portions
  69.         fract = Tc_100 % 10;
  70.         sprintf(SensorDataTemp01,"%d.%d",whole,fract);
  71.         Serial.println(SensorDataTemp01);

  72. //        int SensorDataTemp02 = sensors.getTempC(Thermometer02);
  73.         Tc_100 = sensors.getTempC(Thermometer02)*10;
  74.         whole = Tc_100/10 ;  // separate off the whole and fractional portions
  75.         fract = Tc_100 % 10;
  76.         sprintf(SensorDataTemp02,"%d.%d",whole,fract);
  77.         Serial.println(SensorDataTemp02);

  78. //        int SensorDataTemp03 = sensors.getTempC(Thermometer03);
  79.         Tc_100 = sensors.getTempC(Thermometer03)*10;
  80.         whole = Tc_100/10 ;  // separate off the whole and fractional portions
  81.         fract = Tc_100 % 10;
  82.         sprintf(SensorDataTemp03,"%d.%d",whole,fract);
  83.         Serial.println(SensorDataTemp03);

  84.         int chk = DHT11.read(DHT11PIN);
  85.         DHT11.read(DHT11PIN);
  86. //        Serial.print("Humidity (%): ");
  87. //        Serial.println(DHT11.humidity);
  88. //        int SensorDataTemp04 = DHT11.humidity;
  89.         Tc_100 = DHT11.humidity*10;
  90.         whole = Tc_100/10 ;  // separate off the whole and fractional portions
  91.         fract = Tc_100 % 10;
  92.         sprintf(SensorDataTemp04,"%d.%d",whole,fract);
  93.         Serial.println(SensorDataTemp04);

  94.         int SensorDataTemp05 = analogRead(A0);   


  95.         dataout.updateData(0, SensorDataTemp01);
  96.         dataout.updateData(1, SensorDataTemp02);
  97.         dataout.updateData(2, SensorDataTemp03);
  98.         dataout.updateData(3, SensorDataTemp04);
  99.         dataout.updateData(4, SensorDataTemp05);

  100.         int status = dataout.updatePachube();

  101.         Serial.print("sync status code <OK == 200> => ");
  102.         Serial.println(status);

  103.         PrintDataStream(dataout);

  104.         delay(28000);
  105. }

  106. void PrintDataStream(const ERxPachube& pachube)
  107. {
  108.         unsigned int count = pachube.countDatastreams();
  109.         Serial.print("data count=> ");
  110.         Serial.println(count);

  111.         Serial.println("<id>,<value>");
  112.         for(unsigned int i = 0; i < count; i++)
  113.         {
  114.                 Serial.print(pachube.getIdByIndex(i));
  115.                 Serial.print(",");
  116.                 Serial.print(pachube.getValueByIndex(i));
  117.                 Serial.println();
  118.         }
  119. }
复制代码
回复

使用道具 举报

发表于 2013-9-6 09:17:16 | 显示全部楼层
没有这个DallasTemperature库啊??急求
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-9-8 10:37:05 | 显示全部楼层
HeartRain 发表于 2013-9-6 09:17
没有这个DallasTemperature库啊??急求

你需要同时用到这两个附件

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

发表于 2013-9-11 17:16:52 | 显示全部楼层
muggle 发表于 2013-9-8 10:37
你需要同时用到这两个附件

谢谢好人啊v
回复 支持 反对

使用道具 举报

发表于 2013-11-18 19:23:37 | 显示全部楼层
求ERxPachube.h库 大谢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-18 21:26:08 | 显示全部楼层
zhenyuwuxiang 发表于 2013-11-18 19:23
求ERxPachube.h库 大谢

PACHUBE/COSM已经更名www.xively.com,虽然老的库文件还能使用,但是推荐用官方新版。

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-10 05:28 , Processed in 0.041062 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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