Go Down

Topic: Problem On webclient example with arduino and ENC28J60 ethernet (Read 9295 times) previous topic - next topic

moataz

the web server example working correctly with my ethernet but the webclient example not working properly in my library ENC28J6O and it gives me this in serial monitor

connecting

failed to connect

disconnecting

Quote
##include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 42, 42 } ; // am putting here the assigned ip address for arduino
byte server[] = { 192, 168, 42, 1 } ; // here the google ip address

Client client(server, 80) ;

void setup() {
  Ethernet.begin(mac, ip) ;
  Serial.begin(9600) ;
 
  delay(1000) ;
  Serial.println( "connecting..." ) ;
 
  if (client.connect()) {
    Serial.println("connected") ;
    client.println("GET /search?q=arduino HTTP/1.0") ;
    client.println() ;
  }
  else {
    Serial.println( "connection failed" ) ;
  }
}

void loop() {
  if (client.available()) {
    char c = client.read() ;
    Serial.print(c) ;
  }
 
  if (!client.connected()) {
    Serial.println() ;
    Serial.println( "disconnecting." ) ;
    client.stop() ;
    for( ;; )
      ;
  }
}#

pylon

That's not example code from the ENC28J60 library but from the Ethernet Shield library. That's completely different hardware with completely different software philosophies, so don't mix them up. Use the web client example from the ENC28J60 library you use (you didn't provide a link to the library you're using, so I cannot help you further).


PaulS

Quote
byte server[] = { 192, 168, 42, 1 } ; // here the google ip address

Really? You have google running on a server in your local network?

Which device is that IP address ACTUALLY assigned to - on your local area network?

Code: [Select]
    client.println("GET /search?q=arduino HTTP/1.0") ;
Is there a web server running on that device? Is there a search script in the root directory of the web server directory on that device?

moataz

thanks paul
this is not the google ip address in this place in code i changed this ip with the ip of google which is 173.194.70.138

and i tried this sketch and didn't work
Quote
//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
//the arduino lan IP address { 192, 168, 1, 102 } may need
//to be modified modified to work with your router.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 169, 254, 209, 177 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 173, 194, 70, 138 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.begin(9600);
  Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /search?q=arduino. HTTP/1.0"); //download text
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

zoomkat

Quote
and i tried this sketch and didn't work


What do you not understand about the below part of the code?

Code: [Select]
//for use with W5100 based ethernet shields
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

PaulS

Quote
What do you not understand about the below part of the code?

The whole idea behind classes, and encapsulation, is that one can create a new class that has the same interface, but completely different ways of implementing that functionality, and use that new class in place of the old class. The program that uses the class should now know that the implementation of the class changed.

The class that OP is using is not the one designed for the W5100 chip, but it is supposed to provide the same interface and same responses as the one that is designed to work with the W5100 chip.

The comment is a cover-your-ass thing that says only that you have tested the code with the library designed for the W5100 chip and that you know that it works.

There is nothing in the sketch that is dependent on the W5100 chip. It is only dependent an a specific interface that the library that supports the W5100 chip agrees to implement. Other code supporting other chips can implement the same interface, and your code should work.

The fact that it does not, for the poster, is not a problem with your code or his library. It appears to be either a network problem on OPs end, a router configuration issue, or a PEBKAC issue (that's the most likely cause, in my opinion).

SurferTim


The class that OP is using is not the one designed for the W5100 chip, but it is supposed to provide the same interface and same responses as the one that is designed to work with the W5100 chip.

The comment is a cover-your-ass thing that says only that you have tested the code with the library designed for the W5100 chip and that you know that it works.

There is nothing in the sketch that is dependent on the W5100 chip. It is only dependent an a specific interface that the library that supports the W5100 chip agrees to implement. Other code supporting other chips can implement the same interface, and your code should work.

The fact that it does not, for the poster, is not a problem with your code or his library. It appears to be either a network problem on OPs end, a router configuration issue, or a PEBKAC issue (that's the most likely cause, in my opinion).

I agree with the first statement. They should both be able to work with the same function calls. This just means that nobody has debugged the ENC28J60 like we have the W5100. It is the IC sold on the Arduino ethernet shield, so it will probably be the most popular.

I have tested and debugged the W5100/ethernet library as best I can. The remaining tests would be up to someone else, since I do not have a ENC28J60 shield.

PaulS

Quote
This just means that nobody has debugged the ENC28J60 like we have the W5100.

Agreed. I still think that OPs problem has less to with his/her choice of hardware or libraries or sketches and far more to do with not understanding networking and debugging.

SurferTim

#9
Jul 04, 2013, 05:59 pm Last Edit: Jul 04, 2013, 06:26 pm by SurferTim Reason: 1
You may also be correct there, but I can't tell from here. If I was as familiar with, and confident in, the 28J60 library as I am the 5100 library, I could be a bit more positive on that answer.

I don't think zoomkat meant that code did not work on the ENC28J60. He was mentioning only it was tested on a w5100. I think he is in the same boat with me. I don't think he has a 28J60 either.

For the longest time, these two libraries were not interchangeable.

edit: If someone will verify these known-good w5100 sketches work with an ENC28J60, I'll change the header on both pages.
http://playground.arduino.cc/Code/WebClient
http://playground.arduino.cc/Code/WebServerST

tochinet

FWIW, I am a 28J60 owner/user, and I never found any library that is compatible with the Arduino "official" one. Both ENC28J60 librairies I know (Ethershield and Ethercard) are not even called "Ethernet.h", do not use "client" object, etc.

IF there is (finally) some library that emulates the standard API for the ENC28J60, I'd like to know. I extensively researched it for several months, and could never find any. So there is a priori no reason why the code _should_ work.

ntruchsess


IF there is (finally) some library that emulates the standard API for the ENC28J60, I'd like to know

here you go:

https://github.com/ntruchsess/arduino_uip

To run the standard (Ethernet.h) sketches you have to change the include to UIPEthernet.h and remove SPI.h.

- Norbert

Go Up