02-20-2025, 09:11 AM
Hello Forum colleagues,
I am trying to set the KC868_A8M up, so it performs HTTP Get requests from an API server.
I want to do it via Ethernet (Not Wifi) in a local server.
According to the LAN example code I can get the ETehrnet connection to work, I am also able to ping the device and get a response.
I am struggling with the HTTP GEt requests though:
HTTP Error code: -1
Is there an example code doing the same thing? Or are there any hints on what I am doing wrong?
I am trying to set the KC868_A8M up, so it performs HTTP Get requests from an API server.
I want to do it via Ethernet (Not Wifi) in a local server.
According to the LAN example code I can get the ETehrnet connection to work, I am also able to ping the device and get a response.
I am struggling with the HTTP GEt requests though:
HTTP Error code: -1
Is there an example code doing the same thing? Or are there any hints on what I am doing wrong?
Code:
#include <ETH.h>
#include <HTTPClient.h>
#define ETH_ADDR 0
#define ETH_POWER_PIN -1
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
IPAddress local_ip(192, 168, 49, 98); // Set static IP address for ESP32
IPAddress gateway(192, 168, 49, 1); // Gateway IP
IPAddress subnet(255, 255, 255, 0); // Subnet mask
IPAddress dns(192, 168, 1, 1); // DNS server IP
void setup() {
Serial.begin(115200);
Serial.println();
// Initialize Ethernet interface
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); //start with ETH
if (ETH.config(local_ip, gateway, subnet, dns, dns) == false) {
Serial.println("LAN8720 Configuration failed.");
} else {
Serial.println("LAN8720 Configuration success.");
}
Serial.println("Ethernet Connected");
Serial.print("IP Address: ");
Serial.println(ETH.localIP()); // Print ESP32 IP
}
void loop() {
// Create an HTTPClient object
HTTPClient http;
// Specify the request URL
http.begin("http://192.168.99.3:8481/test"); // Replace with your URL
// Make the GET request
int httpCode = http.GET();
if (httpCode > 0) { // Check if the request was successful
String payload = http.getString(); // Get the response payload
Serial.println("HTTP GET request successful!");
Serial.println(payload); // Print the payload
} else {
Serial.println("HTTP GET failed!");
Serial.printf("Error code: %d\n", httpCode);
}
http.end(); // Close the connection
delay(5000); // Wait before making another request
}