项目描述
此项目通过使用Ethernet Shield及Twitter推送来控制一个情景灯。
步骤1:材料
这个项目用到了很少的材料,有很多是不必要的。你也可以改变灯的外形,这样可能会用到更少材料。
电子零件
Arduino Uno R3(我买了一个SainSmart版本,以削减成本) Arduino Ethernet Shield(我在Radio Shack只花了10美金就搞定了) 4个Led 网线 电源适配器 焊锡
装饰材料
亚克力盒子(我在沃尔玛仓储区找到了一个预制的) 少量约1/2英尺厚的木头 橡胶垫脚 木染料 磨砂玻璃喷漆 泡沫塑料(用来放Arduino) 单乙烯基(做一个可在亚克力上喷漆的模具) 钉子 木胶
需要工具
烙铁 电钻 射钉枪 热胶枪 模切绘图机(用于磨具,不必须有)
步骤2:电子器件构建
受电子知识的限制,我尽量将一切简化。我把led灯直接焊接在了Ethernet Shield上。使用了12和13端口。而前后讲Ethernet Shield插入到Arduino对应引脚上。
步骤3:木头盒子
我构建了一个木头盒子,这样亚克力盒子就能放在里面了。并在一面打了一个洞,如此ethernet 线材及供电线材可以透过来。
步骤4:亚克力盒子
我很幸运找到了一个够透的亚克力盒子,你可以尽可能的简化你的构建。我在与木头盒子相对应的位置钻了一个洞,这样线材就可以轻松的插到灯上。
钻完洞,我用磨砂玻璃喷漆给他罩了一层外衣。一旦射出来盒子里的图形光可见。图像就会清晰的看到。
步骤5:泡沫塑料盒子
我用了一个小薄片的泡沫,并把亚克力盒子的开口按进了泡沫。而后在泡沫中留下一个印记,随后就开始切割。根据泡沫上的印记,讲Arduino放到里面,就构建出镶嵌物的位置了。
步骤6:Arduino代码
以下代码是用于Nike Restock 闹钟的代码。你需要更改代码中的ip地址及mac地址。
<p>/*<br> Twitter Client with Strings
This sketch connects to Twitter using an Ethernet shield. It parses the XML
returned, and looks for this is a tweet
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
either one will work, as long as it's got a Wiznet Ethernet module on board.
This example uses the DHCP routines in the Ethernet library which is part of the
Arduino core from version 1.0 beta 1
This example uses the String library, which is part of the Arduino core from
version 0019.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
by Tom Igoe
modified by Amanda Ghassaei June 2012
http://www.instructables.com/id/Twitter-Controlled-Pet-Feeder/
modified again by cdw181818 December 2014
This code is in the public domain.
*/
#include
#include </p><p>//variable to prevent overfeeding
boolean nikeRestock = 1;</p><p>// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip(xxx,xxx,xxx,xx; //<<< ENTER YOUR IP ADDRESS HERE</p><p>// initialize the library instance:
EthernetClient client;</p><p>const int requestInterval = 60000; // delay between requests = 1min</p><p>char serverName[] = "api.twitter.com"; // twitter URL</p><p>boolean requested; // whether you've made a request since connecting
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds</p><p>String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet</p><p>void setup() {
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
// reserve space for the strings:
currentLine.reserve(256);
tweet.reserve(150);</p><p>// initialize serial:
Serial.begin(9600);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, ip);
}
// connect to Twitter:
connectToServer();
testing();
}</p><p>void loop()
{
if (nikeRestock){
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();</p><p> // add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with , it will
// be followed by the tweet:
if ( currentLine.endsWith("")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
if(tweet == ">restock"){
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
Serial.println("LED ON!");
delay(360000);//turn on for 1 hour
digitalWrite(12, LOW);
digitalWrite(13, LOW);
nikeRestock = 0;
}
if(tweet != ">restock"){
digitalWrite(12, LOW);
digitalWrite(13, LOW);
Serial.println("LED OFF!");
}
// close the connection to the server:
client.stop();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
else if (millis() - lastAttemptTime > 14400000){//if four hours has passed since last feeding
nikeRestock = 1;
}
}</p><p>void testing(){
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}</p><p>void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(serverName, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=nikestore&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}</p>
步骤7:完成
在给Arduino上传程序之后,我把他放到了泡沫垫中,并插上线材。这样就安装好了一个木头盒子。而后在每个角上点一点热熔胶,并盖上亚克力盒子。
剩下的就是在出口那插入电源适配器,并将网线插到路由上。灯会立即点亮,这样就可以开始设置了。接下来就可以等待restock的信息推送了!