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 192.168.1.1 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?

maimi:
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: 255.255.255.248. 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: 255.255.0.0 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.. :~

maimi:
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 192.168.1.1/24 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: 192.168.1.170
mask: 255.255.255.0
gateway:192.168.1.1
Arduino Ethernet: 192.168.1.177
mask: 255.255.255.0
gateway:192.168.1.1

3G dongle:
local IP: 192.168.1.1
mask: 255.255.255.0

dynamic IP: 10.x.x.x
mask: 255.255.255.248
gateway: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: 192.168.1.170
mask: 255.255.255.0
gateway:192.168.1.1

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: 192.168.43.1, the Android has got: 192.168.43.60
so I gave my laptop ethernet interface an address in the same network, 192.168.43.170.
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 192.168.1.170 assigned, then that is the gateway IP for your Arduino. Normally, you would want 192.168.1.1 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 192.168.1.1, and the subnet mask is 255.255.255.0, then any IP between 192.168.1.0 and 192.168.1.255 is localnet. If the subnet mask is 255.255.0.0, then any IP between 192.168.0.0 and 192.168.255.255 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 192.168.1.170 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[] = "www.google.com";    // name address for Google (using DNS)

EthernetClient client;

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

  // if you get a connection, report back via serial:
  int ret= (client.connect(server, 80));
 if (ret ==1) {
    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\r\n");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    Serial.print(ret,DEC);
  }
}

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);
  }
}

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: