So I bought an ethernet shield (this one) and wanted to start a server with an arduino uno.
The problem is that the shield does not connect to the network, and I do not know why. When I upload the example sketch DhcpAddressPrinter it starts, writes to the serial monitor, but is unable to connect (or start the server) and writes the fail message; "Failed to configure Ethernet using DHCP".
I also tried to downgrade the IDE to 1.0.6 and upload the example sketch with no luck.
I thought it might have been faulty soldering or some other hardware failure, so a brought it to an kind of expert who works at a college where we did get it to work by only plugging it to their network.
I assume from this that it is a problem with my router, but I do not know what would make the router reject it, if that is the case. There are no extra security on it, at least not that I know of.
The problem is that the shield does not connect to the network, and I do not know why. When I upload the example sketch DhcpAddressPrinter it starts, writes to the serial monitor, but is unable to connect (or start the server) and writes the fail message; "Failed to configure Ethernet using DHCP".
Are you running a DHCP server? If not, you can't expect magic to happen.
Make sure the ethernet shield pins are properly inserted into the proper arduino pin sockets as sometimes they can get misaligned. You can try the below client code unmodified to see if you can get a reply from the server.
//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET / HTTP/1.1"); //download text
client.println("Host: checkip.dyndns.com");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
I tried your code, zoomkat, and it work the first time the sketch was run, but not anytime after that. Then it fails to configure using DHCP. Do you think it is the arduino or the shield or my router?
And sorry about the long delay. This is my first time posting to the forum, and thought I got a message, when the thread updated.
I replaced the previous code with simple server code using "checkip.dyndns.com" as the server. Try this and see if you get a server response more than once. Some times this server seems a little slow, but it is generally responsive.
Okay. First, sorry for the long reply time. Second, I have been around a few different networks, plugging the arduino in, and uploading the code. The sketch (yours, zoomkat) I have had working two or three times. That is, two or three times it have returned the full results of the sketch. A few other times it have been stuck on the "Send an e in serial monitor to test" without reacting to the input, but most times it is just blank, even after 10 minutes or more. Any more idea for fixing it? please?
Another question I have is that a few times, when it have been working, I uploaded some of the example sketches, which sets the arduino as a web server. But how do I enter these as a client from my computer? I assumed I just had to type the local ip (198.162.1.?) in the browser but that does not work. I have looked the website explanations, but I can not find that piece of information.
Thanks,
Lumme
Edit: -
And one more thing; I just uploaded the WebServer example sketch, and after a few resets I got this:
server is at 255.255.255.255
server is at 255.255.255.255
server is at 255.255.255.255
server is at 255.255.255.255
new client
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿclient disconnected
new client
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿclient disconnected
new client
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿclient disconnected
new client
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿclient disconnected
new client
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿclient disconnected
new client
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿclient disconnected
The first time it connected to something, it did it immediately after it was reset, the other times, I did not see it do, because I was writing this. (the ÿ is to my knowlegde the number 255 in ASCII. I think it is also the defualt value of char, maybe?)
And one last thing; is running DHCP, just checked the router to be sure.
I assumed I just had to type the local ip (198.162.1.???) in the browser but that does not work.
http://198.162.1.xxx/ should work, IF your outer is properly configured to forward the port you are using to your Arduino. By default, that is usually not the case.
Simple server test code that should control a pin on the arduino on/off. If the requesting pc browser and arduino are connected to the same router, then the router usually does not need any changes. Note that the URL IP address is preceded by http:// telling what type communication is desired. You can open the serial monitor to see what is being sent from the browser when the control is clicked in the arduino web page.
//zoomkat 10-6-13
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;
//////////////////////
void setup(){
pinMode(4, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("servertest1"); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
}
else {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Zoomkat's simple Arduino button</H1>");
client.println("<a href='/?on1' target='inlineframe'>ON</a>");
client.println("<a href='/?off' target='inlineframe'>OFF</a>");
client.println("<IFRAME name=inlineframe style='display:none'>");
client.println("</IFRAME>");
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("on1") >0)//checks for on
{
digitalWrite(4, HIGH); // set pin 4 high
Serial.println("Led On");
}
if(readString.indexOf("off") >0)//checks for off
{
digitalWrite(4, LOW); // set pin 4 low
Serial.println("Led Off");
}
//clearing string for next read
readString="";
}
}
}
}
}
I tried the sketch out, with no success. I also tried the sketch you, zoomkat, posted before, where I found the a small error, in that I moved the serial.begin() to the top of setup(). This caused it to print "Failed to configure Ethernet using DHCP".
Now, I also looked over my router again, and tried to allow the arduino through as many router rules as I could find. I haz picz;
I hope maybe some of these might help in some way. Oh, and yes, I did remember to align the ip and gateway addresses of both the sketch and the rules.