ESP32-CAM SSL error

kahlenberg
Posts: 2
Joined: Fri Jun 30, 2017 2:57 pm

ESP32-CAM SSL error

Postby kahlenberg » Sun May 30, 2021 9:39 pm

Hi,
I have a ESP32-CAM modul and I am trying to use Telegrom bot.
For text messages or "basic" things it is working, it sends message to my mobile phone.
But when I want to take a photo esp32 board says invalid SSL error.
I am also using deep sleep function. My esp32 board first wakes up and cheks telegram if new messages exist. If ther is any new message, it sends the response back. But taking photo with camera isn't working due to SSL error:

Code: Select all

String BOTtoken = "XX--XX--XX";
WiFiClientSecure secureClientTCP;
UniversalTelegramBot bot(BOTtoken, secureClientTCP);

void setup () {
// WiFi settings
// Serial settings
  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  ESP_LOGI(TAG, "Number of messages: %d", numNewMessages);
  while (numNewMessages) {
    ESP_LOGI(TAG, "Got response....%d", numNewMessages);
    handleNewMessages(numNewMessages);
    numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  }

  ESP_LOGI(TAG, "Sleeping..");
  esp_deep_sleep_start();
}

void loop () {}

String sendPhotoTelegram() {
  const char* myDomain = "api.telegram.org";
  String getAll = "";
  String getBody = "";

  camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();
  if (!fb) {
    ESP_LOGE(TAG, "Camera capture failed");
    delay(1000);
    ESP.restart();
    return "Camera capture failed";
  }

  ESP_LOGI(TAG, "Connect to %s\n", myDomain);

  if (secureClientTCP.connect(myDomain, 443)) {
    ESP_LOGI(TAG, "Connection successful");
    uint16_t imageLen = fb->len;

    secureClientTCP.println("POST /bot" + BOTtoken + "/sendPhoto HTTP/1.1");
    secureClientTCP.println("Host: " + String(myDomain));
    secureClientTCP.println("Content-Length: " + String(imageLen));
    secureClientTCP.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
    //secureClientTCP.println("");

    uint8_t *fbBuf = fb->buf;
    size_t fbLen = fb->len;
    for (size_t n = 0; n < fbLen; n = n + 1024) {
      if (n + 1024 < fbLen) {
        secureClientTCP.write(fbBuf, 1024);
        fbBuf += 1024;
      }
      else if (fbLen % 1024 > 0) {
        size_t remainder = fbLen % 1024;
        secureClientTCP.write(fbBuf, remainder);
      }
    }

    esp_camera_fb_return(fb);

    int waitTime = 10000;   // timeout 10 seconds
    long startTimer = millis();
    boolean state = false;

    while ((startTimer + waitTime) > millis()) {
      Serial.print(".");
      delay(100);
      while (secureClientTCP.available()) {
        char c = secureClientTCP.read();

        if (state == true) getBody += String(c);
        if (c == '\n') {
          if (getAll.length() == 0) state = true;
          getAll = "";
        }
        else if (c != '\r')
          getAll += String(c);
        startTimer = millis();
      }
      if (getBody.length() > 0) break;
    }
    secureClientTCP.stop();
    ESP_LOGI(TAG, "%s", getBody.c_str());
  }
  else {
    getBody = "Connection to api.telegram.org failed.";
    ESP_LOGI(TAG, "%s", getBody.c_str());
  }
  return getBody;
}


void handleNewMessages(int numNewMessages) {
  ESP_LOGI(TAG, "Handle New Messages: %d\n", numNewMessages);

  for (int i = 0; i < numNewMessages; i++) {

    // Print the received message
    String text = bot.messages[i].text;
    String fromName = bot.messages[i].from_name;

    if (text == "/photo") {
      ESP_LOGI(TAG, "New photo request. Preparing photo");
      sendPhotoTelegram();
    }
}

Who is online

Users browsing this forum: No registered users and 77 guests