< >
Home » Arduino库教程 » Arduino库教程-Ethernet-Web Client Repeating

Arduino库教程-Ethernet-Web Client Repeating

Web Client Repeating(网页客户端重复动作)

  • 这个例子说明了如何用以太网shield重复发出HTTP请求。这个例子通过分配以太网客户端的MAC地址、IP地址、DNS地址,来使用DNS。它连接到http://www.arduino.cc/latest.txt。页面的内容可以通过你的Arduino的串口窗口观察。

硬件要求

  • Arduino 或者 Genuino 开发板
  • Arduino Ethernet Shield

电路

  • 以太网shield可以让你通过SPI总线连接一个 Wiznet 以太网控制器到Arduino或者genuino开发板板。它使用SPI总线连接的引脚pin 10,11,12,和13,到Wiznet。以太网shield后来的模块也有一个SD卡在板上。数字引脚 pin 4 用来控制SD卡上的从选择引脚(slave select pin)。

  • shield应该连接到一个有以太网电缆的网络。您将需要更改程序中的网络设置来对应于您的网络。
    请输入图片描述
    图由 Fritzing 软件绘制
    在上面的图片里,Arduino或genuino开发板会堆叠在以太网shield下面。

原理图
请输入图片描述

样例代码

/*
  Repeating Web client

 This sketch connects to a a web server and makes a request
 using a Wiznet Ethernet shield. 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 DNS, by assigning the Ethernet client with a MAC address,
 IP address, and DNS address.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 19 Apr 2012
 by Tom Igoe
 modified 21 Jan 2014
 by Federico Vanzati

 http://www.arduino.cc/en/Tutorial/WebClientRepeating
 This code is in the public domain.

 */

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

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192, 168, 1, 177);

// fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // give the ethernet module time to boot up:
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns);
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

[Get Code]
更多

  • Arduino Ethernet Shield – 产品描述。
  • Getting started with the Ethernet Shield – 在几分钟内启动所有东西。
  • Ethernet library – 以太网库的参考手册
  • AdvancedChatServer - 一个服务器,用来发送所有传入的信息到所有连接的客户端(除了那个发送信息的客户端)。
  • ChatServer - 一个简单的服务器,用来发送所有传入的信息到所有连接的客户端。
  • WebClient – 查询网络,并通过串口监视器得到答案
  • WebClientRepeating - 如何用以太网shield重复HTTP请求。
  • WebServer - 一个简单的Web服务器,用来显示模拟输入的值。
  • DhcpAddressPrinter – 获取DHCP地址,并打印出到串口监视器。
  • DhcpChatServer – 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。用DHCP。
  • TelnetClient - 连接到一个telnet服务器,并打印所有收到的信息到串口监视器上。
  • BarometricPressureWebServer – 用SPI从压力传感器读取的Post数据。
  • UDPSendReceiveString - 通过UDP协议(通用数据包)发送和接收文本字符串。
  • UdpNtpClient - 查询一个网络时间协议(NTP)服务器,并通过串口服务器监视器获取这个信息。

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: arduino库教程, arduino web client repeating