erjiang 发表于 2012-6-13 19:05:55

5分钟用arduino和yeelink玩转远程家电控制吧

本帖最后由 erjiang 于 2012-11-19 15:52 编辑

实验内容:很多朋友都有这样的想法,能不能通过网页,直接从任何一台计算机,控制和访问自己的单片机或者arduino板呢?这个有趣的功能,相信很多的电子爱好者都可能会想,这个功能如果能实现,是不是意味着就能在web页面,直接通过点击按钮,就能够通过互联网完成对arduino板上的资源甚至是挂接到arduino板上的设备的控制。好像听起来有点耳熟?这是不是就是当下很火爆的数字家庭概念吗?是的没错,如果arduino驱动的是继电器或者可控插座,那么,我们就能很容易的在web上控制普通家用电器啦,想象一下,下班之前,在电脑上登陆自己的yeelink账号,然后点击“热水器烧水”,回家就能洗上舒舒服服的热水澡啦!



硬件要求:

Arduino主板
以太网板(考虑到官方W5100以太网板的价格比较贵,这次再介绍一款SPI通信方式的低成本小板,ENC28J60,参加下图模块的模样和与arduino的连接方式进行连接,并且从这个链接获取ENC的网络函数驱动库并安装即可:

http://geek-workshop.com/forum.php?mod=attachment&aid=NDc1M3w4OTExYjg1M3wxMzM5MzM4Mzk1fDgwN3wyMDA%3D

原理介绍:

为了实现远程控制,为简便起见,我们先讲讲如何web遥控arduino UNO板上的LED灯开关。(实用时应该使用继电器控制强电设备)

yeelink平台提供了两种方式,一种是arduino/单片机通过直接socket网络连接的办法,连入平台上,保持和服务器的长连接,这种方法控制的实时性相对较强;另外一种办法是arduino作为客户端,定期的向服务器查询传感器(LED)的当前值,如果我们要改变arduino的状态(如点亮LED),只需改变当前传感器的值(其实是发送HTTP的post命令,更新一下当前的设备状态),则arduino在定时周期到的时候,发出(HTTPget)命令来获取当前LED状态的时候,发现最近的值有变化(从0变为1)的时候,则相应的改变驱动LED的IO口状态,从而实习远程控制,这里注意,在arduino板上,如果是触发性的操作(只操作一次),则可以在get数据并操作好后,直接发送POST改变服务器上吗的传感器状态,保证不会在arduino端重复触发。

首先,照例我们要先申请到yeelink的API-KEY才可以进行:

如何免费获取API-KEY,和如何添加设备,请移步 快速入门 来开始吧。

第一步: 注册之后,增加一个开关类的传感器
http://blog.yeelink.net/wp-content/uploads/2012/06/AddSwitch1.png


第二步,获取这次插入的控制设备的设备号和传感器号:如下图来说,就是设备号=63,传感器号=57
http://blog.yeelink.net/wp-content/uploads/2012/06/SwitchItem1.png

第三步,好了,控制按钮安装完毕,下面,将第七个PIN和GND之间连上电阻和LED灯,下载下面的arduino程序,更改三个地方,就可以通过点击网页上的按钮,进行控制了。(居然这么简单???是的,就是这么简单…下面想想你能怎么玩更爽吧)

arduino程序中需要修改的地方有
http://blog.yeelink.net/wp-content/uploads/2012/06/replaceItem.png

程序中需要改的地方是:
1.APIKEY: 这个需要更换成你自己账号的APIKEY
2.DEVICEID :这个需要换成设备号
3.SENSORID:这个需要换成传感器号

OK,就这些了,5分钟内学会如何做家庭电器控制,你行的!

另外,需要注意一点,下文中的ethernet shield是需要你家中的路由器开启DHCP功能的,如果没有开启,可以参考将
1. 代码中添加 byte ip[] = { 192, 168, 1, 12 };(根据网络环境更改)
2. 将Ethernet.begin(mac) 替换成Ethernet.begin(mac, ip);

从这下载程序YeelinkPowerSwitch

具体的程序在下面/*
Yeelink 网页远程控制Arduino演示代码
1. 使用arduino UNO和 ethernet shield
2.使用数字7管脚网页控制LED灯
*/

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

byte buff;

// for yeelink api
#define APIKEY         "4bb0814c782a000000e2e3c586bc6963" // replace your yeelink api key here
#define DEVICEID       63 // replace your device ID
#define SENSORID       57 // replace your sensor ID

// 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 = 15*1000; // delay between 2 datapoints, 15s
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");
}
}

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


