Arduino以太网插板教程

来源:互联网 发布:最短路径dijkstra算法 编辑:程序博客网 时间:2024/04/28 21:51

Arduino插板1

Arduino以太网插板让你轻松将你的Arduino连接因特网。这个插板可以让Arduino发送和接收来自世界任何角落的数据。你可以用它来做有意思的东西,比如用网站远程控制机器人,或者每次你收到一个新的twitter信息都会响一次铃。这个插板开启了无穷尽的可能性,让你立刻将你的项目介入因特网。

第一步:安装

Arduino插板2

安装很简单,将插板头部的引脚针插入你的Arduino。

第二步:插板特征

Arduino插板3

以太网插板基于W5100芯片(WIZnet),带有一个16K的内部缓冲区。连接速率高达10/100Mb。

依赖于Arduino以太网库,和开发环境捆绑。

还有一个板载微型SD卡槽,可以让你存储可查找到的数据。这需要使用外部SD库,它并不附带软件。本教程不涵盖SD卡。在无线SD卡的 Step 8(http://www.instructables.com/id/Arduino-Wireless-SD-Shield-Tutorial/step8/Prepare-the-SD-card/)中可以找到。

这个板子也有空间增加PoE模块,它可以给Arduino连接以太网供电。

完整的技术概述,请看官方以太网插板页http://arduino.cc/en/Main/ArduinoEthernetShield

第三步:启动

Arduino插板4

Arduino插板5

将Arduino与你电脑USB口连接;以太网插板连接路由器(或直接联网)

接下来,打开Arduino开发环境。我强烈推荐更新Arduino 1.0及以上版本(如果你还没有用过)。这个软件版本支持DHCP,不需要手动配置一个IP地址

要清楚分配到你板子上的IP地址是多少,打开DhcpAddressPrinter:
File --> Examples --> Ethernet --> DhcpAddressPrinter

打开后,你可能需要换个MAC地址。在较新的以太网插板版本,你应该看到板子上贴了个地址标签。如果你弄丢了这个标签,就编个能工作的唯一地址。如果您使用多个插板,要保证MAC地址的唯一性。

MAC地址配置好后,上传代码到你的Arduino,打开串口监控器。它会打出使用中的IP地址。

第四步:服务器

Arduino插板6

Arduino插板7

Arduino插板8Arduino插板9

你可以将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 <SPI.h> #include <Ethernet.h>

// 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);

int buttonPress = 1;

void setup() {   pinMode(2, INPUT);

// start the Ethernet connection and the server:   Ethernet.begin(mac, ip);   server.begin(); }

void loop() {

buttonPress = digitalRead(2);

// 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

if (c == '\n' && currentLineIsBlank) {

// send a standard http response header

client.println("HTTP/1.1 200 OK");

client.println("Content-Type: text/html");

client.println();

//serves a different version of a website depending on whether or not the button

//connected to pin 2 is pressed.

if (buttonPress == 1) {

client.println("<cke:html><cke:body bgcolor=#FFFFFF>LIGHT!</cke:body></cke:html>");

}

else if (buttonPress == 0){

client.println("<cke:html><cke:body bgcolor=#000000 text=#FFFFFF>DARK!  </cke:body></cke:html>");

}

break;

}

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();

}

}

让这个样例代码工作,附上一个一个按钮在 D2引脚和5V之间,一个10K电阻在 D2引脚与接地之间,然后负载你的ArduinoIP地址到你的网页浏览器。网页应该打开一个黑色的背景。按下这个按钮并保持住,然后刷新页面。这页面将会打开一个白色背景。

下面的代码在URL上点亮一个LED,并发送到Arduino:

/*   Web Server Demo   thrown together by Randy Sarafan    Allows you to turn on and off an LED by entering different urls.    To turn it on:  http://your-IP-address/$1    To turn it off:  http://your-IP-address/$2    Circuit:  * Ethernet shield attached to pins 10, 11, 12, 13  * Connect an LED to pin D2 and put it in series with a 220 ohm resistor to 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 <SPI.h> #include <Ethernet.h>

boolean incoming = 0;

// 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://[YOUR IP ADDRESS HERE]/$1

关闭LED键入这个到你的浏览器:

http://[YOUR IP ADDRESS HERE]/$2

注意:很明显你应该用你的IP地址替换[YOUR IP ADDRESS HERE]

第五步:客户端

Arduino插板10

你也可以使用以太网插板作为一个客户端。换言说,你可以用它像个网页浏览器一个读取网页。

网页也有可见和隐藏的文本,这样使其在客户端编程变得非常棘手。读网页信息通常涉及到解析很多字符串。这很让人受不了,但是值得的是,如果这正是你想要的。

我写一些读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 <text>this is a tweet</text>    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    This code is in the public domain.    */

#include <SPI.h> #include <Ethernet.h>

// 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, 0xDE, 0x01 }; IPAddress ip(191,11,1,1);

//<<< ENTER YOUR IP ADDRESS HERE!!!

// initialize the library instance: EthernetClient client;

const int requestInterval = 60000;

// delay between requests

char serverName[] = "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 <text>, it will

// be followed by the tweet:

if ( currentLine.endsWith("<text>")) {

// 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=RandyMcTester&count=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=[NEW TWITTER NAME HERE]&count=1 HTTP/1.1");

翻译自:http://www.instructables.com/id/Arduino-Ethernet-Shield-Tutorial/

 

感谢您的阅读!欢迎与我们进行更多交流~