【转】物联网跟我动手做系列教程—第三篇 实验三如何用arduino+ethernet shield与yeelink结合5分钟实现web远程家电控制(代码已更新)

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

 

硬件要求:

Arduino主板 以太网板(参加下图模块的模样和与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在定时周期到的时候,发出(HTTP  get)命令来获取当前LED状态的时候,发现最近的值有变化(从0变为1)的时候,则相应的改变驱动LED的IO口状态,从而实习远程控制,这里注意,在arduino板上,如果是触发性的操作(只操作一次),则可以在get数据并操作好后,直接发送POST改变服务器上吗的传感器状态,保证不会在arduino端重复触发。

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

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

第一步: 注册之后,增加一个开关类的传感器

Yeelink-step1

第二步,获取这次插入的控制设备的设备号和传感器号:如下图来说,就是设备号=63,传感器号=57

Yeelink-step2

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

arduino程序中需要修改的地方有

Yeelink-step3

程序中需要改的地方是: 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[2];

// for yeelink api

#define APIKEY “4bb08000082a070000e2e3c580000000″ //更换 yeelink api key

#define DEVICEID 63 // 更换设备IDreplace your device ID

#define SENSORID 57 // 更换传感器IDreplace your sensor ID

// 分配MAC地址.

byte mac[] = { 0×00, 0x1D, 0×72, 0×82, 0×35, 0x9D};

// 初始化以太网库:

EthernetClient client;

char server[] = “api.yeelink.net”; // 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(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 (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();

}

原文来自Yeelink :http://blog.yeelink.net/?p=94

感谢阅读!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值