故意 发表于 2012-6-13 20:56:04

支持下 !!!

yyy_zc 发表于 2012-6-13 21:59:22

为什么要用yeelink

Randy 发表于 2012-6-13 22:12:15

好喜欢的帖子,我有时间也做一下实验!

erjiang 发表于 2012-6-13 23:21:15

Randy 发表于 2012-6-13 22:12 static/image/common/back.gif
好喜欢的帖子,我有时间也做一下实验!

谢谢支持,快快行动吧

erjiang 发表于 2012-6-13 23:22:01

yyy_zc 发表于 2012-6-13 21:59 static/image/common/back.gif
为什么要用yeelink

大大的减轻了开发者的工作量,提供了方便的平台,几乎无需开发服务器程序

飞翔的红猪 发表于 2012-6-14 08:59:57

安全问题是如何考虑的呢?

Ansifa 发表于 2012-6-14 09:41:22

:o好东西,但是能确保持久性吗,万一哪天yeelink关闭了咋办。。。

飞翔的红猪 发表于 2012-6-14 09:44:45

本帖最后由 飞翔的红猪 于 2012-6-14 09:46 编辑

Ansifa 发表于 2012-6-14 09:41 static/image/common/back.gif
好东西,但是能确保持久性吗,万一哪天yeelink关闭了咋办。。。

确实有这种担心。。。有大型网站做靠山,或者有成熟社团做的项目才更可靠

迷你强 发表于 2012-6-14 10:03:41

飞翔的红猪 发表于 2012-6-14 09:44 static/image/common/back.gif
确实有这种担心。。。有大型网站做靠山,或者有成熟社团做的项目才更可靠

我觉得担心是多余的,商业化毕竟代表着要收费,大家又不愿意,免费大家又怕人家会倒。。。。。无谓的担心,有的用就用,用的人越多,就越不会出现状况。商业模式就越成熟

erjiang 发表于 2012-6-14 11:11:52

本帖最后由 erjiang 于 2012-6-14 11:12 编辑

飞翔的红猪 发表于 2012-6-14 09:44 static/image/common/back.gif
确实有这种担心。。。有大型网站做靠山,或者有成熟社团做的项目才更可靠

不是成熟社团,是公司化运作,后期会提供短信事件触发等等电信增值服务,那些会赚钱,盈利。
另外,挣钱是靠商业项目的,开发者玩的爽就够了。

erjiang 发表于 2012-6-14 11:15:25

飞翔的红猪 发表于 2012-6-14 08:59 static/image/common/back.gif
安全问题是如何考虑的呢?

考虑的很多,不过,对爱好者和Geeker来说,这方面不用太担心吧。

商业化方面,
支持https的网页访问和控制。
底层数据上传时使用IPSEC和AES加密,保证了数据的安全性,而且商业项目不使用arduino的,是使用私有的硬件系统。

对个人爱好者来说,无需担心太多,至少CSDN犯的错误我们不会犯,呵呵

erjiang 发表于 2012-6-14 11:17:02

Ansifa 发表于 2012-6-14 09:41 static/image/common/back.gif
好东西,但是能确保持久性吗,万一哪天yeelink关闭了咋办。。。

呃,这个问题好残酷,不过目前来看应该管不了,呵呵,目前几个商业在谈,应该能支持几年问题不大,而且,你存的数据,随时可以取回啊。

飞翔的红猪 发表于 2012-6-14 11:32:43

erjiang 发表于 2012-6-14 11:15 static/image/common/back.gif
考虑的很多,不过,对爱好者和Geeker来说,这方面不用太担心吧。

商业化方面,


其实我想说的是远程控制ARDUINO的安全问题。。。

Alexie 发表于 2012-6-14 12:05:42

Mark 今晚试试。
页: [1] 2 3 4 5
查看完整版本: 5分钟用arduino和yeelink玩转远程家电控制吧