Http get request with ENC28J60

Hello,
I want to send get request from my arduino to a localhost java servlet.
The servlet just displays '1' if the get request gets through.
I just want the arduino to read the '1'.
But I am not able to achieve my objective.

There are a lot of posts for ESP8266 Wifi but there are no posts for ENC28J60
Following is the working code for ESP8266 Wifi that accomplishes exactly what i want.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
 
const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";
 
void setup () {
 
Serial.begin(115200);
WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) {
 
delay(1000);
Serial.print("Connecting..");
 
}
 
}
 
void loop() {
 
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 
HTTPClient http;  //Declare an object of class HTTPClient
 
http.begin("http://jsonplaceholder.typicode.com/users/1");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request
 
if (httpCode > 0) { //Check the returning code
 
String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload
 
}
 
http.end();   //Close connection
 
}
 
delay(30000);    //Send a request every 30 seconds
 
}

I found a WebClient example in my EtherCard library for ENC28J60, but it was using DHCP. Earlier when i tried to run a WebServer, I was facing the same problem. So I changed the DHCP to static IP and it worked, so I changed the DHCP to static for this code too, but the program is still not running.

// Demo using DHCP and DNS to perform a web client request.
// 2011-06-08 <jc@wippler.nl>
//
// License: GPLv2

#include <EtherCard.h>
#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)
static byte myip[] = { 192, 168, 1, 180 };// ethernet interface ip address
static byte gwip[] = { 192, 168, 1, 1 };// gateway ip address
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };// ethernet interface mac address, must be unique on the LAN
byte Ethernet::buffer[500];// tcp/ip send and receive buffer
static uint32_t timer;

const char website[] PROGMEM = "http://localhost:8084/Attendance";

// 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, 3) == 0)//3 is CS pin on arduino
    Serial.println( "Failed to access Ethernet controller");
//      if (!ether.dhcpSetup())
//    Serial.println(F("DHCP failed"));
  ether.staticSetup(myip, gwip);
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

//#if 1
//  // use DNS to resolve the website's IP address
//  if (!ether.dnsLookup(website))
//    Serial.println("DNS failed");
//#elif 2
//  // if website is a string containing an IP address instead of a domain name,
//  // then use it directly. Note: the string can not be in PROGMEM.
//  char websiteIP[] = "192.168.1.1";
//  ether.parseIp(ether.hisip, websiteIP);
//#else
//  // or provide a numeric IP address instead of a string
//  byte hisip[] = { 192,168,1,1 };
//  ether.copyIp(ether.hisip, hisip);
//#endif

  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("/bank?divi=2&roll=10&date=21&month=05&year=2019&min=20&hour=23"), "", website, my_callback);
  }
}

There is no compilation error. I get "<<< REQ" repeatedly in my serial monitor.
I downloaded the library from EtherCard: EtherCard

Can someone please help me out with this?
This step in my project is very crucial to make my project autonomous.

Thank You very much in advance

use ArduinoHTTPClient library over the UIPEthernet library

Thank You for your response, but my Ethernet Module is not able to initialize when i use UIP Ethernet Library. And the ArduinoHttpClient Library has functions for the Wifi not for ethernet. I am not used to Arduino that I could combine them myself.

Ethernet.begin(mac);

The arduino doesnt get past this line in the UIPEthernet Library sketches.

There is post about the exact problem I am facing but it was never solved.

https://forum.arduino.cc/index.php?topic=310100.0
The DNS is failing for me too and the backSoon example is working good.

I would appreciate if you have any ideas for me
Thank you in advance

ebenezerv99:
Thank You for your response, but my Ethernet Module is not able to initialize when i use UIP Ethernet Library. And the ArduinoHttpClient Library has functions for the Wifi not for ethernet. I am not used to Arduino that I could combine them myself.

Ethernet.begin(mac);

The arduino doesnt get past this line in the UIPEthernet Library sketches.

There is post about the exact problem I am facing but it was never solved.

DNS issue with EtherCard library, ENC28J60 module, and Nano v3 - Networking, Protocols, and Devices - Arduino Forum
The DNS is failing for me too and the backSoon example is working good.

I would appreciate if you have any ideas for me
Thank you in advance

did you set the CS pin with Ethernet.init?

the ArduinoHttpClient library uses the base class Client. it is a common base class for all EthernetClient and WiFiClient classes

Thank you for your response,

Juraj:
the ArduinoHttpClient library uses the base class Client. it is a common base class for all EthernetClient and WiFiClient classes

Thats sounds good news to me,if thats true then i just have to manage to initialize the ethernet module with the UIP Library.

Juraj:
did you set the CS pin with Ethernet.init?

I searched for the setting the CS pin within the sketch, but to no avail. So to remove the possibility of the CS pin causing problems, i just connected the CS pin of my ethernet module to the default CS pin of Mega R3(im using Mega).
But if you say there is a function named Ethernet.init(), i will search for it in the library and include it appropriately in my sketch and try again.

