极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12916|回复: 5

uno+w5100向yeelink传数据,经常会没反应

[复制链接]
发表于 2013-4-17 17:17:55 | 显示全部楼层 |阅读模式
本帖最后由 lemodd 于 2013-4-17 17:19 编辑

uno+w5100向yeelink传数据,经常在运行4,5个小时后会停止上传数据,断电重起,或按reset键可以恢复,
但是过一段时间又会出现同样问题,我参考修改的官方代码,请问有什么方法解决,或怎么在程序内过一段时间自动reset下,谢谢!!
我的yeelink网址http://www.yeelink.net/devices/2432
还有代码
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <dht11.h>

  4. dht11 DHT11;
  5. #define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3
  6. #define LIGHTPIN 0 //light sensor > analog pin 0

  7. #define APIKEY         "******************************" // replace your yeelink api key here
  8. #define DEVICEID                2*** // replace your device ID
  9. #define TEMPERATURE_SENSORID    3*** // temperature
  10. #define HUMIDITY_SENSORID       3*** //humidity
  11. #define LIGHT_SENSORID          3*** //light

  12. // assign a MAC address for the ethernet controller.
  13. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  14. // initialize the library instance:
  15. EthernetClient client;
  16. char server[] = "api.yeelink.net";   // name address for yeelink API

  17. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  18. boolean lastConnected = false;                 // state of the connection last time through the main loop
  19. const unsigned long postingInterval = 30*1000; // delay between 2 datapoints, 30s


  20. int sensor = 0;   //which sesor to read
  21.                   //0 temperature
  22.                   //1 humidity
  23.                   //2 light


  24. void setup()
  25. {
  26.   Serial.begin(115200);
  27.   pinMode(resetPIN,OUTPUT);

  28.   if (Ethernet.begin(mac) == 0) {
  29.     Serial.println("Failed to configure Ethernet using DHCP");
  30.     for(;;)
  31.       ;
  32.   }
  33.   else {
  34.     Serial.println("Ethernet configuration OK");
  35.   }

  36.   Serial.println("DHT11 TEST PROGRAM ");
  37.   Serial.print("LIBRARY VERSION: ");
  38.   Serial.println(DHT11LIB_VERSION);
  39.   Serial.println();
  40. }

  41. void loop()
  42. {
  43.   
  44.   // if there's incoming data from the net connection.
  45.   // send it out the serial port.  This is for debugging
  46.   // purposes only:
  47.   if (client.available()) {
  48.     char c = client.read();
  49.     Serial.print(c);
  50.   }

  51.   // if there's no net connection, but there was one last time
  52.   // through the loop, then stop the client:
  53.   if (!client.connected() && lastConnected) {
  54.     Serial.println();
  55.     Serial.println("disconnecting.");
  56.     client.stop();
  57.   }

  58.   // if you're not connected, and ten seconds have passed since
  59.   // your last connection, then connect again and send data:
  60.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  61.     // read sensor data, replace with your code
  62.     //int sensorReading = readLightSensor();
  63.     //int tempC = analogRead(0)*500/1024;
  64.     //send data to server
  65.    
  66.    
  67.       switch(sensor){
  68.         case 0:
  69.           sendData(readSensor(HUMIDITY_SENSORID), HUMIDITY_SENSORID);
  70.           break;
  71.         case 1:
  72.           sendData(readSensor(TEMPERATURE_SENSORID), TEMPERATURE_SENSORID);
  73.           break;
  74.         case 2:
  75.           sendData(readSensor(LIGHT_SENSORID), LIGHT_SENSORID);
  76.           break;
  77.       }
  78.       
  79.       if(++sensor > 2) sensor = 0;


  80.   }
  81.   // store the state of the connection for next time through
  82.   // the loop:
  83.   //
  84.   
  85.   lastConnected = client.connected();
  86. }


  87. int readSensor(int sensor_id){
  88.   Serial.println("\n");

  89.   if (sensor_id == LIGHT_SENSORID)
  90.   {
  91.     int v = analogRead(LIGHTPIN);
  92.     Serial.print("light:");
  93.     Serial.println(v);
  94.     return v;
  95.   }


  96.   int chk = DHT11.read(DHT11PIN);

  97.   Serial.print("Read sensor: ");
  98.   switch (chk)
  99.   {
  100.     case DHTLIB_OK:
  101.                 Serial.println("OK");
  102.                 break;
  103.     case DHTLIB_ERROR_CHECKSUM:
  104.                 Serial.println("Checksum error");
  105.                 break;
  106.     case DHTLIB_ERROR_TIMEOUT:
  107.                 Serial.println("Time out error");
  108.                 break;
  109.     default:
  110.                 Serial.println("Unknown error");
  111.                 break;
  112.   }

  113.   if(sensor_id == HUMIDITY_SENSORID){
  114.     Serial.print("Humidity (%): ");
  115.     Serial.println(DHT11.humidity);
  116.     return DHT11.humidity;
  117.   }else{
  118.     Serial.print("Temperature (C): ");
  119.     Serial.println(DHT11.temperature);
  120.     return DHT11.temperature;
  121.   }
  122. }


  123. void sendData(int thisData, int sensor_id) {
  124.   // if there's a successful connection:
  125.   if (client.connect(server, 80)) {
  126.     Serial.println("connecting...");
  127.     // send the HTTP PUT request:
  128.     client.print("POST /v1.0/device/");
  129.     client.print(DEVICEID);
  130.     client.print("/sensor/");
  131.     client.print(sensor_id);
  132.     client.print("/datapoints");
  133.     client.println(" HTTP/1.1");
  134.     client.println("Host: api.yeelink.net");
  135.     client.print("Accept: *");
  136.     client.print("/");
  137.     client.println("*");
  138.     client.print("U-ApiKey: ");
  139.     client.println(APIKEY);
  140.     client.print("Content-Length: ");

  141.     // calculate the length of the sensor reading in bytes:
  142.     // 8 bytes for {"value":} + number of digits of the data:
  143.     int thisLength = 10 + getLength(thisData);
  144.     client.println(thisLength);

  145.     client.println("Content-Type: application/x-www-form-urlencoded");
  146.     client.println("Connection: close");
  147.     client.println();

  148.     // here's the actual content of the PUT request:
  149.     client.print("{"value":");
  150.     client.print(thisData);
  151.     client.println("}");
  152.   }
  153.   else {
  154.     // if you couldn't make a connection:
  155.     //
  156.    
  157.     Serial.println("connection failed");
  158.     Serial.println();
  159.     Serial.println("disconnecting.");
  160.     client.stop();
  161.   }
  162.    // note the time that the connection was made or attempted:
  163.   lastConnectionTime = millis();
  164. }

  165. // This method calculates the number of digits in the
  166. // sensor reading.  Since each digit of the ASCII decimal
  167. // representation is a byte, the number of digits equals
  168. // the number of bytes:
  169. int getLength(int someValue) {
  170.   // there's at least one byte:
  171.   int digits = 1;
  172.   // continually divide the value by ten,
  173.   // adding one to the digit count for each
  174.   // time you divide, until you're at 0:
  175.   int dividend = someValue /10;
  176.   while (dividend > 0) {
  177.     dividend = dividend /10;
  178.     digits++;
  179.   }
  180.   // return the number of digits:
  181.   return digits;
  182. }
复制代码
回复

使用道具 举报

发表于 2013-5-2 00:09:35 | 显示全部楼层
建议发到yeelink论坛,那里有官方的人候着的
回复 支持 反对

使用道具 举报

发表于 2013-5-2 08:51:39 | 显示全部楼层
是不是while那里出了什么问题?
回复 支持 反对

使用道具 举报

发表于 2013-5-5 15:44:10 | 显示全部楼层
怎么会这样啊

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2013-5-7 11:16:26 | 显示全部楼层
这个看不明白,你是怎么用的,我认为删了就行
回复 支持 反对

使用道具 举报

发表于 2013-7-15 22:13:05 | 显示全部楼层
yimenwang 发表于 2013-5-5 15:44
怎么会这样啊

楼主的代码中,没有定义resetPIN,所以报错。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-6 17:27 , Processed in 0.076767 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表