0

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

知道可能导致问题的原因吗?

非常感谢

斯塔扎

    /**
 * 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();
}
4

2 回答 2

1

Cosm 有一个调试页面,它可能会为您提供有关问题所在的线索。

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

当 Arduino 串行输出似乎停止工作时,您是否看到任何东西,或者 Arduino 似乎仍在愉快地发送数据?

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

如果这一切似乎都不可行,我建议将您的提要详细信息邮寄给 Cosm 支持,并让他们调查一下。

于 2013-04-14T10:21:56.593 回答
0

支持 smulube 的建议以监控串行输出。此外,消除 COSM 代码和以太网的变量:使用仅从 DHT11 读取读数的草图开始调试问题,并监控计算机上 Arduino 串行输出中发生的情况(在“工具”下拉菜单中)。

我昨晚刚从 Sparkfun 收到了我的 DHT22 (RHT03),并尝试了几个无法编译的样本(我敢肯定)。使用我的 Arduino Uno 对我来说“开箱即用”的示例来自 Tom Boyd 的页面(请务必滚动到底部以获取最新代码):DHT11 / Aosong AM2302 湿度和温度传感器

我很好奇:你的传感器需要多长时间才能变平?我将 Tom 的代码与 Cosm 代码集成在一起,它已经运行了一个小时而没有中断。

干杯,里夫斯

于 2013-04-20T22:15:26.930 回答