Go Down

Topic: Ethernet Connection fails and returns 0 (Read 1 time) previous topic - next topic

Alexu

Aug 31, 2015, 11:13 am Last Edit: Aug 31, 2015, 11:14 am by Alexu
Hello there
I have a pretty annoying problem. I try to make a connection between my Arduino and a local Webserver (IIS on port 80) running on my laptop. But it always fails.

I catched the return value from ''client.connect(server, 80)'' It is always 0 which isn't even a possible status. I also can't ping the arduino.

I tried a different EthernetSield, different cable and even a different MAC address.

My Arduino is connected to a switch but I don't think this is the problem.

Many thanks to everyone who help me (and maybe others) out of this problem.

SurferTim

A 0 return value is possible. That indicates the connection attempt failed.

You should post your code.

Alexu

Code: [Select]
#include <UIPEthernet.h> // Used for Ethernet
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x31, 0x31 };
// MAC in decimal{ 84, 52, 65, 48, 49, 49 };


char server[] = "192.168.0.184";

IPAddress subnet(255, 255, 255, 0);
IPAddress ip(192, 168, 0, 243);
IPAddress gateway(192, 168, 0, 1);
IPAddress dnsserver(192, 168, 0, 8);

EthernetClient client;
int  interval = 5000; // Wait between dumps
int x = 6;
void setup() {

  Ethernet.begin(mac,ip,dnsserver,gateway,subnet);

  Serial.begin(9600);
  while(!Serial)
  {
  }
  delay(1000);
 
  Serial.println("Arduino Temperaturmessungen per LAN versenden.");
  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  Serial.print("IP Address :         ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask :        ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP : ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP :      ");
  Serial.println(Ethernet.dnsServerIP());
  Serial.println("");

 
}

void loop() {
  Serial.print("connecting to ");
  Serial.println(server);
  delay(1000);
  while(x != 1)
  {
    x = client.connect(server, 80);
    Serial.println("Failed trying again...");
    delay(1000);
    Serial.println(x);
  }
  if (x == 1)
  {
    Serial.println("-> Connected");
    // Make a HTTP request:
    client.print( "GET /add.php?");
    client.print("temp=");
    client.print( "12" );
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  }
 
    else {
    Serial.println("--> connection failed");
    Serial.print("Reason : ");
    Serial.println(x);
    client.stop();
    Serial.println("Disconnecting...");
  }
}


Here is my code

SurferTim

Code: [Select]
#include <UIPEthernet.h> // Used for Ethernet
Is this a enc28j60 ethernet controller?

Alexu

#4
Aug 31, 2015, 02:54 pm Last Edit: Aug 31, 2015, 02:54 pm by Alexu
Its a w5100

SurferTim

#5
Aug 31, 2015, 02:55 pm Last Edit: Aug 31, 2015, 02:55 pm by SurferTim
Its a w5100
Then you are using the wrong library.
Code: [Select]
// change this
#include <UIPEthernet.h> // Used for Ethernet
// to this
#include <Ethernet.h> // Used for Ethernet

Alexu

I get thist error now:
Code: [Select]
arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\Ethernet\src\utility/w5100.h:13:17: fatal error: SPI.h: No such file or directory
 #include <SPI.h>

althrough i never wrote
Code: [Select]
#include <SPI.h>

SurferTim

#7
Aug 31, 2015, 02:59 pm Last Edit: Aug 31, 2015, 03:02 pm by SurferTim
You need to include that header also.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h> // Used for Ethernet


edit: You should change this also. The dns resolution on this "domain" may (and probably will) fail.
Code: [Select]
// change this
char server[] = "192.168.0.184";
// to this
IPAddress server(192,168,0,184);

Alexu

Thank you verry much sir.
It now works with DHCP IP's but not with static IP's but I will get this.  :)

SurferTim

Insure the static settings use a valid dns server. See the edit to my last post. If you use a character array for the server variable rather than IPAddress, the ethernet library will attempt to perform a dns resolution on that "domain", and it may fail.

Alexu

I can't find the reason why it won't work with static IP's.
I give the same value as it would get from DHCP but it still reports 0 as error.

Anyway its a suboptimal solution but if the arduino always keeps in the network it never changes the IP so its a half-static IP. ;)

nsnt

i have a problem connection failed ... i do not know the Ethernet shield mac address . can i use a random mac address instead of original mac address

Go Up