First steps with ESP8266 Wemos, http Get Request not work

Hello
I tried to send a easy http get parameter to my lokal server script, to save it.
I only got a fail message:

 http.begin(serverPath.c_str());
      |       ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: call to 'HTTPClient::begin' declared with attribute error: obsolete API, use ::begin(WiFiClient, url)

does anyone have a solution?
my code:


#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "certs.h"
#include <ESP8266HTTPClient.h>

#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "pw!"
#endif

//Your Domain name with URL path or IP address with path
String serverName = "myserver/script.php";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;



const char* ssid = STASSID;
const char* password = STAPSK;

X509List cert(cert_DigiCert_Global_Root_CA);

HTTPClient http; 
WiFiClient client;

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
//Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "?temperature=24.37";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
       
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }




}

The code seems to be missing the definition of the cert_DigiCert_Global_Root_CA variable, which is used to verify the server's SSL certificate when making HTTPS requests.
Try to include this:
const unsigned char cert_DigiCert_Global_Root_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIEkjCCA3qgAwIBAgIQAf2jgdBh8NdJqzPxfMfjtzANBgkqhkiG9w0BAQsFADBa
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
QSAyMDE5MB4XDTE5MDUyMTAwMDAwMFoXDTIwMDUyMTIzNTk1OVowWjELMAkGA1UE
BhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3LmRpZ2lj
ZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgR2xvYmFsIFJvb3QgQ0EgMjAxOTCC
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJgIvglRHhN/tJjL1YdbTtla
TJdKjwV0Bp8Wmuq3rfsjLmz7SWLlY8Wuc7VQ2ZxDw6b5ys6S5j6Rrp+C/7YPfqE
e+9k4JqNrb4pBlZgEKcZm0B0puSEjTDrcLO2gDLEwk0XFTlnwuC0JmLhQbZ1M0sK
Hn0t8Wc42iUBkLjXSPnDSeTtYNI1jKwJiG86x7r3vLL8W+7Fl/ejNUFWFYzQV22e
8WxlKjJNFGiC+zrKLq3rNpWLV7jKzxcbluV2vF8RcWzk2m45fZKs52t0m8fuCtzT
aETrKpXbGv+YtIHiBXwZji88EkwIh/xAUEKj1WJKQ1D9n9XoF0MCAwEAAaOCAZMw
ggGTMB8GA1UdIwQYMBaAFE4y4/d3vYb/Lgc+/vdC7x0VcVgUMA4GA1UdDwEB/wQE
Aw

get a fail message
so i edit the code to:

const unsigned char cert_DigiCert_Global_Root_CA[] PROGMEM = R"###(

-----BEGIN CERTIFICATE-----

MIIEkjCCA3qgAwIBAgIQAf2jgdBh8NdJqzPxfMfjtzANBgkqhkiG9w0BAQsFADBa

MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3

d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBHbG9iYWwgUm9vdCBD

QSAyMDE5MB4XDTE5MDUyMTAwMDAwMFoXDTIwMDUyMTIzNTk1OVowWjELMAkGA1UE

BhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3LmRpZ2lj

ZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgR2xvYmFsIFJvb3QgQ0EgMjAxOTCC

ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJgIvglRHhN/tJjL1YdbTtla

TJdKjwV0Bp8Wmuq3rfsjLmz7SWLlY8Wuc7VQ2ZxDw6b5ys6S5j6Rrp+C/7YPfqE

e+9k4JqNrb4pBlZgEKcZm0B0puSEjTDrcLO2gDLEwk0XFTlnwuC0JmLhQbZ1M0sK

Hn0t8Wc42iUBkLjXSPnDSeTtYNI1jKwJiG86x7r3vLL8W+7Fl/ejNUFWFYzQV22e

8WxlKjJNFGiC+zrKLq3rNpWLV7jKzxcbluV2vF8RcWzk2m45fZKs52t0m8fuCtzT

aETrKpXbGv+YtIHiBXwZji88EkwIh/xAUEKj1WJKQ1D9n9XoF0MCAwEAAaOCAZMw

ggGTMB8GA1UdIwQYMBaAFE4y4/d3vYb/Lgc+/vdC7x0VcVgUMA4GA1UdDwEB/wQE

Aw)###";

always a fail:
conflicting declaration 'const unsigned char cert_DigiCert_Global_Root_CA []'

I think it is because i import "include certs.h" there is also the certifikat

Try this code. If it works then adapt it to fit your case.

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* serverName = "myserver.com";
const int httpsPort = 443;

WiFiClientSecure client;

void setup() {
  Serial.begin(9600);

  // Connect to Wi-Fi network
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("Connected to Wi-Fi");

  // Set up HTTPS client
  client.setInsecure();
}

void loop() {
  // Make HTTPS request
  if (client.connect(serverName, httpsPort)) {
    String serverPath = "/script.php?temperature=24.37";
    Serial.print("Sending HTTPS request to ");
    Serial.print(serverName);
    Serial.println(serverPath);
    client.print(String("GET ") + serverPath + " HTTP/1.1\r\n" +
                 "Host: " + serverName + "\r\n" +
                 "Connection: close\r\n\r\n");
    Serial.println("HTTPS request sent");
  } else {
    Serial.println("HTTPS connection failed");
  }

  // Wait for server response
  while (client.connected()) {
    if (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
    }
  }

  // Wait before making another request
  delay(5000);
}

have it by change this command

 // Your Domain name with URL path or IP address with path
      http.begin(client, serverurl);

in the first agrnument must be the wifi client. i don't know it

Another question to the same topic.

Can I use with the wemos the returning data?

If I send a get request to my php script and write in the php script a output. How can I use this?
For example:
I send the number "2". The php script multiplicate it with 5. The result is 10.

How can I use the result in the wemos?

Thank you

i think http.getstring is the correct way

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