實驗內容:很多朋友都有這樣的想法,能不能通過網頁,直接從任何一台計算機,控制和訪問自己的單片機或者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,和如何添加設備,請移步 快速入門 來開始吧。
第一步: 註冊之後,增加一個開關類的傳感器
第二步,獲取這次插入的控制設備的設備號和傳感器號:如下圖來說,就是設備號=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 “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
感謝閱讀!
更多信息與我們交流:
WIZnet郵箱: [email protected]
WIZnet中文主頁:http://www.iwiznet.cn
WIZnet企業微博:http://e.weibo.com/wiznet2012