极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: 风生水起

YEELINK+UNO+w5100 控制多路开关

[复制链接]
发表于 2013-4-15 19:17:34 | 显示全部楼层 |阅读模式
本帖最后由 风生水起 于 2013-4-15 19:21 编辑

一直想通过YEELINK+UNO控制家电开关,读了很多张老师的28J60的贴子,都没试验成功,后来发现,W5100价格也降得很低了,于是入了一块W5100,测试了官方的例程,官方例程说是28J60,但是编译不过,但用W5100,编译成功,但只能控制一路开关。
由于没有C语言基础,只能简单改改例程,所以一直没测试成控制多路开关。
在网上找到了一位高手发的 arduino+w5100与yeelink结合实现7路信号远程控制 http://hi.baidu.com/mizuda/item/fdd9c38a8e8237d9d1f8cdf6#
但是这段例程,还有部分代码是超声波测距上传,并且在使用时,发现,只有初始时可以控制,之后的循环无法控制开关。

于是用两天时间,把代码打印出来,一句一句解读,修改,终于测试成功

分享给大家,并请高手指正

/*

Yeelink sensor client power switch example

*/




#include <SPI.h>  //调用通读接口库

#include <Ethernet.h>  //调用网络模块库

#include <Wire.h>  //调用I2C通讯库

#include <math.h>  //调用函数库




byte buff[2];  //定义缓冲区???不是很明白

float cm;    //浮点型变量???不是很明白

char flag =0;  //定义开关循环控制变量

// for yeelink api

#define APIKEY         "xxxxxxxxxxxxxxxxxxxxxx" // 替换你的 API KEY
#define DEVICEID       136 // 替换你的device ID


String SENSORno[]={"3366","3367","3368","3369","3372","3373","3415"}; // 把sensor ID 依次替在这里,程序定义为数组,几个开关就写几个

//for led pin

byte ledPin[]={A0,A1,A2,A3,5,6,7};   //定义输出的针脚

// 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 = 3*1000; // delay between 2 datapoints, 30s

String returnValue = "";

boolean ResponseBegin = false;




void setup() {

for(int i=0;i<7;i++)       //I<7,几个开关,就把7改成几,其实就是循次数

{ pinMode(ledPin,OUTPUT);}


  Wire.begin();

  // start serial port:

  Serial.begin(9600);

  // 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");

  }

}




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);

      digitalWrite( ledPin[flag], HIGH);

     

    }

      else if(returnValue.charAt(returnValue.length() - 1) == '0') {

      Serial.println("turn off the LED");

    //  digitalWrite(7, LOW);

      digitalWrite( ledPin[flag], LOW);

    }

     returnValue = "";

    flag ++;

  }

  

// 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:");




    if (flag<7)  // 7改成开关数量

  {

    //get data from server

    getData();   

  }

  else

{

    //put data to server

//     postData(readData());

    if(flag=7) {flag =0;}   // 7改成开关数量

  }




   // note the time that the connection was made or attempted:

    lastConnectionTime = millis();

  }

  // 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(SENSORno[flag]);

    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();
  lastConnectionTime = millis();
}  



回复

使用道具 举报

发表于 2013-4-15 21:10:09 | 显示全部楼层
{ pinMode(ledPin,OUTPUT);}?

==>{ pinMode(ledPin[i],OUTPUT);}
回复 支持 反对

使用道具 举报

发表于 2013-4-16 11:22:23 | 显示全部楼层
Cool!
回复 支持 反对

使用道具 举报

发表于 2013-6-4 22:29:47 | 显示全部楼层
嘻嘻  这个可以要哦
回复 支持 反对

使用道具 举报

发表于 2013-6-5 10:26:35 | 显示全部楼层
努力赚分ing....
回复 支持 反对

使用道具 举报

发表于 2013-11-21 01:27:50 | 显示全部楼层
执行效率太低了,,,
有木有更快的,更合适的方法???
回复 支持 反对

使用道具 举报

发表于 2014-6-28 17:01:11 | 显示全部楼层
错的地方很多,我重新修改了,下面代码绝对有效:
/*
Yeelink sensor client power switch example
*/

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

byte buff[2];



// for yeelink apiapi.yeelink.net      LED    /v1.0/device/10964/sensor/18179/datapoints
#define APIKEY         "47412e01ab744a131cb8da0e64d7e040" // 此处替换为你自己的API KEY
#define DEVICEID       10964 // 此处替换为你的设备编号

char flag =0;  //定义开关循环控制变量

//#define SENSORID1      19581 // 此处替换为你的传感器编号
//#define SENSORID2      19582

String SENSORID[]={"19581","19582"}; // 把sensor ID 依次替在这里,程序定义为数组,几个开关就写几个

byte ledPin[]={5,6};



// 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 = 3*1000; // delay between 2 datapoints, 30s
String returnValue = "";
boolean ResponseBegin = false;

void setup() {

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
   
for(int i=0;i<2;i++)       //I<7,几个开关,就把7改成几,其实就是循次数
{ pinMode(ledPin[i],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");
  }
}

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(ledPin[flag], HIGH);
      
      
    }
      else if(returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED");
      digitalWrite(ledPin[flag], LOW);
    }
     returnValue = "";
     flag++;
  }
  
  
  // 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();
   
                 
                 if (flag<2)
                {
                  //get data from server
                  getData();   
                }
                else
              {
                  //put data to server

                  if(flag=3) {flag =0;}
                }


   
  }
  
  // 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(SENSORID2);
    //SENSORID[flag]={"19581","19582"};
    client.print(SENSORID[flag]);
    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();
}
回复 支持 反对

