/*
Web client
This sketch connects to a website ([Link]
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#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[] = "[Link]"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
// 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;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// You can use [Link](pin) to configure the CS pin
//[Link](10); // Most Arduino shields
//[Link](5); // MKR ETH shield
//[Link](0); // Teensy 2.0
//[Link](20); // Teensy++ 2.0
//[Link](15); // ESP8266 with Adafruit Featherwing Ethernet
//[Link](33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
[Link](9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
[Link]("Initialize Ethernet with DHCP:");
if ([Link](mac) == 0) {
[Link]("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if ([Link]() == EthernetNoHardware) {
[Link]("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
if ([Link]() == LinkOFF) {
[Link]("Ethernet cable is not connected.");
// try to congifure using IP address instead of DHCP:
[Link](mac, ip, myDns);
} else {
[Link](" DHCP assigned IP ");
[Link]([Link]());
// give the Ethernet shield a second to initialize:
delay(1000);
[Link]("connecting to ");
[Link](server);
[Link]("...");
// if you get a connection, report back via serial:
if ([Link](server, 80)) {
[Link]("connected to ");
[Link]([Link]());
// Make a HTTP request:
[Link]("GET /search?q=arduino HTTP/1.1");
[Link]("Host: [Link]");
[Link]("Connection: close");
[Link]();
} else {
// if you didn't get a connection to the server:
[Link]("connection failed");
beginMicros = micros();
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = [Link]();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
[Link](buffer, len);
if (printWebData) {
[Link](buffer, len); // show in the serial monitor (slows some boards)
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (![Link]()) {
endMicros = micros();
[Link]();
[Link]("disconnecting.");
[Link]();
[Link]("Received ");
[Link](byteCount);
[Link](" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
[Link](seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
[Link](", rate = ");
[Link](rate);
[Link](" kbytes/second");
[Link]();
// do nothing forevermore:
while (true) {
delay(1);