//2. Get OpenAI API Key
//Sign up at OpenAI.
//Get your API key from the API Keys section.
//Store the key safely.
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Wi-Fi Credentials
const char* ssid = "YOUR_WIFI_SSID"; //use blynk
const char* password = "YOUR_WIFI_PASSWORD";
// OpenAI API Key
const String OPENAI_API_KEY = "YOUR_OPENAI_API_KEY";
// OpenAI API Endpoint
const char* endpoint = "https://api.openai.com/v1/chat/completions";
// Function to send a request to OpenAI
String askOpenAI(String prompt) {
HTTPClient http;
http.begin(endpoint);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer " + OPENAI_API_KEY);
String requestBody = "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}]}";
int httpResponseCode = http.POST(requestBody);
String response = "";
if (httpResponseCode > 0) {
response = http.getString();
} else {
response = "Error: " + String(httpResponseCode);
}
http.end();
// Extract message from JSON
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
String reply = doc["choices"][0]["message"]["content"];
return reply;
}
void setup() {
Serial.begin(115200);
// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
while (1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
display.setCursor(0, 0);
display.println("Connecting...");
display.display();
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connected!");
display.display();
delay(2000);
}
void loop() {
String userInput = "Hello, who are you?"; // You can modify this to get input dynamically
String response = askOpenAI(userInput);
// Display response on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println(response);
display.display();
Serial.println("AI Response: " + response);
delay(10000); // Wait before next request
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK