ESP8266 HTTPS Connection Failed

Hi. I was trying a system that communicates with both firebase and google sheets at the same time. If i try one by one like just get https or just connect to the firebase, there is no problem. But when i try both, always gives me "connection failed" error.


#include <SPI.h>
#include <FirebaseESP8266.h>
#include <MFRC522.h>
#include <Arduino.h>
#include <ESP8266mDNS.h>
//#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//#include <WiFiClientSecure.h>
#include <WiFiClientSecureBearSSL.h>
#include <WiFiClient.h>
#include <LiquidCrystal_I2C.h>
#include <NTPtimeESP.h>
//-----------------------------------------
#define RST_PIN  D3
#define SS_PIN   D4
#define BUZZER   D8
//-----------------------------------------
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;  
MFRC522::StatusCode status;      
//-----------------------------------------
//Clock Settings
unsigned long sonSenkronSaati = 0;
int kayitDurum = 0;
NTPtime NTPch("pool.ntp.org");
strDateTime dateTime;
byte actualHour;
byte actualMinute;
byte actualSecond;
int actualyear;
byte actualMonth;
byte actualday;
byte actualdayofWeek;
boolean valid;
String tamsaat;
String tamgun;
//////////////////////////////////////

/////Firebase Settings
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>
#define API_KEY "*******"
#define DATABASE_URL "******"
String kullanici;
String id_number;
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
/////////////////////////////////////

int blockNum = 2;  
byte bufferLen = 18;
byte readBlockData[18];
//-----------------------------------------
String card_holder_name;
const String sheet_url = "https://script.google.com/macros/s/******";  //Enter Google Script URL

//-----------------------------------------

//-----------------------------------------
#define WIFI_SSID "****"  //Enter WiFi Name
#define WIFI_PASSWORD "****"  //Enter WiFi Password
//-----------------------------------------

//Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);  //Change LCD Address to 0x27 if 0x3F doesnt work


/****************************************************************************************************
 * setup() function
 ****************************************************************************************************/
void setup()
{
  //--------------------------------------------------
  /* Initialize serial communications with the PC */
  Serial.begin(9600);
  //Serial.setDebugOutput(true);

  lcd.begin();
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("   Yukleniyor   ");
  for (int a = 5; a <= 10; a++) {
    lcd.setCursor(a, 1);
    lcd.print(".");
    delay(500);
  }
  
  //--------------------------------------------------
  //WiFi Connectivity
  Serial.println();
  Serial.print("Connecting to AP");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED){
    lcd.clear();
    lcd.print("Wifi Baglaniyor!");
    for (int a = 5; a <= 10; a++) {
    lcd.setCursor(a, 1);
    lcd.print(".");
    delay(500);
    }
    lcd.clear();
    Serial.print(".");
    delay(200);
  }
///////////////////////////////////////////////////Arduino OTA
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else {  // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
////////////////////////////////////////////////////////

//////////////////////////////FIREBASE
  config.api_key = API_KEY;
  config.database_url = DATABASE_URL;
  if (Firebase.signUp(&config, &auth, "", "")) {
    Serial.println("ok");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Sunucu Baglandi!");
    delay(500);
    lcd.clear();
  }else{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("  VERITABANINA  ");
    lcd.setCursor(0, 1);
    lcd.print(" BAGLANILAMADI!");
    delay(2000);
    lcd.clear();
  }
  config.token_status_callback = tokenStatusCallback;
  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
//////////////////////////////////////////////////
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  //--------------------------------------------------
  /* Set BUZZER as OUTPUT */
  pinMode(BUZZER, OUTPUT);
  //--------------------------------------------------
  /* Initialize SPI bus */
  SPI.begin();
  //--------------------------------------------------
  while (!dateTime.valid) {
    dateTime = NTPch.getNTPtime(3.0, 0);
  }
}




/****************************************************************************************************
 * loop() function
 ****************************************************************************************************/
 void loop()
{
  ArduinoOTA.handle();
  //--------------------------------------------------
  if ((millis() - sonSenkronSaati > 30000 || sonSenkronSaati == 0 || kayitDurum == 1)){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  MUNTES MUH.  ");
  saatsenkron();
  lcd.setCursor(0,1);
  lcd.print("  "+tamsaat +"  "+ tamgun);
  kayitDurum = 0;
  }
  /* Initialize MFRC522 Module */
  mfrc522.PCD_Init();
  /* Look for new cards */
  /* Reset the loop if no new card is present on RC522 Reader */
  if ( ! mfrc522.PICC_IsNewCardPresent()) {return;}
  /* Select one of the cards */
  if ( ! mfrc522.PICC_ReadCardSerial()) {return;}
  /* Read data from the same block */
  //--------------------------------------------------
  Serial.println();
  Serial.println(F("Reading last data from RFID..."));
  ReadDataFromBlock(blockNum, readBlockData);
  /* If you want to print the full memory dump, uncomment the next line */
  //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  
  /* Print the data read from block */
  Serial.println();
  Serial.print(F("Last data in RFID:"));
  Serial.print(blockNum);
  Serial.print(F(" --> "));
  //for (int j=0 ; j<16 ; j++)
  {
    //Serial.write(readBlockData[j]);
    //Firebase.getString(fbdo, "/"+String((char*)readBlockData));
    //kullanici = fbdo.stringData();
    //kullanici.trim();
    //Serial.println(kullanici+"bu");
    //lcd.clear();
    //lcd.setCursor(0, 0);
    //lcd.print("--> " + kullanici + "!");

    //Serial.print(String((char*)readBlockData));
    //Serial.print("!");
  }
  id_number="/id_number/"+String((char*)readBlockData);
  Serial.println(id_number);
  if(status != MFRC522::STATUS_OK){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" KART OKUNAMADI");
    lcd.setCursor(0, 1);
    lcd.print("  TEKRAR DENE");
    kayitDurum=1;
    delay(2000);
    return;
  }
  if (Firebase.ready() && status == MFRC522::STATUS_OK){
    if(Firebase.getString(fbdo, id_number)){
    kullanici = fbdo.stringData();
    kullanici.trim();
    Serial.println(kullanici);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("--> " + kullanici + "!");
    Serial.print(fbdo.errorReason().c_str());
    Serial.println();
    }
    else{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("   KULLANICI   ");
    lcd.setCursor(0, 1);
    lcd.print("   BULUNAMADI   ");
    Serial.print(fbdo.errorReason().c_str());
    kayitDurum=1;
    delay(2000);
    return;
    }
  }
  
  //--------------------------------------------------
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  delay(200);
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  //--------------------------------------------------
  
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  if (WiFi.status() == WL_CONNECTED) {
    //-------------------------------------------------------------------------------
    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
    //-------------------------------------------------------------------------------
    //client->setFingerprint(fingerprint);
    // Or, if you want to ignore the SSL certificate
    //then use the following line instead:
    client->setInsecure();
    //-----------------------------------------------------------------
    card_holder_name = sheet_url + String(kullanici);
    card_holder_name.trim();
    Serial.println(card_holder_name);

    //-----------------------------------------------------------------
    HTTPClient https;
    Serial.print(F("[HTTPS] begin...\n"));
    //-----------------------------------------------------------------

    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    if (https.begin(*client, (String)card_holder_name)){
      //-----------------------------------------------------------------
      // HTTP
      Serial.print(F("[HTTPS] GET...\n"));
      // start connection and send HTTP header
      int httpCode = https.GET();
      //-----------------------------------------------------------------
      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
        // file found at server
    lcd.setCursor(0, 1);
    lcd.print(" Data Recorded ");
    delay(2000);
      }
      //-----------------------------------------------------------------
      else 
      {Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());}
      //-----------------------------------------------------------------
      https.end();
      delay(1000);
    }
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else {
      Serial.printf("[HTTPS} Unable to connect\n");
    }
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  }
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
}






/****************************************************************************************************
 * ReadDataFromBlock() function
 ****************************************************************************************************/
void ReadDataFromBlock(int blockNum, byte readBlockData[]) 
{ 
  //----------------------------------------------------------------------------
  /* Prepare the ksy for authentication */
  /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  //----------------------------------------------------------------------------
  /* Authenticating the desired data block for Read access using Key A */
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
  //----------------------------------------------------------------------------s
  if (status != MFRC522::STATUS_OK){
     Serial.print("Authentication failed for Read: ");
     Serial.println(mfrc522.GetStatusCodeName(status));
     return;
  }
  //----------------------------------------------------------------------------
  else {
    Serial.println("Authentication success");
  }
  //----------------------------------------------------------------------------
  /* Reading data from the Block */
  status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
  if (status != MFRC522::STATUS_OK) {
    Serial.print("Reading failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  //----------------------------------------------------------------------------
  else {
    Serial.println("Block was read successfully");  
  }
  //----------------------------------------------------------------------------
}




void saatsenkron() {
  
  do {  //doğru bilgiyi alana kadar dön
    dateTime = NTPch.getNTPtime(3.0, 0);
    
  } while (!dateTime.valid);
  

  //doğru bilgi gelmişse değişkenleri güncelle
  if (dateTime.valid) {
    actualHour = dateTime.hour;
    actualMinute = dateTime.minute;
    actualSecond = dateTime.second;
    actualyear = dateTime.year;
    actualMonth = dateTime.month;
    actualday = dateTime.day;
    sonSenkronSaati = millis();
    /////////////////////////Saat Yazdırma
    if (actualHour < 10) {
    tamsaat = "0" + String(actualHour);
    }
    else {
    tamsaat = String(actualHour);
    }
    if (actualMinute < 10) {
    tamsaat = tamsaat + ":" + "0" + String(actualMinute);
    }
    else {
    tamsaat = tamsaat + ":" + String(actualMinute);
    }
    /////////////////////////Gün Yazdırma
    if (actualday < 10) {
    tamgun = "0" + String(actualday);
    }
    else {
    tamgun = String(actualday);
    }
    if (actualMonth < 10) {
    tamgun = tamgun + "." + "0" + String(actualMonth);
    }
    else {
    tamgun = tamgun + "." + String(actualMonth);
    }
  }
  //tarihyazdir();
  //gunyazdir();

//DEBUG için açabilirsiniz.
//  Serial.print("senkron: ");
//  Serial.print(actualHour);
//  Serial.print(":");
//  Serial.print(actualMinute);
//  Serial.print(":");
//  Serial.print(actualsecond);
//  Serial.println();

}

This is the error.

[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... failed, error: connection failed

I cant find solution

Can anyone help me? When i deleted Firebase, code works well.

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