Struggling In interfacing RP2040 With ENC28j60 Module

Hi, I am Using Raspberry pi pico and want to send data over mqtt i am using ethernet module (ENC28J60), i am able to ping successfully, But when I define EthernetClient or EthernetServer class my code hangs.. My code is following

#include <SPI.h>
#include <EthernetENC.h>
//#include <PubSubClient.h>
#include <ArduinoMqttClient.h>

EthernetClient ethClient;

// Set the SPI pins used for communication with ENC28J60
const int ENC_CS_PIN = 17;
const int ENC_INT_PIN = 2; // Interrupt pin for receiving packets

// Set the MAC address and IP address for the ENC28J60
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 5, 50, 253);

IPAddress server(10, 5, 50, 47);

//void callback(char* topic, byte* payload, unsigned int length) {
// Serial.print("Message arrived [");
// Serial.print(topic);
// Serial.print("] ");
// for (int i=0;i<length;i++) {
// Serial.print((char)payload[i]);
// }
// Serial.println();
//}
//
EthernetServer eth;

//PubSubClient client(ethClient);
//
//void reconnect() {
// // Loop until we're reconnected
// while (!client.connected()) {
// Serial.print("Attempting MQTT connection...");
// // Attempt to connect
// if (client.connect("arduinoClient")) {
// Serial.println("connected");
// // Once connected, publish an announcement...
// client.publish("outTopic","hello world");
// // ... and resubscribe
// client.subscribe("inTopic");
// } else {
// Serial.print("failed, rc=");
// Serial.print(client.state());
// Serial.println(" try again in 5 seconds");
// // Wait 5 seconds before retrying
// delay(5000);
// }
// }
//}

void setup() {
// Initialize the SPI communication with ENC28J60
SPI.begin();
// SPI.beginTransaction(SPISettings(25000000, MSBFIRST, SPI_MODE0));
pinMode(ENC_CS_PIN, OUTPUT);
digitalWrite(ENC_CS_PIN, HIGH);

// Initialize the Ethernet library with the SPI pins and MAC address
Ethernet.init(ENC_CS_PIN);
Ethernet.begin(mac, ip);

Serial.print("ETHERNETTTTTTTTTTT");

// Configure the interrupt pin for receiving packets
// pinMode(ENC_INT_PIN, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(ENC_INT_PIN), packetReceivedISR, FALLING);

// Connect to the MQTT broker
// client.setServer(server, 1883);
// client.setCallback(callback);
}

void loop() {
// Handle incoming packets if any received
Ethernet.maintain();
Serial.print("loooooooooooooooppppppppp");
delay(100);

// Connect to MQTT broker and handle messages
// if (!client.connected()) {
// reconnect();
// }
// client.loop();
}

EthernetENC doesn't use the interrupt pin. the library handles SPI.
try the WebClient example of the Ethernet library (only change the include and use init to set the cs pin)

Hi Jura, Thanks for reply as you suggest i have try using webclient example my code is following at console i am getting this only "16:29:48.353 -> Initialize Ethernet with DHCP:"

