当前位置: 首页 > news >正文

ARDUINO W5100 WebClient 测试

基础工作:W5100扩展板插在ARDUINO上。用网线把W5100和自己家的路由器连接。插上网线能看到侧面网口指示灯变亮。路由器开启DHCP服务(一般都是开启的)。

1.打开官方例程里面的Ethernet->WebClient

2.修改里面的谷歌服务器为百度的。

3.修改IP地址为本地的局域网号码段,比如你的电脑是192.168.1.100。那么设置你的w5100,也在192.168.1.x。后面的x不能与局域网内的其它设备重复。

4.下载代码,看效果。

 1 /*
 2   Web client
 3  
 4  This sketch connects to a website (http://www.google.com)
 5  using an Arduino Wiznet Ethernet shield. 
 6  
 7  Circuit:
 8  * Ethernet shield attached to pins 10, 11, 12, 13
 9  
10  created 18 Dec 2009
11  by David A. Mellis
12  modified 9 Apr 2012
13  by Tom Igoe, based on work by Adrian McEwen
14  
15  */
16 
17 #include <SPI.h>
18 #include <Ethernet.h>
19 
20 // Enter a MAC address for your controller below.
21 // Newer Ethernet shields have a MAC address printed on a sticker on the shield
22 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
23 // if you don't want to use DNS (and reduce your sketch size)
24 // use the numeric IP instead of the name for the server:
25 //IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
26 char server[] = "www.baidu.com";    // name address for Google (using DNS)
27 
28 // Set the static IP address to use if the DHCP fails to assign
29 IPAddress ip(192,168,1,177);
30 
31 // Initialize the Ethernet client library
32 // with the IP address and port of the server 
33 // that you want to connect to (port 80 is default for HTTP):
34 EthernetClient client;
35 
36 void setup() {
37  // Open serial communications and wait for port to open:
38   Serial.begin(9600);
39    while (!Serial) {
40     ; // wait for serial port to connect. Needed for Leonardo only
41   }
42 
43   // start the Ethernet connection:
44   if (Ethernet.begin(mac) == 0) {
45     Serial.println("Failed to configure Ethernet using DHCP");
46     // no point in carrying on, so do nothing forevermore:
47     // try to congifure using IP address instead of DHCP:
48     Ethernet.begin(mac, ip);
49   }
50   // give the Ethernet shield a second to initialize:
51   delay(1000);
52   Serial.println("connecting...");
53 
54   // if you get a connection, report back via serial:
55   if (client.connect(server, 80)) {
56     Serial.println("connected");
57     // Make a HTTP request:
58     client.println("GET /search?q=arduino HTTP/1.1");
59     client.println("Host: www.baidu.com");
60     client.println("Connection: close");
61     client.println();
62   } 
63   else {
64     // kf you didn't get a connection to the server:
65     Serial.println("connection failed");
66   }
67 }
68 
69 void loop()
70 {
71   // if there are incoming bytes available 
72   // from the server, read them and print them:
73   if (client.available()) {
74     char c = client.read();
75     Serial.print(c);
76   }
77 
78   // if the server's disconnected, stop the client:
79   if (!client.connected()) {
80     Serial.println();
81     Serial.println("disconnecting.");
82     client.stop();
83 
84     // do nothing forevermore:
85     while(true);
86   }
87 }

能显示服务器返回的数据,证明通讯成功。

 

转载于:https://www.cnblogs.com/Mysterious/p/5415777.html

相关文章:

  • iOS9横竖屏设置的处理方法
  • spark 性能优化
  • 第一阶段冲刺个人博客08
  • 【代码笔记】iOS-轮询弹出框
  • 圆角矩形“RoundRectShape”使用详解
  • Javascript异步编程的4种方法
  • [APIO2015]巴厘岛的雕塑
  • HTTP原理
  • javascript中利用柯里化函数实现bind方法
  • theano和keras使用过程中遇到的一些问题记录
  • 20145228《Java程序设计》第九周学习总结
  • Atitit.redis操作总结
  • Java数组技巧攻略
  • Java编程思想读书笔记之内部类
  • 2、变量var关键字
  • JavaScript-如何实现克隆(clone)函数
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • CSS选择器——伪元素选择器之处理父元素高度及外边距溢出
  • idea + plantuml 画流程图
  • Java Agent 学习笔记
  • java中的hashCode
  • leetcode-27. Remove Element
  • mockjs让前端开发独立于后端
  • nginx 配置多 域名 + 多 https
  • React16时代,该用什么姿势写 React ?
  • SegmentFault 2015 Top Rank
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • 对超线程几个不同角度的解释
  • 理清楚Vue的结构
  • 聊聊hikari连接池的leakDetectionThreshold
  • 前端存储 - localStorage
  • 设计模式走一遍---观察者模式
  • 小而合理的前端理论:rscss和rsjs
  • 延迟脚本的方式
  • 用简单代码看卷积组块发展
  • ​油烟净化器电源安全,保障健康餐饮生活
  • # Maven错误Error executing Maven
  • $.proxy和$.extend
  • (6)添加vue-cookie
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (南京观海微电子)——COF介绍
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (译)计算距离、方位和更多经纬度之间的点
  • (原)Matlab的svmtrain和svmclassify
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler
  • .Net 知识杂记
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件