0% found this document useful (0 votes)
3 views

source code

This document is an Arduino sketch that connects to WiFi and sends a POST request to the OpenAI API using an HTTP client. It includes the necessary credentials and constructs a JSON payload to query the ChatGPT model. The program continuously sends requests every 10 seconds and prints the response or any errors encountered during the process.

Uploaded by

Nandhini K.L
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

source code

This document is an Arduino sketch that connects to WiFi and sends a POST request to the OpenAI API using an HTTP client. It includes the necessary credentials and constructs a JSON payload to query the ChatGPT model. The program continuously sends requests every 10 seconds and prints the response or any errors encountered during the process.

Uploaded by

Nandhini K.L
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <WiFi.

h>
#include <HTTPClient.h>

const char* ssid = "nandhu yadav";


const char* password = "nandhu56";
const char* chatgpt_token = "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";
const char* chatgpt_Q = "\"Who are you\"";

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

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\": \"gpt-3.5-turbo-instruct\", \"prompt\": ")


+ chatgpt_Q + String(", \"temperature\": 0, \"max_tokens\": 7}"); // JSON Payload

Serial.print("[HTTPS] POST...\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 response = https.getString();
Serial.println(response);
} else {
Serial.printf("[HTTPS] POST... 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);

You might also like