I am using this display
with this i2c expander
in esp-wroom-32. I used this code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <PS2Keyboard.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int PS2_DATA_PIN = 19; // Pin connected to the data line of the PS2 keyboard
const int PS2_IRQ_PIN = 18; // Pin connected to the IRQ line of the PS2 keyboard
PS2Keyboard keyboard;
String ssid = "";
String password = "";
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
connectToWiFi();
Wire.begin(SDA, SCL); // Initialize I2C with the specified pins
if (!display.begin()) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display(); // Clear the display
delay(2000); // Pause for 2 seconds
display.clearDisplay();
}
void connectToWiFi() {
pinMode (33, HIGH);
Serial.println("Enter Wi-Fi SSID:");
while (ssid.length() == 0) {
while (keyboard.available()) {
char c = keyboard.read();
if (c == PS2_ENTER) {
Serial.println(ssid);
break;
} else {
ssid += c;
}
}
}
Serial.println("Enter Wi-Fi password:");
while (password.length() == 0) {
while (keyboard.available()) {
char c = keyboard.read();
if (c == PS2_ENTER) {
Serial.println(password);
break;
} else {
password += c;
}
}
}
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Display a welcome message
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 10);
display.println("Welcome to LearnMateAI...!");
display.display();
delay(3000); // Display the welcome message for 3 seconds
display.clearDisplay();
}
String getChatGPTResponse(String userQuery) {
HTTPClient http;
String apiUrl = "https://api.openai.com/v1/chat/completions"; // Update with the correct endpoint
String apiKey = "YOUR_OPENAI_API_KEY"; // Replace with your OpenAI API key
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() {
while (keyboard.available()) {
char c = keyboard.read();
if (c == PS2_ENTER) {
display.clearDisplay();
display.setCursor(0, 0);
display.println("You asked:");
display.println();
display.print("-> ");
display.print(keyboard.read());
// Get response from ChatGPT
String userQuery = "";
while (keyboard.available()) {
userQuery += keyboard.read();
}
String chatGPTResponse = getChatGPTResponse(userQuery);
display.println();
display.println();
display.println("LearnMateAI says:"); // Change this line
display.println("-> " + chatGPTResponse);
display.display();
}
}
}
but my display is not showing anything.
Please help.