本文是一篇关于地址代码的帖子

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

第一步:安装

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

第二步:插板特征

以太网插板基于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与你电脑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 <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]

第五步:客户端

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

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

我写一些读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/

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

文章结束给大家分享下程序员的一些笑话语录: 问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?  
  答:因为李艳红这种败类,所以我没女友!

--------------------------------- 原创文章 By 地址和代码 ---------------------------------

转载于:https://www.cnblogs.com/xinyuyuanm/archive/2013/05/23/3095711.html

地址代码Arduino以太网插板教程相关推荐

  1. Arduino以太网插板教程

    Arduino以太网插板让你轻松将你的Arduino连接因特网.这个插板可以让Arduino发送和接收来自世界任何角落的数据.你可以用它来做有意思的东西,比如用网站远程控制机器人,或者每次你收到一个新 ...

  2. 用iArduino app+以太网插板实现“iPhone,iPadiPod无线控制Arduino”!

    此教程将会告诉你用iArduino App控制你Arduino板子的全部步骤.为了更好的理解,我们用LED并在iArduino应用的帮助下操作开.关.以此你可以知道如何安装iArduino来无线的控制 ...

  3. 以太网插板W5100——基于Arduino

    以太网插板可以通过网线连接外网,登陆服务器后可以通过分析PHP文件获取所需数据.同时可利用以太网插板将Arduino开发板建立成一个小型服务器. 该代码块为连接网页的代码 #include<st ...

  4. 新手必读:Arduino UNO R3教程,原理图,引脚图,详细介绍

    刚入门的学习Arduino的朋友都会有个疑问Arduino UNO R3是什么?为什么要从Arduino UNO R3开始学起? Arduino概述: Arduino是一个开放源码电子原型平台,拥有灵 ...

  5. Arduino极速入门教程——两篇文章让你会用Arduino(下)

    接上篇关于Arduino基础环境配置.界面介绍和C语言基础,这一篇的内容为具体如何在Arduino中进行编程. 在VSCode上配置Arduino 什么是VSCode VSCode,即Visual S ...

  6. Arduino无线通信– NRF24L01教程

    在本Arduino教程中,我们将学习如何使用NRF24L01收发器模块在两个Arduino板之间进行无线通信.您可以观看以下视频或阅读下面的书面教程. Arduino无线通信– NRF24L01教程 ...

  7. 《Arduino直流电机控制教程》

    <Arduino直流电机控制教程> 在这个Arduino教程中,我们将学习如何使用Arduino控制直流电机.我们来看看控制直流电机的一些基本技术,并通过两个例子,学习如何使用L298N电 ...

  8. Linux中DNS服务器地址查询命令nslookup使用教程

    这篇文章主要介绍了Linux中DNS服务器地址查询命令nslookup使用教程,是Linux服务器运维的必备知识,需要的朋友可以参考下 nslookup 程序是DNS 服务的主要诊断工具,它提供了执行 ...

  9. Github上传代码菜鸟超详细教程

    最近需要将课设代码上传到Github上,之前只是用来fork别人的代码. 这篇文章写得是windows下的使用方法. 第一步:创建Github新账户 第二步:新建仓库 第三部:填写名称,简介(可选), ...

最新文章

  1. ConfigParser MissingSectionHeaderError: File contains no section headers.
  2. go hive skynet_MMORPG游戏服务器技术选型参考-Go语言中文社区
  3. UnisGuard防篡改产品了解
  4. P1232 [NOI2013] 树的计数
  5. python单元测试的应用_单元测试pythongui应用程序的推荐方法是什么?
  6. 开源性能测试工具 - Apache ab 介绍
  7. mysql+'@'%_mysql忘记登录的人:命令拒绝用户”@’%’
  8. 嵌入式通过绑定实现双网卡冗余
  9. BigWorld用到的第三方库
  10. Python数学问题2:求100以内素数之和
  11. windows mobile/wince 大容量存储驱动实现介绍
  12. wuli大excel
  13. 申请苹果开发者帐号傻瓜式教程
  14. windows下tomcat7日志配置
  15. matlab中caitu_tiqu,源码交流=图像处理 车牌号码识别[Tested]
  16. Allegro画不规则形状PCB
  17. 分享几个Vue案例供大家一起学习
  18. idc云计算机房建设标准,IDC机房建设要求
  19. English-人事部翻译资格认证
  20. 找出孤独的一个(IBM面试题)

热门文章

  1. php磁盘,什么是磁盘?
  2. wscript是何物
  3. python批量删除文件中多余的空行
  4. bt分析之bt种子发布---做种(2)
  5. 霍布斯:人对人像狼一样
  6. Hinton机器学习与神经网络课程的第二章学习笔记
  7. 多路复用机制--Redis为什么这么快
  8. 读取EXCEL数据到SAP函数重新封装为ZALSM_EXCEL_TO_INTERNAL_TABLE(解决单元格至多上传50字符 单次至多上传9999行 只能读取单个SHEET)
  9. GetData软件使用--获取曲线图中的数据
  10. 机器学习#假设空间与版本空间