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

Arduino 添加了温度和湿度传感器(DHT11)的Cosm示例,arduino,ethernet,cosm,Arduino,Ethernet,Cosm,在标准Cosm Arduino传感器客户端示例中添加了温度和湿度传感器(DHT11)。工作一小段时间后,数据流平缓 知道是什么导致了这个问题吗 非常感谢 斯塔扎 /** *Cosm Arduino传感器客户端示例。 * ``*此草图演示如何将Arduino连接到Cosm(https://cosm.com), *使用新的Arduino库发送和接收数据。 /** 添加到COSM示例代码中的DHT11温度和湿度传感器 **/ #包括 #包括 #包括 #包括 #包括 //DHT11***********

在标准Cosm Arduino传感器客户端示例中添加了温度和湿度传感器(DHT11)。工作一小段时间后,数据流平缓

知道是什么导致了这个问题吗

非常感谢

斯塔扎

/**
*Cosm Arduino传感器客户端示例。
*
``*此草图演示如何将Arduino连接到Cosm(https://cosm.com),
*使用新的Arduino库发送和接收数据。
/**
添加到COSM示例代码中的DHT11温度和湿度传感器
**/
#包括
#包括
#包括
#包括
#包括
//DHT11*********************************************************************
dht11 dht11;
#定义DHT11引脚7//DHT11传感器连接到的引脚
//DHT11*********************************************************************
#定义API_密钥“xxxxxx”//Cosm API密钥
#定义FEED\u ID xxxxx//您的Cosm FEED ID
//以太网屏蔽的MAC地址
字节mac[]={xxxx,xxxx,xxxx,xxxx,xxxx};
//我们正在监控的模拟引脚(以太网屏蔽使用0和1)
int-sensorPin=2;
无符号长lastConnectionTime=0;//上次我们连接到Cosm
常量无符号长连接区间=15000;//连接到Cosm之间的延迟(毫秒)
//初始化Cosm库
//为数据流ID定义字符串
char sensorId[]=“传感器读取”;
char sensorId2[]=“DHT11湿度传感器读数”;
字符传感器ID3[]=“DHT11温度传感器读数”;
CosmDatastream数据流[]={
CosmDatastream(传感器ID、strlen(传感器ID)、数据流_FLOAT),
CosmDatastream(传感器ID2、strlen(传感器ID2)、数据流_FLOAT),
CosmDatastream(传感器ID3、strlen(传感器ID3)、数据流_FLOAT),
};
//将数据流包装到提要中
CosmFeed提要(提要ID,数据流,3/*数据流数*/);
以太网络客户端;
CosmClient CosmClient(客户);
无效设置(){
//将安装代码放在此处,以便运行一次:
Serial.begin(9600);
Serial.println(“Cosm传感器客户端示例”);
Serial.println(“===========================================”);
Serial.println(“初始化网络”);
while(以太网开始(mac)!=1){
Serial.println(“通过DHCP获取IP地址时出错,请重试…”);
延迟(15 000);
}
Serial.println(“网络初始化”);
Serial.println();
}
void循环(){
//主程序循环
如果(毫秒()-lastConnectionTime>connectionInterval){
//检查DHT11传感器是否工作正常
int chk=DHT11.read(DHT11PIN);
串行打印(“读取DHT11传感器:”);
开关(chk)
{
案例0:Serial.println(“OK”);中断;
案例1:Serial.println(“校验和错误”);中断;
案例2:Serial.println(“超时错误”);中断;
默认值:Serial.println(“未知错误”);中断;
}
sendData();//将数据发送到Cosm
getData();//从Cosm读回数据流
lastConnectionTime=millis();//更新连接时间,以便在再次连接之前等待
}
}
//将提供的值发送到Cosm,并在执行过程中打印一些调试信息
void sendData(){
int sensorValue=模拟读数(sensorPin);
int humidityDHT11=((浮动)DHT11.湿度);
int tempDHT11=((浮动)DHT11.温度);
数据流[0]。setFloat(传感器值);
数据流[1]。setFloat(humidityDHT11);//DHT11湿度值*******
数据流[2]。setFloat(tempDHT11);//DHT11临时值********
串行打印(“读取传感器值”);
Serial.println(数据流[0].getFloat());
串行打印(“读取DHT11湿度传感器值”);
Serial.println(datastreams[1].getFloat());
串行打印(“读取DHT11温度传感器值”);
Serial.println(datastreams[2].getFloat());
Serial.println(“上传至Cosm”);
int ret=cosmclient.put(提要,API_键);
序列号。打印(“输入返回代码:”);
序列号println(ret);
Serial.println();
}
//从Cosm获取数据流的值,打印出我们收到的值
void getData(){
Serial.println(“从Cosm读取数据”);
int-ret=cosmclient.get(提要,API_键);
Serial.print(“获取返回代码:”);
序列号println(ret);
如果(ret>0){
Serial.print(“数据流为:”);
Serial.println(提要[0]);
串行打印(“传感器值为:”);
Serial.println(提要[0].getFloat());
Serial.print(“数据流为:”);
Serial.println(提要[1]);
串行打印(“传感器值为:”);
Serial.println(提要[1].getFloat());
Serial.print(“数据流为:”);
Serial.println(提要[2]);
串行打印(“传感器值为:”);
Serial.println(提要[2].getFloat());
}
Serial.println();
}

Cosm有一个调试页面,可以为您提供出错的线索

它当前位于:并且在传入请求通过时实时列出所有传入请求。如果您的设备最初工作正常,您应该看到它开始成功地发出请求,并且根据需要多长时间,您可能能够保持此页面打开,并希望看到它何时开始失败

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

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


如果这些都不可行,我建议将您的提要详细信息发送给Cosm支持部门,并让他们进行调查。

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

我昨晚刚从Sparkfun收到我的DHT22(RHT03),并尝试了几个无法编译的示例(我确定是我的错)。使用Arduino Uno“开箱即用”的示例来自Tom Boyd的页面(请确保滚动到
    /**
 * 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();
}