SSE (Server Sided Events) with HTTPS connection

Hi all,

For a project I want to use SSE (Server Sided Events).
But the server requires me to use HTTPS. I have never done this before on an ESP32 (in PlatformIO).

I have everything working on my PC via cURL. Because with the following command I create an SSE connection and everything works:

curl --insecure -N -H "hue-application-key: abcMM1P83mBl10cgS3m4JCbWFbTE7hlRJ5M2c" -H "Accept: text/event-stream" https://192.168.1.2/eventstream/clip/v2

I had ChatGPT “translate” the above line to code for my ESP32, and with some input of my own, I then have this code:

void HueSSE(void * parameter){  

  String URL = "https://" + Hue_IP + "/eventstream/clip/v2";

  SendUDP("prodedure.begin...");

  WiFiClientSecure client;
  client.setInsecure(); // Not using certificate check while testing

  HTTPClient https;
  https.useHTTP10(true);
  SendUDP("https.begin...");
  if (https.begin(client, URL)) {  // HTTPS   "https://httpbin.org/get"
    SendUDP("Sending GET request...");
    https.addHeader("Accept", "text/event-stream");
    https.addHeader("hue-application-key", Hue_User_Name); // Add Authorization header
    int httpCode=https.GET();
    SendUDP(String(httpCode));

     if(httpCode == HTTP_CODE_OK) {
                int len = https.getSize();
                uint8_t buff[128] = { 0 };
                WiFiClient * stream = https.getStreamPtr();

                while(https.connected() && (len > 0 || len == -1)) {
                    size_t size = stream->available();

                    if(size) {
                        int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
                        char* pointer = (char*) buff;

                        SendUDP(pointer);

                        if(len > 0) {
                            len -= c;
                        }
                    }
                    delay(1);
                }
            }

    SendUDP(https.getString());

    https.end();
  }else{
    SendUDP("Could not connect to server");
  }

}

The problem is, however, that it doesn’t work. I have also tried other online examples and “ChatGPT solutions”, but with none of them I get this working…

Who knows a solution? :grinning:

Thanks in advance for thinking with me!

Regards,

vanlumig

Hmzzz, it looks like it has more to do with the method of invocation. Namely, I want to start this code from a Task so that the “main program” just keeps running.

I do it this way:

void CreatHueSSETask(){

BaseType_t xReturned;

 xReturned = xTaskCreate(
    HueSSE,    // Function that should be called
    "HueSSE",   // Name of the task (for debugging)
    200000,           // Stack size (bytes)  this were 50000 before
    NULL,            // Parameter to pass
    1,               // Task priority
    &xHandle         // Task handle
  );

}

But if I just call the code directly without a Task, the code works, but then it doesn’t run through the rest of my code.

What could be a solution for this?

Are HTTPS connections not possible in Tasks or so? :roll_eyes: