So, I recently got a Mega set up with the ethernet shield, and I'm trying to get the very simple index.html from a site (http://bulbrelay.000webhostapp.com/). Here is the code I am using:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "bulbrelay.000webhostapp.com"; // name address (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;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
Ethernet.begin(mac);
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("GET http://bulbrelay.000webhostapp.com/ HTTP/1.1");
client.println();
} else {
// if 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:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
And I get the standard 400 bad request return from the console. With other sites, such as server[] = go.com and the get as http://go.com/ HTTP/1.1, it works fine! Just not for my site (which has a filler of just one string of text right now).
Thank you guys for taking the time to look over this!