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 }
能显示服务器返回的数据,证明通讯成功。
文章对您有帮助,开心可以打赏我,金额随意,欢迎来赏!
需要电子方面开发板/传感器/模块等硬件可以到我的淘宝店逛逛