Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The first Arduino HTTP request
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAB, 0xCB, 0xCD, 0xDE, 0x02 };
IPAddress ip(192,168,0,140);
IPAddress server(173,194,70,103);
EthernetClient client;
void connect(){
if (client.connect(server, 80)) {
Serial.print("Make a HTTP request ... ");
client.println("GET /search?q=arduino HTTP/1.0");
client.println("HOST: google.com");
client.println();
Serial.println("ok");
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void setup() {
Serial.begin(9600);
Serial.print("Setup LAN ... ");
// give the Ethernet shield a second to initialize:
delay(1000);
Ethernet.begin(mac, ip);
Serial.println("ok");
delay(1000);
connect();
}
void loop(){
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();
delay(3000);
connect();
}
}
@mydhitz
Copy link

mydhitz commented May 15, 2018

im try and show

disconnecting.
connection failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment