2

we are using Arduino W5100 Ethernet shield. The code is to try DHCP first and if that fails, then try the static IP. we do the Ethernet Initialization and stop every time. The library is Arduino Ethernet library.

  1. when we try with a wireless router, everything works and we are able to contact our web server.
  2. Now the wireless router is connected to a switch. switch <======> router <=====> W5100

if we try to send data using the switch directly, DHCP fails, whereas a laptop using the same cable works.

Question is, even Arduino Library has DHCP functionality, why should a laptop work where the shield is failing? The network and topology and rules should be same for both the laptop and W5100 shield. Anything we can do to debug this issue more?

Edit1 : - Switch which we are using is L3 in nature.

Below is the code which we are using at present.

 while(1) {
 KDEBUG("wait 10 seconds...\r\n");
 // wait 10 seconds
 for(loop = 10; loop > 0 ; loop--) {
 wait_millis(1000);
 }
 KDEBUG("Try transmitting data on ethernet...\r\n");
 transmit_data();
 }
 return 0 ;
}
void wait_millis(uint16_t num) {
 for(; num > 0 ; num-- ) {
 _delay_ms(1) ;
 }
}
// transmit data using Ethernet
static uint8_t transmit_data() {
 char request[256];
 uint8_t status = 0 ;
 // reset counter
 counter = (counter > 254) ? 0 : counter ;
 memset(&request[0], 0 , sizeof(request));
 memset(&bencode[0], 0 , sizeof(bencode));
 wait_millis(5000);
 // try DHCP
 if (Ethernet.begin(mac_address) == 0) {
 KDEBUG("Failed to configure Ethernet using DHCP \r\n");
 // use static IP. no return code.
 Ethernet.begin(mac_address, ip_address);
 } else {
 KDEBUG(" Got lease from DHCP \r\n");
 }
 // wait for ethernet to initialize:
 wait_millis(5000);
 // we got a connection?
 status = client.connect(server, port) ;
 KDEBUGF("ethernet client status= %d \r\n", status);
 if(!status) {
 goto quit ;
 }
 KDEBUG("connected \n");
 // create an HTTP request
 strcat(request, "GET /api/gl868.php?data=");
 strcat(request,bencode);
 // 12 chars
 strcat(request," HTTP/1.1 \r\n");
 // 25 chars
 strcat(request,"Host: ");
 strcat(request,server);
 strcat(request," \r\n");
 // send GET request
 client.println(request);
 _delay_ms(1);
 client.println() ;
 wait_millis(1000);
 client.stop();
 wait_millis(1000);
 if(client.connected()) {
 client.stop();
 }
 return 1 ;
quit:
 wait_millis(100);
 if(client.connected()) {
 client.stop();
 }
 return 0 ;
}
asked Jan 8, 2016 at 7:21
3
  • What Arduino board? Commented Jan 8, 2016 at 9:28
  • @MikaelPatel - We are using Arduino Mega. Commented Jan 8, 2016 at 9:58
  • I have exactly the same problem with an arduino uno - it works fine plugged in to the router but not when plugged in via a switch (in fairness, a chain of switches) Fixed IP is fine and laptop DHCP is fine, it's just DHCP on the ethercard that is an issue. I suspect that the ethercard.h library has a bug but I'm not good enough to find it. srj0408 did you ever find a solution? Commented Jun 12, 2017 at 6:27

2 Answers 2

1

A certain way to debug this is to use tcpdump to capture network packets from both Arduino and your laptop and check the differences. I would start by looking for DHCPDISCOVER packet that Arduino should be sending.

A simpler way would be to check DHCP server logs to see how the communication looks like from server's perspective. Here, you should be looking for lines which have the MAC address of your Arduino shield. The success of this approach will depend of the verbosity of the DHCP server you're using.

answered Jan 8, 2016 at 8:25
1

If I understand your connection...

  • Device over wifi to router works.
  • Wired, via a switch to router does not?

DHCP is clearly switched on for WLAN, is it switched on for LAN?

Personally I always use fixed IP for such projects, with a default value that can be reset so that I can always find my device. I change the IP (and/or MAC) via a serial monitor.

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
answered Jan 8, 2016 at 10:18
2
  • The poster specifically says that a laptop connected via the same cable works (which I assume means "gets a configuration via DHCP"). Commented Jan 8, 2016 at 11:28
  • Even though i am using a static or fix IP for my ethernet shield, it is unable to send data to server. Of that, our switch is L3 in nature. Commented Jan 8, 2016 at 12:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.