#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String token = "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";
String servidor = "https://api.openai.com/v1/chat/completions";
String payload = "{\"model\":\"gpt-3.5-turbo\",\"messages\":\[{\"role\":\"user\",\"content\":\"Hola\"}]}";
/*String servidor = "https://reqres.in/api/users";
String payload = "{\"name\":\"morpheus\",\"job\":\"leader\"}";*/
/*String servidor = "https://api.openai.com/v1/completions";
String payload = "{\"model\":\"text-davinci-003\",\"prompt\":\"Say this is a test\"}";*/
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Conectando...");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Se ha conectado al WiFi con la IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
if(WiFi.status() == WL_CONNECTED) {
HTTPClient httpClient;
httpClient.begin(servidor);
httpClient.addHeader("Content-Type", "application/json");
httpClient.addHeader("Authorization", "Bearer " + token);
Serial.println(payload);
int httpResponseCode = httpClient.POST(payload);
if (httpResponseCode > 0) {
String payload = httpClient.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println(payload);
Serial.println("Conectado a la API correctamente");
delay(1000);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
httpClient.end();
} else {
Serial.println("WiFi Desconectado");
}
delay(10000);
}