B4R Question rHttpUtils2 SSL problem

GaryDUK

Member
Hi all,

My goal is to be able to post data to a website using an ESP32 so I thought I would start at the basics & downloads Erel's example code at https://www.b4x.com/android/forum/threads/module-rhttputils2-http-client.74785/

I've spent the past couple of days trying to get Erel's original code working but with no joy
The error message I'm getting is 'ErrorMessage: Failed to connect'

With the debuglevel=debug the log I'm getting is below. I'm suspecting it's something to do with the SSL side

============ After Setup End =============
connected: 1
trying to connect to: example.com port: 443 ssl: 1
[ 3212][D][NetworkManager.cpp:83] hostByName(): Clearing DNS cache
[ 3225][E][NetworkManager.cpp:130] hostByName(): DNS Failed for 'example.com' with error '-54'
[ 3233][NetworkClientSecure.cpp:153] connect(): Actual TLS start postponed.
[ 3240][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
*******************************
JobName: Example
ErrorMessage: Failed to connect
Status: 0

I'm just going around in circles! so any help greatly appreciated.
 

Gerardo Tenreiro

Active Member
Licensed User
Hello,
I'm in the same situation, and I've managed to get it to connect if instead of "HTTS" and port 443 I use "HTTP" and port 80.

I'll leave you the code that I'm trying and it connects, the rest I haven't got yet, I'm working on it.
The intention is to upload a file from the ESP32

#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 2000
#End Region

Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Public Fichero_P As String = "Parametros.CSV"

Dim bc As ByteConverter

End Sub

Private Sub AppStart
Serial1.Initialize(115200)
Log("****************************")
Log("Inicia Aplicacion")
Conecta_Wifi(0)
End Sub

Sub Conecta_Wifi(Nada As Byte)
wifi.ConnectAsync("", "", 0, Null, "wifi_Connected")
End Sub

Sub WiFi_Connected (Success As Boolean)
If Success = True Then
Log("Conectado !!!")
Else
Log("No conectado, se reintenta")
wifi.Disconnect
CallSubPlus("Conecta_Wifi",5000,0)
End If

If Success = True Then
HttpJob.Initialize("Paso_000")
HttpJob.Download("http://ota-puerta.000webhostapp.com/")
End If
End Sub


Sub JobDone (Job As JobResult)
Log("*******************************")
Log("Nombre del Paso : ", Job.JobName)



If Job.Success Then

Log("Response: ", bc.SubString2(Job.Response, 0, Min(20, Job.Response.Length))) ' Limita a 20 Caracteres

If Job.JobName = "Paso_000" Then
HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
HttpJob.Post("http://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")

HttpJob.Initialize("Paso_001") ' Envia Fichero Parametros

End If


Else
Log("ErrorMessage: ", Job.ErrorMessage)
Log("Status: ", Job.Status)
Log(Job.Response)
End If
End Sub
 
Upvote 0

GaryDUK

Member
Hello,
I'm in the same situation, and I've managed to get it to connect if instead of "HTTS" and port 443 I use "HTTP" and port 80.

I'll leave you the code that I'm trying and it connects, the rest I haven't got yet, I'm working on it.
The intention is to upload a file from the ESP32
Hi,

I think changing to HTTP & using port 80 would work for you as your server will except both but my server will only except HTTPS on port 443.
I've tried everything I can think of now & still no joy even with Erel's example code and just changing the SSID & Pasword! It seems to point to an SSL issue.
 
Upvote 0

GaryDUK

Member
Thanks for the reply, Would this be the caes with all ESP32 or just the version I have do you think? It seemed to work at some time on this post which makes me think its something my end that is wrong!

If I flash it with some example Arduino code although its a different URL but still HTTPS it works fine. The only difference I can see is the Arduino code is using a certificate. If I remove the certificate I get the same error. It appears to be something to do with the certificate. Arduino example below.

Arduino SSL code:
/**
   BasicHTTPSClient.ino

    Created on: 14.10.2018

*/

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#include <NetworkClientSecure.h>

// This is a Baltimore CyberTrust cert, the root Certificate Authority that
// signed the server certificate for the demo server https://jigsaw.w3.org in this
// example. This certificate is valid until Mon, 12 May 2025 23:59:00 GMT
const char *rootCACertificate = "-----BEGIN CERTIFICATE-----\n"
                                "MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\n"
                                "RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\n"
                                "VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\n"
                                "DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\n"
                                "ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\n"
                                "VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\n"
                                "mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\n"
                                "IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\n"
                                "mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\n"
                                "XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\n"
                                "dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\n"
                                "jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\n"
                                "BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\n"
                                "DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\n"
                                "9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\n"
                                "jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\n"
                                "Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\n"
                                "ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\n"
                                "R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\n"
                                "-----END CERTIFICATE-----\n";

// Not sure if NetworkClientSecure checks the validity date of the certificate.
// Setting clock just to be sure...
void setClock() {
  configTime(0, 0, "pool.ntp.org");

  Serial.print(F("Waiting for NTP time sync: "));
  time_t nowSecs = time(nullptr);
  while (nowSecs < 8 * 3600 * 2) {
    delay(500);
    Serial.print(F("."));
    yield();
    nowSecs = time(nullptr);
  }

  Serial.println();
  struct tm timeinfo;
  gmtime_r(&nowSecs, &timeinfo);
  Serial.print(F("Current time: "));
  Serial.print(asctime(&timeinfo));
}

WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
   Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("BrokerWIFI", "uAwgtPHMixq9");

  // wait for WiFi connection
  Serial.print("Waiting for WiFi to connect...");
  while ((WiFiMulti.run() != WL_CONNECTED)) {
    Serial.print(".");
  }
  Serial.println(" connected");

  setClock();
}

void loop() {
  NetworkClientSecure *client = new NetworkClientSecure;
  if (client) {
   client->setCACert(rootCACertificate);

    {
      // Add a scoping block for HTTPClient https to make sure it is destroyed before NetworkClientSecure *client is
      HTTPClient https;

      Serial.print("[HTTPS] begin...\n");
      if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) {  // HTTPS

        Serial.print("[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
          if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            String payload = https.getString();
            Serial.println(payload);
          }
        } else {
          Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
        }

        https.end();
      } else {
        Serial.printf("[HTTPS] Unable to connect\n");
      }

      // End extra scoping block
    }

    delete client;
  } else {
    Serial.println("Unable to create client");
  }

  Serial.println();
  Serial.println("Waiting 10s before the next round...");
  delay(10000);
}
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try the attached library (v1.60). In the past the option to discard the certificate verification wasn't available in ESP32. It is available and enabled now in the library.

It works for me with https://www.b4x.com

Make sure to copy library to internal libraries folder.
 

Attachments

  • rESP8266WiFi.zip
    29.4 KB · Views: 49
Upvote 0
Top