muggle 发表于 2012-12-2 10:08:07

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

物联网的发展,给了大家更多的学习机会。以前如果要管理数据,一定要自己开发平台,或者使用GooglePower一样的固定平台。有了COSM和Yeelink,大家可以更多地发挥自己的想法,像搭积木一样搭载硬件和软件。

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

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

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

程序:
/*
Pachube Data Out

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

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

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

* Created 22 April 2011
* By Jeffrey Sun
* http://code.google.com/p/pachubelibrary/

Arduino学习笔记:2560+W5100试验实时室温对Pachubbe.com推送 - Powered by Discuz!
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=544
*/
#include <Arduino.h>
#include <HardwareSerial.h>
#include <ERxPachube.h>
#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <dht11.h>

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

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

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

dht11 DHT11;
#define DHT11PIN 2

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

char SensorDataTemp01;
char SensorDataTemp02;
char SensorDataTemp03;
char SensorDataTemp04;
uint16_t Tc_100;
uint8_t i,whole, fract;

ERxPachubeDataOut dataout(PACHUBE_API_KEY, PACHUBE_FEED_ID);

void PrintDataStream(const ERxPachube& pachube);

void setup() {

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

        dataout.addData(0);
        dataout.addData(1);
        dataout.addData(2);
      dataout.addData(3);
      dataout.addData(4);

      // 初始库
      sensors.begin();
}

void loop() {

      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // 发送命令获取温度
      Serial.println("DONE");
        Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++");

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

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

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

      int chk = DHT11.read(DHT11PIN);
      DHT11.read(DHT11PIN);
//      Serial.print("Humidity (%): ");
//      Serial.println(DHT11.humidity);
//        int SensorDataTemp04 = DHT11.humidity;
      Tc_100 = DHT11.humidity*10;
      whole = Tc_100/10 ;// separate off the whole and fractional portions
      fract = Tc_100 % 10;
      sprintf(SensorDataTemp04,"%d.%d",whole,fract);
      Serial.println(SensorDataTemp04);

      int SensorDataTemp05 = analogRead(A0);   


        dataout.updateData(0, SensorDataTemp01);
        dataout.updateData(1, SensorDataTemp02);
        dataout.updateData(2, SensorDataTemp03);
        dataout.updateData(3, SensorDataTemp04);
        dataout.updateData(4, SensorDataTemp05);

        int status = dataout.updatePachube();

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

        PrintDataStream(dataout);

        delay(28000);
}

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

        Serial.println("<id>,<value>");
        for(unsigned int i = 0; i < count; i++)
        {
                Serial.print(pachube.getIdByIndex(i));
                Serial.print(",");
                Serial.print(pachube.getValueByIndex(i));
                Serial.println();
        }
}

HeartRain 发表于 2013-9-6 09:17:16

没有这个DallasTemperature库啊??急求

muggle 发表于 2013-9-8 10:37:05

HeartRain 发表于 2013-9-6 09:17 static/image/common/back.gif
没有这个DallasTemperature库啊??急求

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

HeartRain 发表于 2013-9-11 17:16:52

muggle 发表于 2013-9-8 10:37 static/image/common/back.gif
你需要同时用到这两个附件

谢谢好人啊v

zhenyuwuxiang 发表于 2013-11-18 19:23:37

求ERxPachube.h库:) 大谢

muggle 发表于 2013-11-18 21:26:08

zhenyuwuxiang 发表于 2013-11-18 19:23 static/image/common/back.gif
求ERxPachube.h库 大谢

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

页: [1]
查看完整版本: 麻瓜学习笔记:01_基于ERxPachube.h库的COSM多传感器数据上传