I was using this code..
#include <Wire.h>
#include <SSD1306Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <PS2Keyboard.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDRESS 0x3C
SSD1306Wire display(OLED_ADDRESS, SDA, SCL);
const char* ssid = "DESKTOP-828D1JN 3582";
const char* password = "2Y04w<42";
const int PS2_DATA_PIN = 19;
const int PS2_IRQ_PIN = 18;
const int LED_PIN = 2;
PS2Keyboard keyboard;
String userQuery;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
if (!display.init()) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.flipScreenVertically();
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "Connecting to Wi-Fi...");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
display.clear();
display.drawString(0, 0, "Connected to WiFi");
display.display();
display.clear();
display.display();
keyboard.begin(PS2_DATA_PIN, PS2_IRQ_PIN);
}
String getChatGPTResponse(String userQuery) {
HTTPClient http;
String apiUrl = "https://api.openai.com/v1/chat/completions";
String apiKey = "*****************************";
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer " + apiKey);
String requestBody = "{\"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},{\"role\": \"user\", \"content\": \"" + userQuery + "\"}]}";
int httpResponseCode = http.POST(requestBody);
String response = http.getString();
http.end();
return response;
}
void loop() {
display.println();
display.println();
display.println("LearnMateAI...");
display.display();
while (keyboard.available()) {
char c = keyboard.read();
if (c == PS2_ENTER || c == 13) {
display.clearDisplay();
display.setCursor(0, 0);
display.println("You asked:");
display.println();
display.print("-> ");
display.print(userQuery);
String chatGPTResponse = getChatGPTResponse(userQuery);
display.println();
display.println();
display.println("LearnMateAI says:");
display.println("-> " + chatGPTResponse);
display.display();
userQuery = "";
} else if (c == PS2_BACKSPACE || c == 8) {
if (userQuery.length() > 0) {
userQuery.remove(userQuery.length() - 1);
}
} else {
userQuery += c;
}
}
}
Successfully uploaded this code but not connecting in Wi-Fi..