Program is stuck at Ethernet.begin()

I'm using Arduino UNO and ENC28J60 for ethernet module.

when turn on the arduino, it works somtime but, somtimes is stuck at Ethernet.begin() and not thing happen.

does anyone know about this situation?

#include <UIPEthernet.h>
#include <avr/wdt.h>
#include <NewPing.h>

//define으로 선언하면 메모리에 저장되지 않고 코드에 직접 삽입되므로
//변수를 선언하는 것보다 메모리 사용량이 적다.
#define PORT 5000
#define CHECK_INTERVAL 500
#define SEND_INTERVAL 1000
#define RECV_INTERVAL 2000
#define RECV_MAXCOUNT 3
#define CONNECT_MAXCOUNT 3
#define SONAR_TRIG A0
#define SONAR_ECHO A1
#define SONAR_MAXD 400
#define FIRST_DIGITAL_PIN 2
#define LAST_DIGITAL_PIN 9

enum State {
  CONNECT,
  DISCONNECT,
  SEND_RECV,
  RESET
};

State state = CONNECT;

EthernetClient client;

NewPing sonar(SONAR_TRIG, SONAR_ECHO, SONAR_MAXD);

const IPAddress SERVER_IP(192,168,5,14);

short recvEmptyCount = 0;
short ConnectCount = 0;

unsigned short lastValidReadValue = 0;
unsigned short readValue = 0;

unsigned long prev_time = 0;

bool isSent = false;

void check_distance()
{
  unsigned short distance = sonar.ping_cm();
  if (distance > 4 && distance < 200) 
  {
    Serial.println(distance);
    // 사람 있음
    if(distance < 70) {
      readValue = 1;
      lastValidReadValue = readValue;
    }
    // 사람 없음
    else if(distance > 70) {
      readValue = 0;
      lastValidReadValue = readValue;
    }
  }
  // 오류 값
  else if(distance <= 3 || distance > 200)
  {
    readValue = lastValidReadValue;
  }
}

void send() 
{
  client.print(readValue);
}

//서버에서 메세지 recv 해서 처리하는 함수
void recv() {
  String serverMsg = client.readString();
  Serial.println(serverMsg);
  int ServerMsg_Len = serverMsg.length();
  if(ServerMsg_Len <= 0 )
  {
    //빈 메세지
    recvEmptyCount++;
    Serial.println("rcv-1");
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(A0,INPUT);

  for (int i = 2; i < 10; i++)
  {
    pinMode(i, OUTPUT);
  }

  uint8_t mac [6] = {0xC0, 0xFF, 0xEE, 0xBA, 0x11, 0xEA};
  IPAddress myip(192,168,5,130);

  Ethernet.begin(mac,myip);
  Serial.println("Ethernet begin");
}

void loop() {
  switch(state)
  {
    //연결 시도 케이스
    case CONNECT : 
      //연결 실패 시도가 CONNECT_MAXCOUNT를 넘으면?
      
      if(ConnectCount > CONNECT_MAXCOUNT)
      {
        Serial.println("Rst");
        //아두이노 초기화
        wdt_enable(WDTO_15MS);
      }

      if(client.connect(SERVER_IP,PORT))
      {
        Serial.println("Conn0");
        ConnectCount = 0;
        state = SEND_RECV;
        prev_time = millis();
        break;
      }

      ConnectCount++;
      Serial.println("conn-1");
      break;

    //연결 종료 케이스
    case DISCONNECT : 
      //클라이언트 소켓 닫음
      client.stop();

      //다시 연결 시도 케이스로
      state = CONNECT;
      break;

    //CHECK SEND RECV 로직
    case SEND_RECV :

      if(millis() - prev_time > CHECK_INTERVAL)
      {
        check_distance();
      }

      //SEND
      if(!isSent && millis() - prev_time > SEND_INTERVAL)
      {
        send();
        isSent = true;
      }

      //RECV
      if(millis() - prev_time > RECV_INTERVAL)
      {
        prev_time = millis();
        recv();
        isSent = false;
      }
      //recvEmptyCount가 RECV_MAXCOUNT보다 많다면
      //연결이 끊긴것으로 가정하고 연결 해제
      if(recvEmptyCount > RECV_MAXCOUNT)
      {
        state = DISCONNECT;
        break;
      }
      break;
  }
}

Have you tried your lines as the Arduino reference shows them (type byte)?:

byte mac[] = { 0xC0, 0xFF, 0xEE, 0xBA, 0x11, 0xEA };  
byte ip[] = {192, 168, 5, 130}; 

check the wiring.

for new projects please use the EthernetENC library

void UIPEthernetClass::init(const uint8_t* mac) {
  periodic_timer = millis() + UIP_PERIODIC_TIMER;

  initialized = Enc28J60Network::init((uint8_t*)mac);
  Serial.println(initialized);
  uip_seteth_addr(mac);

  uip_init();
  uip_arp_init();
}

this time , the initialized value is 0 at there.
Could you give me some advice about this?

it means the library can't communicate with the enc28j60 over SPI. check the wiring

Can you share why you think the program is stuck at Ethernet.begin ( )?
How did you find out/arrive at that conclusion?

maybe because it doesn't execute Serial.println("Ethernet begin");?

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