ENC28j60 API request issue with negative response

Hello

I have an issue getting the response of my GET request. I have an ENC28J60 attatched to it. I get response -2, -3 or some times 200, but it never prints out the content.

See attached my .ino code. When I do the same code in Postman, I get the json object bellow.
If I change the url from /api/user to /api/uoehonu (an adress that does not exist) I get as expected the full error print:

making GET request
Status code: 404
Response:
(…)

I first used an arduino Uno, and since it used 77 % of it’s dynamic memory, I thought this might be due to some unstability because of the memory. So, I changed to the Arduino Mega, but with the same result.

If anyone see any problem with my code / setup, or has another way to receive values from a JSON-object from a webserver to the Arduino, I would be very pleased!

{
“recordsets”: [
[
{
“PersonID”: 1,
“LastName”: “r2d2”,
“FirstName”: “90”,
“Address”: “gata”,
“City”: “vinter”,
“created_date”: “2020-03-01T10:09:00.000Z”
}
]
],
“recordset”: [
{
“PersonID”: 1,
“LastName”: “r2d2”,
“FirstName”: “90”,
“Address”: “gata”,
“City”: “vinter”,
“created_date”: “2020-03-01T10:09:00.000Z”
}
],
“output”: {},
“rowsAffected”: [
1
]
}

SMSserviceWorksIsh.ino (1.84 KB)

https://www.arduino.cc/en/Tutorial/WebClient

Thank you!

I get the output: Initialize Ethernet with DHCP: Failed to configure Ethernet using DHCP Ethernet shield was not found. Sorry, can't run without hardware. :(

Since I am using the MEGA now, I use pin 50, 51 and 52. CS: 53

So, the change I did with the code was to add Ethernet.init(53);

Without doing any changes with the hardware, the ethernetshield works with the code I attached in my first post, though not using the SPI-library. Why can this be?

I see from https://forum.arduino.cc/index.php?topic=567246.0 that you say:

the Ethernet library is for Wiznet W5000 chips, not for the ENC28J60 chip. for the ENC28J60 is the UIPEthernet library. but it is too big for Atmega 328p. other option is EtherCard library

So, I tried to change from Ethernet.h to UIPEthernet.h but of course, that caused alot of new issues with non-existing methods.

Any idea of how to get through this?

See in the example how to send a HTTP request

Yay, got it to work by modifing the code in that link to work with ENC28J20!

Thanks alot!!! :slight_smile:

/*
  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 <UIPEthernet.h>
#include <ArduinoHttpClient.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(10,280,18,91);  // 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(10, 250, 21, 112);
IPAddress myDns(10, 250, 20, 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() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(53);  // 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");

    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());


    Serial.println("Initiliazed");
    Serial.print(("localIP: "));
    Serial.println(Ethernet.localIP());
    Serial.print(("subnetMask: "));
    Serial.println(Ethernet.subnetMask());
    Serial.print(("gatewayIP: "));
    Serial.println(Ethernet.gatewayIP());
    Serial.print(("dnsServerIP: "));
    Serial.println(Ethernet.dnsServerIP());
  }
  // 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, 8280)) {
    Serial.print("connected to ");
//    Serial.println(client.remoteIP());
    // Make a HTTP request:
    client.println("GET /api/user 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);
    }
  }
}

you should set Host header to the host you connect to

client.print("Host: "); client.println(server);

Thanks, I will update that :)

It seems like my next issue is unstable network connection. So, I need to reset the Arduino about 10 times before it returns the json. Usually it returns:

connection failed

disconnecting. Received 0 bytes in 0.0001, rate = 0.00 kbytes/second

Are there any way to make it wait at least 1 minute before it disconnects?

do you use the latest version of the UIPEthernet library?

Yes, the UIPEthernet-2.0.8

is the MAC address and IP address unique in your local network? do you have some other SPI device attached? for example a SD card? is ground wired and vcc at right voltage?

Hello!

Thanks alot for your great support Juraj!

When I continously ping the Ethernet shield from another computer in the network, I only get response from it while it is connecting, and untill it either returns the json or fails and disconnects. This shows that there is not any IP conflict. The MAC-address is just the default code from the Shield. I would be supprised if that was a conflict, but if you have a way I can verify it - I will be happy to test it.

I only have the Ethernet shield connected to the Mega. Nothing else.

I've tried both 5V and 3.3V voltage to the Ethernet shield, with the Arduino powered through USB on my laptop.

Ground is connected between the Arduino and the ENC.

The shield gets IP assigned from the DHCP, and this works fine every time. The shield is "pingable" evry time I reset the Arduino and untill it disconnects/return json. It's only the HTTP-request it fails with.

Are there any other suggestions on how to make this a little more stable? Anything I can change in the code to make it less sensible for unstable network or whatever is the issue?

do you have some other SPI device attached? exactly what shield do you have?

Hello Juraj

Thank you for your feedback. I only have the ENC28J60 connected to the Mega (via SPI). Nothing else.

There is a label on the Ethernet-port saying HanRun HR911105A 14/16 and on the back side it says ENC28J60 The chip says: ENC28J60-I/SO (e3) 1445JDE

r2d290: Hello Juraj

Thank you for your feedback. I only have the ENC28J60 connected to the Mega (via SPI). Nothing else.

There is a label on the Ethernet-port saying HanRun HR911105A 14/16 and on the back side it says ENC28J60 The chip says: ENC28J60-I/SO (e3) 1445JDE

HanRun is the connector. There are only a few ems28j60 module designs. I want to know which one is yours. where do you buy the module? do you have a link?

Hello

The product name is "ENC28J60 Ethernet LAN Network Module For Arduino SPI AVR PIC LPC STM32" and it was bought from eBay. Unfortunatly the link to it seems to be broken. I bought it back in 2015 so it might not be available anymore.

I'm not sure if a picture will help to answer this? https://photos.app.goo.gl/GD234XsNeNuNY1MX6

this is a 3.3 V only module and the Mega 3.3 V pin can't supply enough current for the enc28j60 top power consumption.

the data pins of enc28j60 are 5 V tolerant and the ATmega will read the 3.3 V as HIGH, but you must solve the powering.