热度 13
2013-5-23 15:39
992 次阅读|
0 个评论
Arduino以太网插板让你轻松将你的Arduino连接因特网。这个插板可以让Arduino发送和接收来自世界任何角落的数据。你可以用它来做有意思的东西,比如用网站远程控制机器人,或者每次你收到一个新的twitter信息都会响一次铃。这个插板开启了无穷尽的可能性,让你立刻将你的项目介入因特网。 第一步:安装 安装很简单,将插板头部的引脚针插入你的Arduino。 第二步:插板特征 以太网插板基于W5100芯片(WIZnet),带有一个16K的内部缓冲区。连接速率高达10/100Mb。 依赖于Arduino以太网库,和开发环境**。 还有一个板载微型SD卡槽,可以让你存储可查找到的数据。这需要使用外部SD库,它并不附带软件。本教程不涵盖SD卡。在无线SD卡的 Step 8中可以找到。 这个板子也有空间增加PoE模块,它可以给Arduino连接以太网供电。 完整的技术概述,请看官方以太网插板页。 第三步:启动 将Arduino与你电脑USB口连接;以太网插板连接路由器(或直接联网) 接下来,打开Arduino开发环境。我强烈推荐更新Arduino 1.0及以上版本(如果你还没有用过)。这个软件版本支持DHCP,不需要手动配置一个IP地址 要清楚分配到你板子上的IP地址是多少,打开DhcpAddressPrinter: File -- Examples -- Ethernet -- DhcpAddressPrinter 打开后,你可能需要换个MAC地址。在较新的以太网插板版本,你应该看到板子上贴了个地址标签。如果你弄丢了这个标签,就编个能工作的唯一地址。如果您使用多个插板,要保证MAC地址的唯一性。 MAC地址配置好后,上传代码到你的Arduino,打开串口监控器。它会打出使用中的IP地址。 第四步:服务器 你可以将Arduino插板用作一个网络服务器,来负载一个HTML页或者聊天服务器功能。你也可以解析请求客户端发送,就像一个网络浏览器。下面的两个例子说明了怎样使用它来负载HTML页,和解析URL字符串。 重要的是要记住,你需要输入你的Arduino IP地址在下面两个例子中,这样才能工作。 下面的代码将网页服务改换到基于一个按钮: /* Web Server Demo thrown together by Randy Sarafan A simple web server that changes the page that is served, triggered by a button press. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Connect a button between Pin D2 and 5V * Connect a 10K resistor between Pin D2 and ground Based almost entirely upon Web Server by Tom Igoe and David Mellis Edit history: created 18 Dec 2009 by David A. Mellis modified 4 Sep 2010 by Tom Igoe */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 }; IPAddress ip(191,11,1,1); // ENTER YOUR IP ADDRESS HERE!!! // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { pinMode(2, OUTPUT); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.begin(9600); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // if you've gotten to the end of the line (received a newline character) and the line is blank, the http request has ended, // so you can send a reply //reads URL string from $ to first blank space if(incoming c == ' '){ incoming = 0; } if(c == '$'){ incoming = 1; } //Checks for the URL string $1 or $2 if(incoming == 1){ Serial.println(c); if(c == '1'){ Serial.println("ON"); digitalWrite(2, HIGH); } if(c == '2'){ Serial.println("OFF"); digitalWrite(2, LOW); } } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); } } 将正极引导LED连接到引脚D2,负极连接220欧姆电阻到地。 打开LED键入这个到你的浏览器: http:// /$1 关闭LED键入这个到你的浏览器: http:// /$2 注意:很明显你应该用你的IP地址替换 第五步:客户端 你也可以使用以太网插板作为一个客户端。换言说,你可以用它像个网页浏览器一个读取网页。 网页也有可见和隐藏的文本,这样使其在客户端编程变得非常棘手。读网页信息通常涉及到解析很多字符串。这很让人受不了,但是值得的是,如果这正是你想要的。 我写一些读Twitter信息的代码,但这代码已经作为Arduino编辑器的例子存在了。相反,我只需要稍微修改一下,就可以在信息被读的时候 点亮一个LED灯。 连接正极引导LED到引脚D2,连接负极引导220欧姆电阻接地。 不要忘记键入你的IP地址到下面的代码,不然它不会工作的。 代码如下: /* 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. The example uses the DHCP routines in the Ethernet library which is part of the Arduino core from version 1.0 beta 1 The 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 This code is in the public domain. */ #include #include // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac = "api.twitter.com"; // twitter URL boolean requested; // whether you've made a request since connecting long lastAttemptTime = 0; // last time you connected to the server, in milliseconds 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 void setup() { pinMode(2, OUTPUT); // reserve space for the strings: currentLine.reserve(256); tweet.reserve(150); // 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(); } void loop() { if (client.connected()) { if (client.available()) { // read incoming bytes: char inChar = client.read(); // 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 == "Hello Cruel World"){ digitalWrite(2, HIGH); Serial.println("LED ON!"); } if(tweet != "Hello Cruel World"){ digitalWrite(2, 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(); } } 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=RandyMcTestercount=1 HTTP/1.1"); client.println("HOST: api.twitter.com"); client.println(); } // note the time of this connect attempt: lastAttemptTime = millis(); } 也许你想读一些其他最近的帖子在 RandyMcTester Twitter feed 阅读其他Twitter feed, 改变下面的文本: client.println("GET /1/statuses/user_timeline.xml?screen_name= count=1 HTTP/1.1"); 翻译自:http://www.instructables.com/id/Arduino-Ethernet-Shield-Tutorial/ 感谢您的阅读! 与我们更多交流: WIZnet邮箱: wiznet@wiznettechnolog.com WIZnet中文主页: http://www.iwiznet.cn WIZnet中文博客:http://blog.iwiznet.cn WIZnet企业微博:http://e.weibo.com/wiznet2012