Wio Terminal and HTTPS connection issues (WiFiClientSecure)

Wanted to interface to the new OpenAI API from Wio Terminal which requires https post protocol. I cannot seem to establish a connection to OpenAI. Certificate is from “api.openai.com” which hosts the endpoint. Program executes up until the connect function from WiFiClientSecure is executed and then it just seems to hang with no return value. Code is at:

Thanks for any help!

Hi, we’ve been meaning to do this tutorial for a while, but unfortunately we don’t have the time to do it at the moment. But first we finished the example and tutorial of XIAO ESP32C3 calling OpenAI API, which might be helpful for you.
Web Services Advanced Usage XIAO ESP32C3 And ChatGPT - Seeed Wiki (seeedstudio.com)

Thanks for taking a look, but it’s still not functional. I took the suggested code and modified <#include WiFi.h> to include rpcWiFi.h instead (for the Wio, I guess). This simple program hangs at the Get request. Could be an issue with rpcWiFi? I know my OpenAI key is good and use it from other programs not on the Wio Terminal.

If anyone with a Wio Terminal wants to try this out, the code is below. You’ll need a free OpenID API key, though. Thanks!

/*

  • The simplest demo of Wio Terminal calling OpenAI
    */

#include <Arduino.h>
#include <rpcWiFi.h>
#include <HTTPClient.h>

const char* ssid = “YOUR_SSID”;
const char* password = “YOUR_PASSWORD”;
const char* chatgpt_token = “YOUR_API_KEY”;
const char* chatgpt_Q = ““Who are you””;

void setup() {
Serial.begin(115200);

WiFi.mode(WIFI_STA);
WiFi.disconnect();
while(!Serial);

// wait for WiFi connection
WiFi.begin(ssid, password);
Serial.print(“Connecting to “);
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(”.”);
}
Serial.println(“connected”);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
HTTPClient https;

Serial.print("[HTTPS] begin...\n");
if (https.begin("https://api.openai.com/v1/completions")) {  // HTTPS
  
  https.addHeader("Content-Type", "application/json"); 
  String token_key = String("Bearer ") + chatgpt_token;
  https.addHeader("Authorization", token_key);
  
  String payload = String("{\"model\": \"text-davinci-003\", \"prompt\": ") + chatgpt_Q + String(", \"temperature\": 0, \"max_tokens\": 7}"); //Instead of TEXT as Payload, can be JSON as Paylaod
  
  Serial.print("[HTTPS] GET...\n");
  
  // start connection and send HTTP header
  int httpCode = https.POST(payload);

  // httpCode will be negative on error      
  // 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");
}

Serial.println(“Wait 10s before next round…”);
delay(10000);
}

Is there any HTTP response after the program runs? If there are error messages, this is important and can help us identify the problem.

There are no error codes. The last line I see execute is Serial.print("[HTTPS] GET…\n"); then it just seems to hang. The next line is the int httpCode = https.POST(payload); which doesn’t return and just seems to hang…I have all of the updated and most current libraries. Wifi connection is strong. I am out of ideas.

I bought a XIAO ESP32C3, the hardware this code was originally developed to run on. It will be interesting if it runs there but not on the Wio Terminal.

Hi Citric, I purchased a XIAO ESP32C3 to test the example code you supplied. In the github repository for the project there is a simple program showing accessing OpenAI in a very simple way. The program is at xiaoesp32c3-chatgpt/simplest_xiao-chatgpt.ino at main · limengdu/xiaoesp32c3-chatgpt · GitHub. The program runs great on the new ESP32C3 I purchased, but will not run on my Wio Terminal, even after I changed the WiFi.h library to rpcWiFi.h for the Wio. It will complle with no errors like that, but will not run. It does not give any error messages, it simply seems to run until line 49, where the https POST is called and I never see a return value. Has anyone a working example of an HTTPS POST using the Wio Terminal?

Hi Kevin,
I have some projects using post requests with the Wio-Terminal using PlatformIo IDE.

Best regards
RoSchmi

1 Like