Wow, thanks a lot, the UIP Library is working now after i used the init function();

Following is the code I used

/*
   UIPEthernet TcpClient example.

   UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
   Ethernet-shield.

   UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>

        -----------------

   This TcpClient example gets its local ip-address via dhcp and sets
   up a tcp socket-connection to 192.168.0.1 port 5000 every 5 Seconds.
   After sending a message it waits for a response. After receiving the
   response the client disconnects and tries to reconnect after 5 seconds.

   Copyright (C) 2013 by Norbert Truchsess <norbert.truchsess@t-online.de>
*/
#include <UIPEthernet.h>
#include "utility/logging.h"
EthernetClient client;
unsigned long next;


void setup() {
  Serial.begin(9600);
  uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
  Ethernet.init(3); Serial.println("cs pin");
  while (!Ethernet.begin(mac)) 
  { //Configure IP address via DHCP
    Serial.println("not Initiliazed");
  } 
  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());
  next = 0;
}

void loop() {
  while (true) {
    if (((signed long)(millis() - next)) > 0)
    {
      next = millis() + 5000;
      Serial.println("Client connect");
      // replace hostname with name of machine running tcpserver.pl
      //      if (client.connect("server.local",5000))
      if (client.connect(IPAddress(192, 168, 1, 2), 8084))
      {
        Serial.println("Client connected");
        client.println("DATA from Client");
        int size;
        while ((client.available() == 0) && (millis() < next))
        {
        }
        while ((size = client.available()) > 0)
        {
          uint8_t* msg = (uint8_t*)malloc(size + 1);
          memset(msg, 0, size + 1);
          size = client.read(msg, size);
         // Serial.println(msg, size);
          free(msg);
        }

        Serial.println("Client disconnect");
        client.stop();
      }
      else
      {
        Serial.println("Client connect failed");
      }
    }
  }
}

Output:

16:29:15.211 -> cs pin
16:29:19.448 -> Initiliazed
16:29:19.448 -> localIP: 192.168.1.4
16:29:19.481 -> subnetMask: 255.255.255.0
16:29:19.481 -> gatewayIP: 192.168.1.1
16:29:19.516 -> dnsServerIP: 192.168.1.1
16:29:19.551 -> Client connect
16:29:21.031 -> Client connected
16:29:24.542 -> Client disconnect
/*
   UIPEthernet TcpClient example.

   UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
   Ethernet-shield.

   UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>

        -----------------

   This TcpClient example gets its local ip-address via dhcp and sets
   up a tcp socket-connection to 192.168.0.1 port 5000 every 5 Seconds.
   After sending a message it waits for a response. After receiving the
   response the client disconnects and tries to reconnect after 5 seconds.

   Copyright (C) 2013 by Norbert Truchsess <norbert.truchsess@t-online.de>
*/
#include <UIPEthernet.h>
#include <ArduinoHttpClient.h>
#include "utility/logging.h"
EthernetClient client;
unsigned long next;
char serverAddress[] = "192.168.1.2";  // server address
int port = 8084;

void setup() {
  Serial.begin(9600);
  uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
  Ethernet.init(3); Serial.println("cs pin");
  while (!Ethernet.begin(mac)) 
  { //Configure IP address via DHCP
    Serial.println("not Initiliazed");
  } 
  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());
  next = 0;
  

}
HttpClient client1 = HttpClient(client, serverAddress, port);
void loop() 
{  
  Serial.println("making GET request");
  client1.get("/Attendance/bank?divi=2&roll=10&date=21&month=05&year=2019&min=20&hour=23");
  int statusCode = client1.responseStatusCode();
  String response = client1.responseBody();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
  Serial.println("Wait five seconds");
  delay(5000);
}

This is the final code that worked for me.

I cant thank you enough, I had been scratching my head over this the past week.
Thanks a lot man

Hi.......I have created my own web page and making a LED ON/OFF through that web page when i insert IP address in the web browser. Now, I have my own web server and i want to link that server to my Atmega interface with enc28j60 and want to send some data on that server through TCP/IP.

Problem is that I don't understand how to link that server with my controller

My question is, why is the Ethernet libraries name missing from "#includes"? Because there are several types, and even the additional directories associated with it cannot be invented. So how can one test a piece of that program? I have found this on every forum that libraries are missing. Why is this a secret? Thanks to: Tony

it is the original UIPEthernet library by Norbert Truchsess as the comment says.
it has has too many bugs and is abandoned.

use my EthernetENC library for enc28j60. it is a the same library but fixed and updated for all architectures.

1 Like

The first fault, the 3.3V current limit was 50mA (NANO), switched to 5V as described. I fixed UdpNTP.ino with EthernetENC. Thanks for the help. : Tony

Juraj via Arduino Forum <arduino@discoursemail.com> ezt írta (időpont: 2021. ápr. 16., P, 17:26):

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