使用道具 举报

发表于 2014-6-28 17:03:23 | 显示全部楼层
这个是八路的,执行效率不太高,但是相应时间,在20秒左右。。
/*
Yeelink sensor client power switch example
*/

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

byte buff[2];



// for yeelink apiapi.yeelink.net      LED    /v1.0/device/10964/sensor/18179/datapoints
#define APIKEY         "47412e01ab744a******************" // 此处替换为你自己的API KEY
#define DEVICEID       10964 // 此处替换为你的设备编号

char flag =0;  //定义开关循环控制变量

//#define SENSORID1      19581 // 此处替换为你的传感器编号
//#define SENSORID2      19582

String SENSORID[]={"19581","19582","19583","19584","19585","19586","19587","19587"}; // 把sensor ID 依次替在这里,程序定义为数组,几个开关就写几个

byte ledPin[]={A0,A1,A2,A3,5,6,7,8};



// 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 = 3*1000; // delay between 2 datapoints, 30s
String returnValue = "";
boolean ResponseBegin = false;

void setup() {

// pinMode(5, OUTPUT);
// pinMode(6, OUTPUT);
   
for(int i=0;i<8;i++)       //I<7,几个开关,就把7改成几,其实就是循次数
{ pinMode(ledPin[i],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");
  }
}

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(ledPin[flag], HIGH);
      
      
    }
      else if(returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED");
      digitalWrite(ledPin[flag], LOW);
    }
     returnValue = "";
     flag++;
  }
  
  
  // 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();
   
                 
                 if (flag<8)
                {
                  //get data from server
                  getData();   
                }
                else
              {
                  //put data to server

                  if(flag=8) {flag =0;}
                }


   
  }
  
  // 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(SENSORID2);
    //SENSORID[flag]={"19581","19582"};
    client.print(SENSORID[flag]);
    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();
}
回复 支持 反对

使用道具 举报

发表于 2014-11-19 16:03:39 | 显示全部楼层
确定可以吗?我试了一下都不行
回复 支持 反对

使用道具 举报

发表于 2014-12-11 16:52:11 | 显示全部楼层
口腔(戴志强) 发表于 2014-6-28 17:03
这个是八路的,执行效率不太高,但是相应时间,在20秒左右。。
/*
Yeelink sensor client power switch  ...

这个代码好像真的不行,7个那个只能控制2个多了不行,8个这个无法控制
回复 支持 反对

使用道具 举报

发表于 2014-12-11 17:50:24 | 显示全部楼层
                 if (flag<2)
                {
                  //get data from server
                  getData();   
                }
                else
              {
                  //put data to server

                  if(flag=3) {flag =0;}
                }
回复 支持 反对

使用道具 举报

发表于 2014-12-11 17:50:53 | 显示全部楼层
这个地方也要修改,不过不知为什么
回复 支持 反对

使用道具 举报

发表于 2015-1-22 18:10:22 | 显示全部楼层
测距离的在Yeelink平台怎么设置呢,求解
回复 支持 反对

使用道具 举报

发表于 2015-5-15 11:47:21 | 显示全部楼层
口腔(戴志强) 发表于 2014-6-28 17:03
**** 作者被禁止或删除 内容自动屏蔽 ****

强哥,请问你上述提及的库(比如:math、SPI ,Ethernet ,wire )是在哪下载的?
找论坛找了两天还没找着。
回复 支持 反对

使用道具 举报

发表于 2016-12-25 23:18:41 | 显示全部楼层
话不多说,可以借鉴的借鉴,这个可以实现2个电器的控制,状态变化时的最大反应时间为30s*2(两个电器嘛)
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>

byte buff[2];

// for yeelink api
#define APIKEY         "xxxxxxxxxxxxxxxxxxxxxxx" // 此处替换为你自己的API KEY
#define DEVICEID1_kt_light       353506 // 客厅灯光的设备编号
#define SENSORID1_kt_light      398630 // 客厅灯光的传感器编号
#define DEVICEID2_cf_light       353512 // 厨房灯光的设备编号
#define SENSORID2_cf_light      398636 // 厨房灯光的传感器编号

long int DEVICEID[2]={DEVICEID1_kt_light,DEVICEID2_cf_light};
long int SENSORID[2]={SENSORID1_kt_light,SENSORID2_cf_light};
//String DEVICEID[]={"DEVICEID1_kt_light","DEVICEID2_cf_light"};
//String SENSORID[]={"SENSORID1_kt_light","SENSORID2_cf_light"};
short int b[11]={};//电器开关的标志
short int c[11]={22,23,24,25,32,28,33,30,31,26,27};//电器输出端的引脚,
char i=0;//i用于b[11],c[],DEVICEID[],SENSORID[]计数

// 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 = 3*1000; // delay between 2 datapoints, 30s
String returnValue = "";
boolean ResponseBegin = false;

void setup()
{
  pinMode(22, OUTPUT);//客厅灯光
  pinMode(23, 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");
  }
}

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(c[i], HIGH);
    }
      else if(returnValue.charAt(returnValue.length() - 1) == '0')
      {
      Serial.println("turn off the LED");
      digitalWrite(c[i], LOW);
    }
     returnValue = "";
     i++;
     if(i>=2){i=0;}//不能超出要查询的范围
  }
  // 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[i]);
    client.print("/sensor/");
    client.print(SENSORID[i]);
    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();
}
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 16:38 , Processed in 0.044993 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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