guozhongWL 发表于 2013-5-17 19:02:32

arduino uno+ethernet 5100和yeelink玩转远程家电控制---疑惑》》》

如下边的帖子:
http://www.geek-workshop.com/thread-1165-1-1.html


按照那个帖子,我把程序改成这样

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>

byte buff;

// for yeelink api
#define APIKEY         "798e1ba4a9a0f********86c9d38" // replace your yeelink api key here
#define DEVICEID       2988 // replace your device ID
#define SENSORID       4215 // replace your sensor ID   

// assign a MAC address for the ethernet controller.
byte mac[] = { 0x00, 0x5D, 0x60, 0x15, 0xDA, 0x8D};
//byte ip[] = { 10, 1,19, 205 };
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net";   // name address for yeelink API
//IPAddress server(42,96,164,52);
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
String returnValue = "";
boolean ResponseBegin = false;

void setup() {
pinMode(7, OUTPUT);

Wire.begin();
// start serial port:
Serial.begin(57600);
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
}
else {
    Serial.println("Ethernet configuration OK");
}
// Ethernet.begin(mac, ip);
}

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 (c == '{')
      ResponseBegin = true;
      else if (c == '}')
      ResponseBegin = false;

      if (ResponseBegin)
      returnValue += c;   
}
if (returnValue.length() !=0 && (ResponseBegin == false))
{
    Serial.println(returnValue);
   
    if (returnValue.charAt(returnValue.length() - 1) == '1') {
      Serial.println("turn on the LED");
      digitalWrite(7, HIGH);

    }
      else if(returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED");
      digitalWrite(7, LOW);
    }
   returnValue = "";
}
// 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();
    Serial.print("yeelink:");
    //get data from server
    getData();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}



// this method makes a HTTP connection to the server and get data back
void getData(void) {
// if there's a successful connection:
if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
   
    client.print("GET /v1.0/device/");
    client.print(DEVICEID);
    client.print("/sensor/");
    client.print(SENSORID);
    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.println("Content-Length: 0");
    client.println("Connection: close");
    client.println();
    Serial.println("print get done.");
   
}
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();
}




问题:
1、串口一直打印:
Ethernet configuration OK   
yeelink:connecting...
print get done.
yeelink:connecting...
print get done.
..................
2、无法使用yeelink平台对led控制;

由于之前的帖子时间太久,就重发了个》》》》
大伙给点建议吧........谢谢了》》》

guozhongWL 发表于 2013-5-18 09:13:26

大家给点意见呗,,,,
坐等》》》》

guozhongWL 发表于 2013-5-18 15:57:54

if (client.available()) {
    char c = client.read();
    Serial.print(c);
      if (c == '{')
      ResponseBegin = true;
      else if (c == '}')
      ResponseBegin = false;

      if (ResponseBegin)
      returnValue += c;   
}
貌似就没执行过........:L 但是 ArduinoEthernet 模块 的RX 指示灯 一直在无规律的闪烁(说明有接收数据...),表示很不理解:dizzy:

guozhongWL 发表于 2013-5-19 11:01:38

:@-----你们这些 无爱 之人******
出现以上问题的原因:
网络不符合要求------》换了一个网络环境,解决了以上的问题;

但是到现在为止,依然不确定我之前使用的网络为何不符合要求.......
测试失败时的网络环境:
1、该网络是 使用 无线路由 搭建的 网络,连接该网络无需密码-----
由于是 校园网,用户必须 使用 自己的 账号+密码 方可使用 网络(有线或无线网络),即 我们使用笔记本 连接 网络 需要 使用自己的账号+密码,不论是有线还是无线,只要是使用学校的网络,必需这样弄;(现在,在我们学校 使用笔记本 作为WIFI 热点来搭建网络都是不允许滴,确切说, 不允许 网络共享......)
2、从路由引出一根网线与 ethernet 模块儿连接;

测试 成功 时的网络环境:
1、找了个 上网 不需要账号 的 网络;
2、从路由引出一根网线与 ethernet 模块儿连接;

就这样,于是 搞定了。。。。。。。;(希望我 描述清楚了.......)

但是,如果 使用我 最开始 的网络环境,我的程序应该如何修改,还是希望大家指点指点,
希望大家不吝赐教啊》》》》
帮我把这个问题彻底解决~~

caodingguang 发表于 2013-5-19 23:10:13

guozhongWL 发表于 2013-5-19 11:01 static/image/common/back.gif
-----你们这些 无爱 之人******
出现以上问题的原因:
网络不符合要求------》换了一个网络环境,解决 ...

楼主分析的很好,之前我一直用的是ENC的那个芯片,也是用的校园网,但是大家都知道校园网用的是锐捷的认证客户端,MAC地址是绑定的,所以估计不行,但是访问内网估计是可以的。

weijinhe 发表于 2014-3-14 12:56:22

MAC 地址怎么设置 老是出现Failed to configure Ethernet using DHCP

dymrkj 发表于 2016-4-10 19:27:16

weijinhe 发表于 2014-3-14 12:56 static/image/common/back.gif
MAC 地址怎么设置 老是出现Failed to configure Ethernet using DHCP

请问怎么解决的?谢谢
页: [1]
查看完整版本: arduino uno+ethernet 5100和yeelink玩转远程家电控制---疑惑》》》