Arduino + ENC28j60 failed to connect to the global internet

Hello everyone, I can't figure out the seemingly simple situation arduino + ENC28j60 in client mode for parsing the site. I connect everything according to the instructions and a simple sketch from the example does not work for me

#include <Ethernet.h>           //library for ethernet functions
#include <SPI.h>
#include <Client.h>             //library for client functions
byte mac[] =  { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte ip[] = { 192,168,1,25};  //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,1,1};
IPAddress server { 64, 233, 187, 99  }; // Google
            
EthernetClient client;
bool connected = false;                                
void setup(void) {                                     
    Serial.begin(9600);                                
    Serial.println("Initializing Ethernet.");
    delay(1000);
    //Ethernet.begin(mac, ip , gateway , subnet);    //tried this, no working
    Ethernet.begin(mac);    
}
void loop(void) {                                      
    if(!connected)   {                                 
      Serial.println("Not connected");
      if (client.connect(server, 80)) {                
          connected = true;                                              
          Serial.println("Connected");
          client.println("GET /search?q=arduino HTTP/1.0");
          delay(1000);                                            
      }
      else{
        Serial.println("Cannot connect to Server");               
      }
    }  
    else {
      delay(1000);                                              
      while (client.connected() && client.available()) {        
        char c = client.read();                                 
        Serial.print(c);                                        
      }                                                         
      Serial.println();                                         
      client.stop();                                            
      connected = false;                                        
    }
}

The first thought is to check if my bundle works at all, I checked it on this sketch:

#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
void setup () {
   Serial.begin(57600);
   Serial.println(F("n[testDHCP]"));
   Serial.print("MAC: ");
   for (byte i = 0; i < 6; ++i) {
      Serial.print(mymac[i], HEX);
       if (i < 5)
         Serial.print(':');
        }
     Serial.println();
    if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
        Serial.println(F("Failed to access Ethernet controller"));
    Serial.println(F("Setting up DHCP"));
    if (!ether.dhcpSetup())
        Serial.println(F("DHCP failed"));

   ether.printIp("My IP: ", ether.myip);
   ether.printIp("Netmask: ", ether.netmask);
   ether.printIp("GW IP: ", ether.gwip);
   ether.printIp("DNS IP: ", ether.dnsip);
}
void loop () {}

on it I get the following.

n[testDHCP]
MAC: 74:69:69:2D:30:31
Setting up DHCP
My IP: 192.168.1.25
Netmask: 255.255.255.0
GW IP: 192.168.1.1
DNS IP: 192.168.1.1

I also ran a sketch of a local server - I can connect to arduino, but I can not connect arduino to a server on the global Internet, apparently I do not understand something

Ethernet library is for Wiznet chips W5100, W5200 and W5500.
for the ENC28j60 use EthernetENC library. install it in Library Manager.

what Arduino do you use?

Juraj:
what Arduino do you use?

UNO

ololoepololoe:
UNO

ok. use pin 10 for CS or if you use some other pin set it with Ethernet.init()

Here's what I found, this sketch works

#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
static uint32_t timer;

const char website[] PROGMEM = "www.google.com";

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Serial.println(">>>");
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);
  Serial.println("...");
}

void setup () {
  Serial.begin(9600);
  Serial.println(F("\n[webClient]"));

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println(F("Failed to access Ethernet controller"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer) {
    timer = millis() + 5000;
    Serial.println();
    Serial.print("<<< REQ ");
    ether.browseUrl(PSTR("/foo/"), "bar", website, my_callback);
  }
}

that is, I can get the site "www.google.com" but I can’t get the 64.233.187.99

yes but EtherCard library doesn't use the standard Arduino networking classes so you can use it with Ethernet library examples

Juraj:
ok. use pin 10 for CS or if you use some other pin set it with Ethernet.init()

already use this

I took the w5100 shield and it works

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte myserver[] = { 197, 166, 245, 115}; //

EthernetClient client;

void setup() {

  //Ethernet.begin(mac, ip, myDns);
  Ethernet.begin(mac);
  Serial.begin(9600);
  
}

void loop() {

  delay(3000);
 
  if (client.connect(myserver, 8081)) {  
    Serial.println("connected");
    client.println("GET /adress HTTP/1.0"); //GET request
    client.println(); //end of get request

  }
  else {
    Serial.println("connection failed"); 
    Serial.println();
  }

  while (client.connected() && !client.available()) delay(100); 
  while (client.connected() || client.available()) { 

    char webdata = client.read(); 
     // parse webdata

  }

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

}

and i don't know why enc28j60 is not connecting to ip

ololoepololoe:
I took the w5100 shield and it works

and i don't know why enc28j60 is not connecting to ip

the enc28j60 was not on shield so you wired it. how?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.