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