Go Down

Topic: Ethernet Shield Really basic beginner query (Read 984 times) previous topic - next topic

I am new to this and trying to test my cards connectivity i am using a basic sketch as below but Whenever i try to monitor it with the serial monitor it drops the ip connectivity is this normal or am i doing something wrong?

/*
  Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis

*/

#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[] = {  0x90, 0xA2, 0xDA, 0x00, 0x81, 0x30 };
byte ip[] = { 192,168,0,80 };
byte gateway[] = { 192,168,0,98 };
byte subnet[] = { 255, 255, 255, 0 };
IPAddress server(74, 125, 113, 99); // Google

// 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() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  Ethernet.begin(mac,ip,gateway);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  }
  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 = 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:
    for(;;)
      ;
  }
}

SurferTim

I did not evaluate all the code, but this will cause problems.
Code: [Select]
byte gateway[] = { 192,168,0,98 };

// start the Ethernet connection:
  Ethernet.begin(mac,ip,gateway);

If you gateway is 192.168.0.98, then you will have no internet connection. You have the dns server set to the gateway, but if you do not pass the gateway parameter, the gateway by default will be 192.168.0.1.
The parameters for Ethernet.begin are:
Ethernet.begin(mac);
Ethernet.begin(mac,ip);
Ethernet.begin(mac,ip,dns);
Ethernet.begin(mac,ip,dns,gateway);
Ethernet.begin(mac,ip,dns,gateway,subnet);


If you are not using dns, you can set the dns server to the gateway. This should work better.
Code: [Select]
  Ethernet.begin(mac,ip,gateway,gateway,subnet);


#2
Jul 05, 2012, 01:29 pm Last Edit: Jul 05, 2012, 01:31 pm by hyperactivestar Reason: 1
Our gateway is 192.168.0.98 though so this is what i was passing as the gateway address. The board is pingable on the IP address set but when i upload the sketch and try to monitor the code running on the serial monitor the IP connectivity drops out

SurferTim


Our gateway is 192.168.0.98 though so this is what i was passing as the gateway address

No, that was what you were passing as the dns server. That is the third parameter. The fourth parameter is the gateway.

Ah ok i unstand now you have to set a value for each parameter. Ok change that but still no connection I upload the sketch and i get responses from pings to the board. But when i put the serial monitor on the board looses IP connection so the programme fails to connect to the webserver

SurferTim

Have you tried dhcp?
Code: [Select]
 Serial.print("Starting ethernet..");
 if(!Ethernet.begin(mac)) Serial.println("failed");
 else Serial.println("ok");

Do you have a microSD card in the shield slot?
What version IDE are you using?

Go Up