|
本帖最后由 erjiang 于 2012-11-19 15:52 编辑
实验内容:很多朋友都有这样的想法,能不能通过网页,直接从任何一台计算机,控制和访问自己的单片机或者arduino板呢?这个有趣的功能,相信很多的电子爱好者都可能会想,这个功能如果能实现,是不是意味着就能在web页面,直接通过点击按钮,就能够通过互联网完成对arduino板上的资源甚至是挂接到arduino板上的设备的控制。好像听起来有点耳熟?这是不是就是当下很火爆的数字家庭概念吗?是的没错,如果arduino驱动的是继电器或者可控插座,那么,我们就能很容易的在web上控制普通家用电器啦,想象一下,下班之前,在电脑上登陆自己的yeelink账号,然后点击“热水器烧水”,回家就能洗上舒舒服服的热水澡啦!
硬件要求:
Arduino主板
以太网板(考虑到官方W5100以太网板的价格比较贵,这次再介绍一款SPI通信方式的低成本小板,ENC28J60,参加下图模块的模样和与arduino的连接方式进行连接,并且从这个链接获取ENC的网络函数驱动库并安装即可:
http://geek-workshop.com/forum.p ... 4Mzk1fDgwN3wyMDA%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,和如何添加设备,请移步 快速入门 来开始吧。
第一步: 注册之后,增加一个开关类的传感器
第二步,获取这次插入的控制设备的设备号和传感器号:如下图来说,就是设备号=63,传感器号=57
第三步,好了,控制按钮安装完毕,下面,将第七个PIN和GND之间连上电阻和LED灯,下载下面的arduino程序,更改三个地方,就可以通过点击网页上的按钮,进行控制了。(居然这么简单???是的,就是这么简单…下面想想你能怎么玩更爽吧)
arduino程序中需要修改的地方有
程序中需要改的地方是:
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 "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();
- }
复制代码 |
|