ENC28J60 and UIPEthernet only connects once?

krypttr
Posts: 1
Joined: Sun May 09, 2021 11:41 am

ENC28J60 and UIPEthernet only connects once?

Postby krypttr » Sun May 09, 2021 11:56 am

Hello!

I trying to connect ESP32 to LAN over ethernet using ENC28J60 with UIPEthernet library. I can get ip from DHCP just fine, and the first ever connection works ok. However the next connection doesn't work and simply hangs. What might be the problem?

Code: Select all

#include <UIPEthernet.h>

EthernetClient ethClient;
int maxtry = 0;

uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

void BeginEth(){
    Ethernet.init(5);
    if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    } 
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to configure using IP address instead of DHCP:
    //Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
}

void Test(){
  String url = "someurl";
  String endpoint = "someendpoint";
  String json_body = "AAA";
  
  while (!ethClient.connected() && maxtry < 3) {
        if (ethClient.connect(url.c_str(), 80)) {
            ethClient.print(F("POST "));
            ethClient.print(endpoint);
            ethClient.println(F(" HTTP/1.1"));
            ethClient.print(F("HOST: "));
            ethClient.println(url);
            ethClient.println(F("Content-Type: application/json"));
            ethClient.print(F("Content-Length: "));
            ethClient.println(json_body.length());
            ethClient.println();
            ethClient.print(json_body);
        } else {
            Serial.println("No connect");
            maxtry++;
            delay(1000);
        }
    }
    
    if (!ethClient.connected()) {
        Serial.println("Not connected");
        ethClient.stop();
        return;
    }

    unsigned long timeout = millis();
    while (ethClient.available() == 0) {
        if (millis() - timeout > 5000) {
        ethClient.stop();
        delay(1000);
        Serial.println("Timeout");
        }
    }

    while(ethClient.available())
        Serial.print(ethClient.readStringUntil('\n'));

    Serial.println();
    ethClient.stop();
}

void setup(){
  Serial.begin(115200);
  BeginEth();
  delay(2000);
}

void loop(){
    Serial.println("Sending request");
    Test();
}

Who is online

Users browsing this forum: No registered users and 70 guests