/*
  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
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <EthernetENC.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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// 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;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement

void setup() {

  delay(5000);
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(17);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  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 congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  beginMicros = micros();
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  int len = client.available();
  if (len > 0) {
    byte buffer[80];
    if (len > 80) len = 80;
    client.read(buffer, len);
    if (printWebData) {
      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
    }
    byteCount = byteCount + len;
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    endMicros = micros();
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    Serial.print("Received ");
    Serial.print(byteCount);
    Serial.print(" bytes in ");
    float seconds = (float)(endMicros - beginMicros) / 1000000.0;
    Serial.print(seconds, 4);
    float rate = (float)byteCount / seconds / 1000.0;
    Serial.print(", rate = ");
    Serial.print(rate);
    Serial.print(" kbytes/second");
    Serial.println();

    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
}

And when I comment EthernetClient client object related code this is what i am getting on console "16:39:40.613 -> Initialize Ethernet with DHCP:
16:39:44.828 -> DHCP assigned IP 10.5.50.111
16:39:45.819 -> connecting to www.google.com..."

MY NEW CHANGES ARE

/*
  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
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <EthernetENC.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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// 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;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement

void setup() {

  delay(5000);
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(17);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  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 congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
//  if (client.connect(server, 80)) {
//    Serial.print("connected to ");
//    Serial.println(client.remoteIP());
//    // Make a HTTP request:
//    client.println("GET /search?q=arduino HTTP/1.1");
//    client.println("Host: www.google.com");
//    client.println("Connection: close");
//    client.println();
//  } else {
//    // if you didn't get a connection to the server:
//    Serial.println("connection failed");
//  }
//  beginMicros = micros();
}

void loop() {

    Ethernet.maintain();
  Serial.print("loooooooooooooooppppppppp");
  delay(100);
  
  // if there are incoming bytes available
  // from the server, read them and print them:
//  int len = client.available();
//  if (len > 0) {
//    byte buffer[80];
//    if (len > 80) len = 80;
//    client.read(buffer, len);
//    if (printWebData) {
//      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
//    }
//    byteCount = byteCount + len;
//  }
//
//  // if the server's disconnected, stop the client:
//  if (!client.connected()) {
//    endMicros = micros();
//    Serial.println();
//    Serial.println("disconnecting.");
//    client.stop();
//    Serial.print("Received ");
//    Serial.print(byteCount);
//    Serial.print(" bytes in ");
//    float seconds = (float)(endMicros - beginMicros) / 1000000.0;
//    Serial.print(seconds, 4);
//    float rate = (float)byteCount / seconds / 1000.0;
//    Serial.print(", rate = ");
//    Serial.print(rate);
//    Serial.print(" kbytes/second");
//    Serial.println();
//
//    // do nothing forevermore:
//    while (true) {
//      delay(1);
//    }
//  }
}

and the ping interval is too high

Reply from 10.5.50.111: bytes=32 time=1790ms TTL=128
Reply from 10.5.50.111: bytes=32 time=806ms TTL=128
Reply from 10.5.50.111: bytes=32 time=787ms TTL=128
Reply from 10.5.50.111: bytes=32 time=890ms TTL=128

try to add Serial.flush(); after Serial.println("...");

when i uncomment EthernetClient client My code got stuck

yes, but flush should wait until the Serial output is sent. because the code should run at least to connect. Serial is handled with interrupts so the output is sent from a buffer while the sketch continues after print

Jurag,
I have add Serial.flush(), in my code as you suggest, but it didn't work for me... my issue is still same..

I am telling you what i have done till now..

I have imported webclient example from ethernet library and change the
#include Ethernet.h to #include EthernetENC.h

And uncomment Ethernet.init(17);
And run the code the code stops at Serial.println("Initialize Ethernet with DHCP:");
And unable to connect to server and unable to get ip address Through DHCP or Statically

And when i comment out the line EthernetClient client;

My pico got ip from Dhcp and i am able to ping that ip from my system

So please help me what wrong i am doing
And i have also add Serial.flush(); as you suggest but this also doesn't work

please help me

I will test it. I have a Pico. I don't remember if I tested EthernetENC with it.

i am getting these logs

15:56:07.635 -> --------------
15:56:07.635 -> packet received
15:56:07.635 -> readPacket type ARP, uip_len: 60
15:56:07.635 -> freeing received packet
15:56:07.635 -> stop(), data: NULL
15:56:07.635 -> --------------
15:56:07.635 -> packet received
15:56:07.635 -> readPacket type ARP, uip_len: 60
15:56:07.635 -> freeing received packet
15:56:07.873 -> chksum uip_buf[34-58]: 1EEB
15:56:07.873 -> Enc28J60Network_send uip_buf (uip_len): 42, packet: 1, bits: 10
15:56:07.873 -> --------------
15:56:07.873 -> packet received
15:56:07.873 -> readPacket type ARP, uip_len: 60
15:56:07.873 -> freeing received packet
15:56:08.254 -> chksum uip_buf[34-58]: 1EEB
15:56:08.254 -> Enc28J60Network_send uip_buf (uip_len): 58, packet: 1, bits: 10
15:56:08.254 -> --------------
15:56:08.254 -> packet received
15:56:08.254 -> readPacket type IP, uip_len: 60, bits: 10010

After getting
15:56:08.254 -> readPacket type IP, uip_len: 60, bits: 10010 the code get hanged

after enabling the logs what i found is when i try to access pico from pc like trying to ping to pico or if i use pico as web server and try to open page from browser the pico got hanged i am getting following logs before pico got hanged

19:17:03.873 -> Ethernet WebServer Example
19:17:03.920 -> Ethernet cable is not connected.
19:17:03.920 -> server is at 10.5.50.253
19:17:05.299 -> --------------
19:17:05.299 -> packet received
19:17:05.299 -> readPacket type ARP, uip_len: 60
19:17:05.299 -> freeing received packet
19:17:06.346 -> --------------
19:17:06.346 -> packet received
19:17:06.346 -> readPacket type ARP, uip_len: 60
19:17:06.346 -> freeing received packet
19:17:07.336 -> --------------
19:17:07.336 -> packet received
19:17:07.336 -> readPacket type ARP, uip_len: 60
19:17:07.336 -> freeing received packet
19:17:08.381 -> --------------
19:17:08.381 -> packet received
19:17:08.381 -> readPacket type ARP, uip_len: 60
19:17:08.381 -> freeing received packet
19:17:09.380 -> --------------
19:17:09.380 -> packet received
19:17:09.380 -> readPacket type ARP, uip_len: 60
19:17:09.380 -> freeing received packet
19:17:10.427 -> --------------
19:17:10.427 -> packet received
19:17:10.427 -> readPacket type ARP, uip_len: 60
19:17:10.427 -> freeing received packet
19:17:11.472 -> --------------
19:17:11.472 -> packet received
19:17:11.472 -> readPacket type ARP, uip_len: 60
19:17:11.472 -> freeing received packet
19:17:15.464 -> --------------
19:17:15.464 -> packet received
19:17:15.464 -> readPacket type ARP, uip_len: 60
19:17:15.464 -> freeing received packet
19:17:16.509 -> --------------
19:17:16.509 -> packet received
19:17:16.509 -> readPacket type ARP, uip_len: 60
19:17:16.509 -> freeing received packet
19:17:17.512 -> --------------
19:17:17.512 -> packet received
19:17:17.512 -> readPacket type ARP, uip_len: 60
19:17:17.512 -> freeing received packet
19:17:18.559 -> --------------
19:17:18.559 -> packet received
19:17:18.559 -> readPacket type ARP, uip_len: 60
19:17:18.559 -> freeing received packet
19:17:19.557 -> --------------
19:17:19.557 -> packet received
19:17:19.557 -> readPacket type ARP, uip_len: 60
19:17:19.557 -> freeing received packet
19:17:20.603 -> --------------
19:17:20.603 -> packet received
19:17:20.603 -> readPacket type ARP, uip_len: 60
19:17:20.603 -> freeing received packet
19:17:24.654 -> --------------
19:17:24.654 -> packet received
19:17:24.654 -> readPacket type ARP, uip_len: 60
19:17:24.654 -> freeing received packet
19:17:25.652 -> --------------
19:17:25.652 -> packet received
19:17:25.652 -> readPacket type ARP, uip_len: 60
19:17:25.652 -> freeing received packet
19:17:26.717 -> --------------
19:17:26.717 -> packet received
19:17:26.717 -> readPacket type ARP, uip_len: 60
19:17:26.717 -> freeing received packet
19:17:27.720 -> --------------
19:17:27.720 -> packet received
19:17:27.720 -> readPacket type ARP, uip_len: 60
19:17:27.720 -> freeing received packet
19:17:28.722 -> --------------
19:17:28.722 -> packet received
19:17:28.722 -> readPacket type ARP, uip_len: 60
19:17:28.722 -> freeing received packet
19:17:29.737 -> --------------
19:17:29.737 -> packet received
19:17:29.737 -> readPacket type ARP, uip_len: 60
19:17:29.737 -> freeing received packet
19:17:33.222 -> --------------
19:17:33.222 -> packet received
19:17:33.222 -> readPacket type ARP, uip_len: 60
19:17:33.222 -> freeing received packet
19:17:34.222 -> --------------
19:17:34.222 -> packet received
19:17:34.222 -> readPacket type ARP, uip_len: 60
19:17:34.222 -> freeing received packet
19:17:35.267 -> --------------
19:17:35.267 -> packet received
19:17:35.267 -> readPacket type ARP, uip_len: 60
19:17:35.267 -> freeing received packet
19:17:36.266 -> --------------
19:17:36.266 -> packet received
19:17:36.266 -> readPacket type ARP, uip_len: 60
19:17:36.266 -> freeing received packet
19:17:37.301 -> --------------
19:17:37.301 -> packet received
19:17:37.301 -> readPacket type ARP, uip_len: 60
19:17:37.301 -> freeing received packet
19:17:38.346 -> --------------
19:17:38.346 -> packet received
19:17:38.346 -> readPacket type ARP, uip_len: 60
19:17:38.346 -> freeing received packet
19:17:39.348 -> --------------
19:17:39.348 -> packet received
19:17:39.348 -> readPacket type ARP, uip_len: 60
19:17:39.348 -> freeing received packet
19:17:40.397 -> --------------
19:17:40.397 -> packet received
19:17:40.397 -> readPacket type ARP, uip_len: 60
19:17:40.397 -> freeing received packet
19:17:41.396 -> --------------
19:17:41.396 -> packet received
19:17:41.396 -> readPacket type ARP, uip_len: 60
19:17:41.396 -> freeing received packet
19:17:42.424 -> --------------
19:17:42.424 -> packet received
19:17:42.424 -> readPacket type ARP, uip_len: 60
19:17:42.424 -> freeing received packet
19:17:43.471 -> --------------
19:17:43.471 -> packet received
19:17:43.471 -> readPacket type ARP, uip_len: 60
19:17:43.471 -> freeing received packet
19:17:44.469 -> --------------
19:17:44.469 -> packet received
19:17:44.469 -> readPacket type ARP, uip_len: 60
19:17:44.469 -> freeing received packet
19:17:45.516 -> --------------
19:17:45.516 -> packet received
19:17:45.516 -> readPacket type ARP, uip_len: 60
19:17:45.516 -> freeing received packet
19:17:46.513 -> --------------
19:17:46.513 -> packet received
19:17:46.513 -> readPacket type ARP, uip_len: 60
19:17:46.513 -> freeing received packet
19:17:47.546 -> --------------
19:17:47.546 -> packet received
19:17:47.546 -> readPacket type ARP, uip_len: 60
19:17:47.546 -> freeing received packet
19:17:48.591 -> --------------
19:17:48.591 -> packet received
19:17:48.591 -> readPacket type ARP, uip_len: 60
19:17:48.591 -> freeing received packet
19:17:49.592 -> --------------
19:17:49.592 -> packet received
19:17:49.592 -> readPacket type ARP, uip_len: 60
19:17:49.592 -> freeing received packet
19:17:50.637 -> --------------
19:17:50.637 -> packet received
19:17:50.637 -> readPacket type ARP, uip_len: 60
19:17:50.637 -> freeing received packet
19:17:54.018 -> --------------
19:17:54.018 -> packet received
19:17:54.018 -> readPacket type ARP, uip_len: 60
19:17:54.018 -> freeing received packet
19:17:55.004 -> --------------
19:17:55.004 -> packet received
19:17:55.004 -> readPacket type ARP, uip_len: 60
19:17:55.004 -> freeing received packet
19:17:56.033 -> --------------
19:17:56.033 -> packet received
19:17:56.033 -> readPacket type ARP, uip_len: 60
19:17:56.033 -> freeing received packet
19:17:57.034 -> --------------
19:17:57.034 -> packet received
19:17:57.034 -> readPacket type ARP, uip_len: 60
19:17:57.034 -> freeing received packet
19:17:58.080 -> --------------
19:17:58.080 -> packet received
19:17:58.080 -> readPacket type ARP, uip_len: 60
19:17:58.080 -> freeing received packet
19:17:59.077 -> --------------
19:17:59.077 -> packet received
19:17:59.077 -> readPacket type ARP, uip_len: 60
19:17:59.077 -> freeing received packet
19:18:00.121 -> --------------
19:18:00.121 -> packet received
19:18:00.121 -> readPacket type ARP, uip_len: 60
19:18:00.121 -> freeing received packet
19:18:01.122 -> --------------
19:18:01.122 -> packet received
19:18:01.122 -> readPacket type ARP, uip_len: 60
19:18:01.122 -> freeing received packet
19:18:02.169 -> --------------
19:18:02.169 -> packet received
19:18:02.169 -> readPacket type ARP, uip_len: 60
19:18:02.169 -> freeing received packet
19:18:03.216 -> --------------
19:18:03.216 -> packet received
19:18:03.216 -> readPacket type ARP, uip_len: 60
19:18:03.216 -> freeing received packet
19:18:04.216 -> --------------
19:18:04.216 -> packet received
19:18:04.216 -> readPacket type ARP, uip_len: 60
19:18:04.216 -> freeing received packet
19:18:05.263 -> --------------
19:18:05.263 -> packet received
19:18:05.263 -> readPacket type ARP, uip_len: 60
19:18:05.263 -> freeing received packet
19:18:06.260 -> --------------
19:18:06.260 -> packet received
19:18:06.260 -> readPacket type ARP, uip_len: 60
19:18:06.260 -> freeing received packet
19:18:07.311 -> --------------
19:18:07.311 -> packet received
19:18:07.311 -> readPacket type ARP, uip_len: 60
19:18:07.311 -> freeing received packet
19:18:07.644 -> --------------
19:18:07.644 -> packet received
19:18:07.644 -> readPacket type ARP, uip_len: 60
19:18:07.644 -> Enc28J60Network_send uip_buf (uip_len): 42, packet: 1, bits: 0
19:18:07.644 -> freeing received packet
19:18:07.644 -> --------------
19:18:07.644 -> packet received
19:18:07.644 -> readPacket type IP, uip_len: 74, bits: 10 --------------------------------->>>>> here i try to access my pico ip from my pc

My code is


/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi
 
 */

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

