uno+w5100向yeelink传数据,经常会没反应
本帖最后由 lemodd 于 2013-4-17 17:19 编辑uno+w5100向yeelink传数据,经常在运行4,5个小时后会停止上传数据,断电重起,或按reset键可以恢复,
但是过一段时间又会出现同样问题,我参考修改的官方代码,请问有什么方法解决,或怎么在程序内过一段时间自动reset下,谢谢!!
我的yeelink网址http://www.yeelink.net/devices/2432
还有代码#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3
#define LIGHTPIN 0 //light sensor > analog pin 0
#define APIKEY "******************************" // replace your yeelink api key here
#define DEVICEID 2*** // replace your device ID
#define TEMPERATURE_SENSORID 3*** // temperature
#define HUMIDITY_SENSORID 3*** //humidity
#define LIGHT_SENSORID 3*** //light
// assign a MAC address for the ethernet controller.
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net"; // name address for yeelink API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 30*1000; // delay between 2 datapoints, 30s
int sensor = 0; //which sesor to read
//0 temperature
//1 humidity
//2 light
void setup()
{
Serial.begin(115200);
pinMode(resetPIN,OUTPUT);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for(;;)
;
}
else {
Serial.println("Ethernet configuration OK");
}
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
void loop()
{
// if there's incoming data from the net connection.
// send it out the serial port.This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// read sensor data, replace with your code
//int sensorReading = readLightSensor();
//int tempC = analogRead(0)*500/1024;
//send data to server
switch(sensor){
case 0:
sendData(readSensor(HUMIDITY_SENSORID), HUMIDITY_SENSORID);
break;
case 1:
sendData(readSensor(TEMPERATURE_SENSORID), TEMPERATURE_SENSORID);
break;
case 2:
sendData(readSensor(LIGHT_SENSORID), LIGHT_SENSORID);
break;
}
if(++sensor > 2) sensor = 0;
}
// store the state of the connection for next time through
// the loop:
//
lastConnected = client.connected();
}
int readSensor(int sensor_id){
Serial.println("\n");
if (sensor_id == LIGHT_SENSORID)
{
int v = analogRead(LIGHTPIN);
Serial.print("light:");
Serial.println(v);
return v;
}
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
if(sensor_id == HUMIDITY_SENSORID){
Serial.print("Humidity (%): ");
Serial.println(DHT11.humidity);
return DHT11.humidity;
}else{
Serial.print("Temperature (C): ");
Serial.println(DHT11.temperature);
return DHT11.temperature;
}
}
void sendData(int thisData, int sensor_id) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("POST /v1.0/device/");
client.print(DEVICEID);
client.print("/sensor/");
client.print(sensor_id);
client.print("/datapoints");
client.println(" HTTP/1.1");
client.println("Host: api.yeelink.net");
client.print("Accept: *");
client.print("/");
client.println("*");
client.print("U-ApiKey: ");
client.println(APIKEY);
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for {"value":} + number of digits of the data:
int thisLength = 10 + getLength(thisData);
client.println(thisLength);
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.print("{\"value\":");
client.print(thisData);
client.println("}");
}
else {
// if you couldn't make a connection:
//
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
// This method calculates the number of digits in the
// sensor reading.Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
建议发到yeelink论坛,那里有官方的人候着的 是不是while那里出了什么问题? 怎么会这样啊 这个看不明白,你是怎么用的,我认为删了就行 yimenwang 发表于 2013-5-5 15:44 static/image/common/back.gif
怎么会这样啊
楼主的代码中,没有定义resetPIN,所以报错。
页:
[1]