ESP32 - https Get with timestamp and some met. data with unofficel ssl certificate

Hi,

i try to post some messured data (BME280 data and wind data) from an ESP32 to my server/mysql DB. The following code is working in the browser:

(https://my.domain.com/getdata-gc1.php?location_id=1&writepwd=xxxx&timestamp=2023-03-29%2019:52:23&temperature=30&humidity=14&pressure=1005.21&WindSpeedAvgMS=13&WindSpeedMinMS=12&WindSpeedMaxMS=16&windDirectionDegrees=20&windDirectionText=NNO)

My Arduino-Code to post is this:

#include <ArduinoJson.h>
#include <WebServer.h>
#include <TaskScheduler.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <WiFiClientSecure.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <time.h>

const char* host = "https://my.domain.com";
const int Port = 443;
// SSL certificate of the website
const char* certificate = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIFxzCCA6+gAwIBAgIUEu+oOFvUEoN1aaDtIW/KXIfqa3cwDQYJKoZIhvcNAQEL\n" \

and so one...
"xPATYnW+vRCH/BglQFRsnaNmDrXbziR9aU1MD08dY9gXQCTBWOEeybCa6w==\n" \
"-----END CERTIFICATE-----\n";

void sendData() {
  // Get the current timestamp
  // const char* location_id = LOCATIONID;
  // const char* write_pwd = WRITEPWD;
  time_t now;
  struct tm timeinfo;
  char buffer[20];
  time(&now);
  localtime_r(&now, &timeinfo);
  strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
  String timestamp = buffer;

  // Send the data to the PHP script
 WiFiClientSecure client;
  client.setCACert(certificate);
  if (!client.connect(host, Port)) {
	Serial.print("Connection failed!");
    return;
  }
    
 
  String url = "getdata-gc1.php?location_id="
      + LOCATIONID
      + "&writepwd=" + WRITEPWD
      + "&timestamp=" + String(timestamp)
      + "&temperature=" + String(bmeTemp) 
      + "&humidity=" + String(bmeHum) 
      + "&Pressure=" + String(bmePress) 
      + "&WindSpeedAvgMS=" + String(minuteWindSpeedAvgMS) 
      + "&WindSpeedMinMS=" + String(minuteWindSpeedMinMS) 
      + "&WindSpeedMaxMS=" + String(minuteWindSpeedMaxMS)
      + "&windDirectionDegrees=" + String(directionMostCalculated) 
      + "&windDirectionText=" + String(getWindDirectionDescriptionFromDegree(directionMostCalculated));
  
  client.print(String("GET ") 
  		+ url + " HTTP/1.1\r\n"
     	+ "Host: " + host + "\r\n" 
			+ "Connection: close\r\n\r\n");
  
  // Wait for the response
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      break;
    }
  }
  
  // Print the response
  String response = client.readString();
  Serial.println(response);
  
  // Wait for 10 seconds before sending the next data
  delay(10000);
}

its not possible to get a ssl connection... the certificate i made on my server and in the browser it is mentioned that its no a valid certificate of an official authority - sure.... but i was thinking if i integrate it here in the code is used to encrypt the data transfer and thats the only need....

I also tried the above code without SSL encryption... and its was also not working... i think due to the "Space" in the timestamp... but when i try to substitute i with "%20" it was also ot working... maybe someone has done something like this already and has some hints...

nobody here who has an idea ???

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