I am trying to configure Ethernet using below sketch
#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[] = {
0x4,0x2,0x45,0x68,0x5E,0x96,0x56
};
// 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);
delay(5000);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
Serial.println("trying to configure ethernet...");
while(Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP, retrying after 5 sec");
// no point in carrying on, so do nothing forevermore:
delay(5000);
Serial.println("trying to configure ethernet...");
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
Its generates below output in the Serial Monitor
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
Failed to configure Ethernet using DHCP, retrying after 5 sec
trying to configure ethernet...
I have captured the LED indicators on video on youtube, it shows Tx and Rx LEDs glowing priodically. Please find link below
"
"
Looks like Adruino not able to connect to Ethernet...
I am using Arduino IDE 1.6.5 on Windows 7.
Is there any debug tool for Arduino that can be used to figure out the issue?
Try this test sketch. It checks the SPI bus and SPI side of the w5100. If it displays 192.168.0.2, then that part is working. If it displays anything else, it is failing.
@ SurferTim : It has printed the ip address as expected :).
Starting w5100
192.168.0.2
I have added further code and tried connecting to google using below code
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,2);
IPAddress server(74,125,232,128);
// 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() {
Serial.begin(9600);
// disable SD card if one in the slot
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.println("Starting w5100");
Ethernet.begin(mac,ip);
Serial.println(Ethernet.localIP());
Serial.println("Connecting to google....");
int ret;
if (ret = client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println(ret);
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
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();
// do nothing forevermore:
while(true);
}
}
Produced below output
Starting w5100
192.168.0.2
Connecting to google....
connection failed
0
disconnecting.
So it could assign the ip address but failing to go online.
I printed out the gateway and subnet mask, it turns out to be 192.168.1.1. So I changed the ip address to 192.168.1.10. This ip is not used by any other device. I also tried assigning dns server but no change in output (Although adding DNS server does not matter at this point). Also I noticed that I could not even ping from my local desktop to "192.168.1.10". Although both are in the same Local Area Network.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,10);
IPAddress dns_server(8,8,8,8);
IPAddress server(74,125,232,128);
// 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() {
Serial.begin(9600);
// disable SD card if one in the slot
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.println("Starting w5100");
Ethernet.begin(mac,ip,dns_server);
Serial.print("ip-");
Serial.println( Ethernet.localIP());
Serial.print("Subnet mask-");
Serial.println( Ethernet.subnetMask());
Serial.print("Gateway-");
Serial.println( Ethernet.gatewayIP());
Serial.print("DNS-");
Serial.println( Ethernet.dnsServerIP());
Serial.println("Connecting to google....");
int ret;
if (ret = client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println(ret);
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
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();
// do nothing forevermore:
while(true);
}
}
I think its picking up the subnet mask and gateway from router but maybe its following some different protocol to communicate with router? Let me know if I should provide the video with more details? or any other way to debug
The ethernet library generates the gateway, and dns server from your specified IP. If you do not specify the netmask, the default netmask is 255.255.255.0.
With your arduino connected to your router, try the below code unmodified to see if you can get a response from the dyndns 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.0"); //download text
client.println("Host: checkip.dyndns.com");
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 finally got things working, below are my findings
1.Directly connecting router to Arduino has no success if you are manually setting the IP address, BUT when you use "Ethernet.begin(mac)" to obtain ip address by directly connecting Arduino to router( and not through network Switch) everything works perfectly. Earlier when I was using "Ethernet.begin(mac)" I was connecting through network Switch.
So after connecting directly to router + using Ethernet.begin(mac) to obtain IP address dynamically things went smoothly.
Thanks SurferTim and zoomkat for your help and time.
Below is the final working code
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,9);
IPAddress dns_server(8,8,8,8);
IPAddress server(74,125,232,128);
// 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() {
Serial.begin(9600);
// disable SD card if one in the slot
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.println("Obtaining DHCP config...");
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.print("ip-");
Serial.println( Ethernet.localIP());
Serial.print("Subnet mask-");
Serial.println( Ethernet.subnetMask());
Serial.print("Gateway-");
Serial.println( Ethernet.gatewayIP());
Serial.print("DNS-");
Serial.println( Ethernet.dnsServerIP());
Serial.println("Connecting to google....");
int ret;
if (ret = client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println(ret);
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
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();
// do nothing forevermore:
while(true);
}
}