Ethernet shield can't connect to web

Hello there,

I'm want to send sensor data to Xively. I have a laptop with windows 7, Internet USB dongle, and Arduino Mega 2560 with Ethernet shield W5100..
I tried the Ethernet Server Sketch and it worked fine. The Web Client sketch doesn't work as well as the upload stream sketch to send Data to Xively.

it seems the problem is to connect to the net, either google or xively or anyother website. I tried to add DNS Server IP, but nothing changed.

Can anybody help, please?
Thank you in advance!

If you are using a static network setup, insure all your settings are correct. If you are sure about that, then you should post your code.

I don't know if this is because the dongle and the Arduino are connected direct to one laptop.. When it tries to connect to a website, the LAN connection all fails.. It says no network access. sometimes, it needed about 30 seconds and gained network access as well as internet access again, but still the Ethernet shield returned 0 connection failed, or by Xively returned -1.
I tried multiple possible combinations, but no use :confused:
I don't know where the problem is!!

Is the Ethernet cable connected to the laptop? If so, you need to set up internet connection sharing.

yea of course, I did so.. and the local network shows internet access. but there should be some mistake in the IP settings..
I configured a local network, gave the laptop and the Arduino static IPs. The Dongle has its dynamic IPs, but I assigned it another static one from my local network, which is since it's the router. when I ping this IP request times out.
when I ping the dynamic one, it works..
then the default gateway and DNS servers.. I tried the static one, the dynamic one , even the DNS associated with the dynamic IP, but nothing worked..

Explain the dongle. What does that device do? Is it the wifi client device?

it's a G3 surf stick. does it make a difference?

it's a G3 surf stick. does it make a difference?

I can't tell from here. If the surf stick is your internet connection, and the ip range is 192.168.1.x, and you have a 192.168.1.x localnet on the internet connection sharing device, you have a routing problem.

What ip does the surf stick get if using dhcp?
What ip does the ethernet shield get if using dhcp?
What ip is the ethernet device in the laptop assigned that the Arduino is connected to?

  • well the stick has a dynamic IP from the range: 10.33.x.x with subnet mask: it says that dhcp is not activated, but each time it get a new ip from the range.

  • the 192.168.1.x , I made in order to build a local network of all the devices. the stick gets no local ip alone. I had assigned it to it. (I tried also without assigning).

  • if DHCP on laptop is activated, the Ethernet device of the laptop gets: IPv4 Address: 169.254.162.x, IPv4 Subnet Mask: but with DHCP it loses its network access. it was actually a coincidence that I tried with 192.168.1.x which gave internet access to the local network.

  • with DHCP the Ethernet shield doesn't work. I need to specify an IP.

other thing, my local network is unidentified network (public network). which means I can't view the devices of the network. So not after all sure that Arduino Ethernet is really working.. But that the Web server example has worked, was my proof..

You need to use internet connection sharing. Do a Google search for internet connection sharing. There are plenty of tutorials and videos to help you for both Windows and Mac.

I did it, but even with internet sharing, the local network didn't get internet access until I gave it the address 192.168.1.X..
Which is really weird.. :~

I did it, but even with internet sharing, the local network didn't get internet access until I gave it the address 192.168.1.X..
Which is really weird.. :~

Not really weird. Most ICS apps use for the computer interface that will be sharing the internet interface. You must then use 192.168.1.x/24 for an ip on any device connected to that interface. The ICS app will do a Network Address Translation (NAT) to the internet interface.

Some ICS apps do not start a dhcp server, so you must set the ip and other network settings manually.

I see! well, I'm going to try it again from scratch. If it doesn't work I'll try another kind of Internet connection (WiFi). Thank you for helping :slight_smile:

still not working :confused: local network works, since web server example works. local network has Internet access. So it must be a wrong or missing IP in Arduino code.
local network
Laptop Ethernet:
Arduino Ethernet:

3G dongle:
local IP:

dynamic IP: 10.x.x.x

DNS: 193.189.244.x
or 193.189.244.y

what should I put in Arduino code exactly? I tried already many (even the clearly wrong ones) and nothing worked.. But I might have overseen something :expressionless:

How did the laptop ethernet interface get this IP? Is it connected to a router?

Laptop Ethernet:

Is the Arduino connected to that interface directly with no router or switch between them?

no, there's no router or switch. I gave the addresses to it. and the arduino is connected directly..

well, in the mean time, I tried another thing. I turned my Android phone into a portable Wi-Fi hotspot.
the laptop has gained Internet access. the Wi-Fi Connection has dhcp, Dns, gateway:, the Android has got:
so I gave my laptop ethernet interface an address in the same network,
now forArduino I let it use DHCP. it worked partially, the error comes not any more "0", but "-2". which according to Arduino cookbook means, "no DNS records found.".
by tring the IP Address of google instead, it gave "0"..

If the laptop ethernet interface has assigned, then that is the gateway IP for your Arduino. Normally, you would want assigned to the laptop interface, but that IP should work.

edit: In the event you are not aware how all this ties together, I will explain simply.

The IP assignment sets the localnet IP range in conjunction with the subnet mask.

The subnet mask determines how big the localnet is. If the IP is, and the subnet mask is, then any IP between and is localnet. If the subnet mask is, then any IP between and is localnet.

The Arduino (or any device) will attempt to connect to any destination IP in that IP range directly. If the destination IP is outside that range, the Arduino (or any device) will send the request to the gateway IP. The gateway IP MUST BE in the localnet range or it will not be reachable.

it worked!!!! :smiley: it has really worked!!
Thank you very much SurferTim! I do appreciate your help :slight_smile:

the code now for people who are trying to do the same, with Arduino & Ethernet shield, laptop, and 3G dongle (huawei E1750), since I found the question in net but no answer..
The Dongle has its dynamic addresses. I just added a static IP address in order to integrate it in my local network.

#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192,168,1,177}; //Arduino
byte myDns[] = {192,168,1,1}; //Laptop Ethernet interface, myDns cause the "dns" wasn't recognized
byte gateway[] = {192,168,1,1}; //Laptop Ethernet interface
byte subnet[] = {255,255,255,0};

//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "";    // name address for Google (using DNS)

EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  // start the Ethernet connection:
Ethernet.begin(mac, ip, myDns, gateway, subnet);  // give the Ethernet shield a second to initialize:

  // if you get a connection, report back via serial:
  int ret= (client.connect(server, 80));
 if (ret ==1) {
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Connection: close\r\n");
  else {
    // kf 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:
  if (client.available()) {
    char c =;

  // if the server's disconnected, stop the client:
  if (!client.connected()) {

    // do nothing forevermore:

Thanks for the explanation :slight_smile: I had the basic knowledge of networking, but after 10 years of doing other things in life, I needed to refresh my information!! and the time stress doesn't help.. Now the other good thing, that Xively code has worked as well!

And I'm really loving Arduino and prototyping! after this project, I'll have no stress, then everything will be better!

The stress relief is only temporary. Every device I have dealt with causes new stress.

I had to debug the ethernet library to get it to work. It is now fixed. :slight_smile:

I had to try to debug the wifi library and firmware to get it to work. It is still not fixed. :frowning:

Now I am trying to debug Adafruit's 10DOF IMU library. The pitch and roll were reversed, and the magnetic compass was 90 degrees out. I have the pitch and roll fixed, but the compass is still giving me fits on one roll direction. But I will get it fixed. :wink: