Hi everyone!
I have a project to make the arduino remote switch, so I can turn on/off my server (aka my computer) when I am not at home. I have a HanRun HR911105A Ethernet shield that I use for my project. I was thinking that I have a good code and everything, but when I uploaded my project, I got the "Failed to configure Ethernet using DHCP" error. I need to read a file that i host on 000webhost.com service, so I know is a "on" or "off" word in a file, so I can turn on or off my server using relay, but that is not part of the problem. I want to know is the problem on the 000webhost side, or on my arduino coding side.
These are my project specifications:
I use 000webhost free hosting service
This is the file I need to read: http://spoz.website/control.txt
I use HanRun HR911105A Ethernet shield for arduino
There are no errors on compiling sketch part of arduino uploading proccess
Below ethernet shield is the normal Arduino UNO board.
Here is my source code:
#include <SPI.h>
#include <Ethernet.h>
#include <WString.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, 0x49 };
// 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.spoz.website"; // name address for Google (using DNS)
bool conn=false;
// 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:
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
String readString = String(100);
Serial.println("connecting...");
conn = client.connect(server, 80);
// if you get a connection, report back via serial:
if (conn) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /control.txt HTTP/1.1");
client.println("Host: www.spoz.website");
//client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
if (client.available()) {
char c = 'X';
while (client.available())
{
c = client.read();
readString.concat(c);
Serial.print(c);
}
}
Serial.println(readString);
delay(1000);
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
//while (true);
delay(2000);
}
}
If you know that error is in the code can you please correct my code and send it on the forum, if you can of course. I was searching on the internet, but I couldn't find the answer. I hope you can help me.