ARDUINO W5100 WebClient 測(cè)試 基礎(chǔ)工作:W5100擴(kuò)展板插在ARDUINO上。用網(wǎng)線(xiàn)把W5100和自己家的路由器連接。插上網(wǎng)線(xiàn)能看到側(cè)面網(wǎng)口指示燈變亮。路由器開(kāi)啟DHCP服務(wù)(一般都是開(kāi)啟的)。 1.打開(kāi)官方例程里面的Ethernet->WebClient 2.修改里面的谷歌服務(wù)器為百度的。 3.修改IP地址為本地的局域網(wǎng)號(hào)碼段,比如你的電腦是192.168.1.100。那么設(shè)置你的w5100,也在192.168.1.x。后面的x不能與局域網(wǎng)內(nèi)的其它設(shè)備重復(fù)。 - /*
- 【Arduino】168種傳感器模塊系列實(shí)驗(yàn)(資料+代碼+圖形+仿真)
- 實(shí)驗(yàn)一百四十四:Ethernet W5100S 網(wǎng)絡(luò)擴(kuò)展板 SD卡擴(kuò)展模塊 支持MEGA
- 安裝 "Ethernet.h"庫(kù)-工具-管理庫(kù)-搜索-安裝
- 項(xiàng)目測(cè)試之二 :ARDUINO W5100 WebClient 測(cè)試
- */
- #include <SPI.h>
- #include <Ethernet.h>
- // Enter a MAC address for your controller below.
- // Newer Ethernet shields have a MAC address printed on a sticker on the shield
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- // if you don't want to use DNS (and reduce your sketch size)
- // use the numeric IP instead of the name for the server:
- //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
- char server[] = "www.baidu.com"; // name address for Google (using DNS)
- // Set the static IP address to use if the DHCP fails to assign
- IPAddress ip(192, 168, 1, 177);
- // Initialize the Ethernet client library
- // with the IP address and port of the server
- // that you want to connect to (port 80 is default for HTTP):
- EthernetClient client;
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // start the Ethernet connection:
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
- // no point in carrying on, so do nothing forevermore:
- // try to congifure using IP address instead of DHCP:
- Ethernet.begin(mac, ip);
- }
- // give the Ethernet shield a second to initialize:
- delay(1000);
- Serial.println("connecting...");
- // if you get a connection, report back via serial:
- if (client.connect(server, 80)) {
- Serial.println("connected");
- // Make a HTTP request:
- client.println("GET /search?q=arduino HTTP/1.1");
- client.println("Host: www.baidu.com");
- client.println("Connection: close");
- client.println();
- }
- else {
- // kf you didn't get a connection to the server:
- Serial.println("connection failed");
- }
- }
- void loop()
- {
- // if there are incoming bytes available
- // from the server, read them and print them:
- if (client.available()) {
- char c = client.read();
- Serial.print(c);
- }
- // if the server's disconnected, stop the client:
- if (!client.connected()) {
- Serial.println();
- Serial.println("disconnecting.");
- client.stop();
- // do nothing forevermore:
- while (true);
- }
- }
復(fù)制代碼
|