const int EchoPin = 2;
const int TriggerPin = 3;
int cm;

int ping(int TriggerPin, int EchoPin) {
   long duration, distanceCm;
   
   digitalWrite(TriggerPin, LOW);  //para generar un pulso limpio ponemos a LOW 4us
   delayMicroseconds(4);
   digitalWrite(TriggerPin, HIGH);  //generamos Trigger (disparo) de 10us
   delayMicroseconds(10);
   digitalWrite(TriggerPin, LOW);
   
   duration = pulseIn(EchoPin, HIGH);  //medimos el tiempo entre pulsos, en microsegundos
   
   distanceCm = duration * 10 / 292/ 2;   //convertimos a distancia, en cm
   return distanceCm;
}


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 5, 50, 253);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {

  delay(5000);
   pinMode(TriggerPin, OUTPUT);
   pinMode(EchoPin, INPUT);
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet
 Ethernet.init(17);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer Example");

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

  // 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.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          cm = ping(TriggerPin, EchoPin);
          client.print("Distancia: ");
          client.print(cm);
          client.println("<br />");
          delay(1000);
          // output the value of each analog input pin
          //for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
          //  int sensorReading = analogRead(analogChannel);
          //  client.print("analog input ");
          //  client.print(analogChannel);
          //  client.print(" is ");
          //  client.print(sensorReading);
          //  client.println("<br />");
          //}
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

I am debugging it right now. it crashes in the uIP libray on read access to the parameter ipaddr in uip_arp_update when called from uip_arp_ipin. it is very strange.
the uip library is 20 years old. and it was very popular then. all bugs were found long time ago.

2 Likes

hi juraj, thanks for helping have you debug that , is that worked or not.

I have a workaround for uip_arp_update. this will definitely not be the final solution.
I am afraid the same problem can occur in other parts of the C code.

uip_arp.c (13.4 KB)

Thanks juraj, after replacing uip_arp.c file Now its working. What was the issue.

the issue is as I described it in above. I suspect it is some compiler bug with that old C code. (something like discussed here 58270 – Wrong code while accessing trailing array elements in a global common structure)

I can't fix the issue. I can only do a workaround. I found an acceptable workaround and pushed it to GitGub repo.

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