添加了温度和湿度传感器(DHT11)的Cosm示例

时间:2013-04-13 06:37:10

标签: arduino ethernet cosm

在标准的Cosm Arduino传感器客户端示例中添加了温度和湿度传感器(DHT11)。工作一会儿然后数据流扁平化。

知道可能导致问题的原因是什么?

非常感谢

Staza

    /**
 * Cosm Arduino sensor client example.
 *
`` * This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),
 * using the new Arduino library to send and receive data.

/**
DHT11 temp and humidity sensor added to the COSM example code
**/

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <dht11.h>

//DHT11*********************************************************************
dht11 DHT11;
#define DHT11PIN 7//pin DHT11 sensor is connected to
//DHT11*********************************************************************


#define API_KEY "xxxxxx" // your Cosm API key
#define FEED_ID xxxxx // your Cosm feed ID

// MAC address for your Ethernet shield
byte mac[] = {xxxx, xxxx, xxxx, xxxx, xxxx, xxxx}; 

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;

unsigned long lastConnectionTime = 0;                // last time we connected to Cosm
const unsigned long connectionInterval = 15000;      // delay between connecting to Cosm in milliseconds

// Initialize the Cosm library

// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
char sensorId2[] = "DHT11_humidity_sensor_reading";
char sensorId3[] = "DHT11_temperature_sensor_reading";
CosmDatastream datastreams[] = {
  CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
  CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  CosmDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
};

// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 3 /* number of datastreams */);

EthernetClient client;
CosmClient cosmclient(client);

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

  Serial.println("Cosm Sensor Client Example");
  Serial.println("==========================");

  Serial.println("Initializing network");
  while (Ethernet.begin(mac) != 1) {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(15000);
  }

  Serial.println("Network initialized");
  Serial.println();
}

void loop() {
  // main program loop
  if (millis() - lastConnectionTime > connectionInterval) {

//check DHT11 sensor is working OK    
    int chk = DHT11.read(DHT11PIN);
    Serial.print("Read DHT11 sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

    sendData(); // send data to Cosm
    getData(); // read the datastream back from Cosm
    lastConnectionTime = millis(); // update connection time so we wait before connecting again
  }
}

// send the supplied values to Cosm, printing some debug information as we go
void sendData() {
  int sensorValue = analogRead(sensorPin);
  int humidityDHT11 = ((float)DHT11.humidity);
  int tempDHT11 = ((float)DHT11.temperature);
  datastreams[0].setFloat(sensorValue);
  datastreams[1].setFloat(humidityDHT11); //DHT11 humidity value*******
  datastreams[2].setFloat(tempDHT11); //DHT11 temp value********
  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());
  Serial.print("Read DHT11 humidity sensor value ");
  Serial.println(datastreams[1].getFloat());
  Serial.print("Read DHT11 temperature sensor value ");
  Serial.println(datastreams[2].getFloat());

  Serial.println("Uploading to Cosm");
  int ret = cosmclient.put(feed, API_KEY);
  Serial.print("PUT return code: ");
  Serial.println(ret);

  Serial.println();
}

// get the value of the datastream from Cosm, printing out the value we received
void getData() {
  Serial.println("Reading data from Cosm");

  int ret = cosmclient.get(feed, API_KEY);
  Serial.print("GET return code: ");
  Serial.println(ret);

  if (ret > 0) {
    Serial.print("Datastream is: ");
    Serial.println(feed[0]);
    Serial.print("Sensor value is: ");
    Serial.println(feed[0].getFloat());

    Serial.print("Datastream is: ");
    Serial.println(feed[1]);
    Serial.print("Sensor value is: ");
    Serial.println(feed[1].getFloat());

    Serial.print("Datastream is: ");
    Serial.println(feed[2]);
    Serial.print("Sensor value is: ");
    Serial.println(feed[2].getFloat());
   }

  Serial.println();
}

2 个答案:

答案 0 :(得分:1)

Cosm有一个调试页面,可能会给你一个关于出了什么问题的线索。

目前位于:https://cosm.com/users/YOURUSERNAME/debug,它会在所有传入的请求通过时实时列出。如果您的设备最初工作,您应该看到它成功开始发出请求,并且根据它需要多长时间才能使其平坦,您可以保持此页面打开并希望看到它何时开始失败。

当Arduino串口输出似乎停止工作时,你看到了什么,或者看起来Arduino仍然乐意发送数据?

您可以尝试的另一件事是使用Wireshark检查网络上的网络流量。然而,设置这一点稍微复杂一点,所以我建议先尝试其他方法。

如果这一切看起来都不可行,我建议您使用Feed详情发送Cosm支持并让他们查看。

答案 1 :(得分:0)

借调smulube建议监控串口输出。另外,消除COSM代码的变量&amp;以太网:用草图开始调试问题,草图只是从DHT11读取数据并监控计算机上Arduino串行输出中发生的情况(在工具下拉菜单中)。

我昨晚刚从Sparkfun收到我的DHT22(RHT03)并尝试了几个无法编译的样本(我的错,我敢肯定)。我的Arduino Uno为我提供“开箱即用”的样本来自Tom Boyd的页面(请务必滚动到底部以获取最新代码):DHT11 / Aosong AM2302 humidity and temperature sensor

我很好奇:你的传感器要平整多长时间?我将Tom的代码与Cosm代码集成在一起,并且它已经不间断地运行了一个小时了。

干杯, Reeves的