Fetch data from MySQL by using httpCode = https.GET(); not working with Ethernet LAN

Hi Guys,

I have code was using it in ESP32 with Wifi connection to fetch variable from MySQL and was working fine but when connect by Ethernet LAN using "ETH.h" Library, it doesn't work with me.
here is my code:


#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ETH.h>
#include <SPI.h>
#include <OneWire.h>
#include <HTTPClient.h>

#include <WiFiClientSecure.h>


char server[] = "www.1234.com"; /// IP ADDRESS OF THE LOCAL SERVER WE CONNECT AND SEND DATA TO
int  interval = 180000; // DELAY INTERVAL TO SAVE THE DATA IN THE DATABSE
long myinterval = 180000; //3 Minutes-> 180000ms INTERVAL BETWEEN DATA READINGS, I SET IT IN 6 SECONDS FOR TESTING PURPOSES
long previousMillis = 0;
char host[] = "1234.com";

static bool eth_connected = false;


// This is GandiStandardSSLCA2.pem, the root Certificate Authority that signed 
// the server certifcate for the demo server https://jigsaw.w3.org in this
// example. This certificate is valid until Sep 11 23:59:59 2024 GMT
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"12365\n" \
 \
"-----END CERTIFICATE-----\n";



void WiFiEvent(WiFiEvent_t event)
{
        

  switch (event) {
    case SYSTEM_EVENT_ETH_START:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("esp32-ethernet");
      break;
    case SYSTEM_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      break;
    case SYSTEM_EVENT_ETH_GOT_IP:
      Serial.print("ETH MAC: ");
      Serial.print(ETH.macAddress());
      Serial.print(", IPv4: ");
      Serial.print(ETH.localIP());
      if (ETH.fullDuplex()) {
        Serial.print(", FULL_DUPLEX");
      }
      Serial.print(", ");
      Serial.print(ETH.linkSpeed());
      Serial.println("Mbps");
      eth_connected = true;

      break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case SYSTEM_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default:
      break;
  }

}

void fetchsql(){


 WiFiClientSecure client;
  client.setCACert(rootCACertificate);
  HTTPClient https;
  
if (https.begin(client, "https://1234.com/Vafriable.php?id=1")){ 
 Serial.print("[HTTPS ] GET...\n");
int httpCode = https.GET();
Serial.print("httpCode : ");
Serial.println(httpCode);
 
  const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5);
  DynamicJsonBuffer jsonBuffer(bufferSize);
  JsonObject& root = jsonBuffer.parseObject(https.getString());
 
String var1 = root["VAR1"];
String var2 = root["VAR2"];
int var3 = root["VAR3"];
Serial.println(var1);
Serial.println(var2);
Serial.println(var3);
}
}


void setup() {
   Serial.begin(115200);
  sensors.begin ();
  fetchsql();
  WiFi.onEvent(WiFiEvent);
  ETH.begin();
}

  void loop() {
 
}

Do the examples that come with the ETH library work? You should find them in File->Examples->WiFi->ETH_...

1 Like

Actually didn't find example to follow it! and ETH.h Library inside file->Examples->WiFi.

Which ESP32 board model do you have selected? I selected the "WEMOS LOLIN32" and it had ETH_ examples.

I am using ESP32-POE from Lilygo and I am selecting ESP32 Dev module!

I switched to "WEMOS LOLIN32" but didn't find example for ETH_?

1 Like

I did that and still see the three "ETH_..." examples in File->Examples->WiFi->

1 Like

Yes is there, but all example for Wifi and it works with me when using WiFi Connection!

yes Thanks for you guys, it works with me when I used BasicHTTPSClinet example!

even the name is WiFiClient it will use the Ethernet interface

1 Like

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