#include <WiFi.h> // WiFi library for GIGA R1
#include <HTTPClient.h> // For API requests
#include <Adafruit_GFX.h> // Graphics library
#include <Adafruit_ST7735.h> // ST7735 library
#include <SPI.h> // SPI for LCD
#include <USBHostGiga.h> // USB host library for keyboard
// ST7735 pin definitions
#define TFT_CS 10 // PB10
#define TFT_RST 9 // PB9
#define TFT_DC 8 // PB8
#define TFT_MOSI 15 // PB15 (SPI1 MOSI)
#define TFT_SCLK 13 // PB13 (SPI1 SCK)
// Initialize ST7735 LCD
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
// USB keyboard setup
USBHostGiga usbHost;
KeyboardController keyboard(usbHost);
String inputString = "";
String ssid = "";
String password = "";
bool enteringSSID = true;
bool enteringPassword = false;
bool wifiConnected = false;
// OpenAI API key
const char* apiKey = "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";
// ChatGPT API endpoint
const char* chatGPTUrl = "https://api.openai.com/v1/chat/completions";
void setup() {
Serial.begin(115200);
// Initialize LCD
tft.initR(INITR_BLACKTAB); // Adjust for your ST7735 variant
tft.setRotation(1); // Landscape mode
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(1);
tft.setCursor(0, 0);
tft.print("Enter SSID:");
// Start USB host
usbHost.begin();
}
void loop() {
usbHost.Task(); // Process USB events
if (!wifiConnected) {
handleWiFiSetup();
} else {
if (inputString.length() > 0 && keyboard.getKey() == 13) { // Enter key for ChatGPT prompt
sendToChatGPT(inputString);
inputString = "";
}
}
}
// Keyboard callback
void keyPressed() {
char key = keyboard.getKey();
if (!wifiConnected) {
if (key >= 32 && key <= 126) { // Printable characters
inputString += key;
displayInput();
} else if (key == 13) { // Enter key
if (enteringSSID) {
ssid = inputString;
inputString = "";
enteringSSID = false;
enteringPassword = true;
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("Enter Password:");
} else if (enteringPassword) {
password = inputString;
inputString = "";
enteringPassword = false;
connectToWiFi();
}
}
} else if (key >= 32 && key <= 126) { // ChatGPT input
inputString += key;
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("You: ");
tft.print(inputString);
}
}
void displayInput() {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
if (enteringSSID) {
tft.print("Enter SSID:");
} else if (enteringPassword) {
tft.print("Enter Password:");
}
tft.setCursor(0, 20);
tft.print(inputString);
}
void connectToWiFi() {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("Connecting to ");
tft.print(ssid);
WiFi.begin(ssid.c_str(), password.c_str());
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("WiFi Connected");
wifiConnected = true;
delay(2000);
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("Type a message:");
} else {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("WiFi Failed");
delay(2000);
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("Enter SSID:");
enteringSSID = true;
wifiConnected = false;
}
}
void sendToChatGPT(String prompt) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(chatGPTUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", String("Bearer ") + apiKey);
String payload = "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}], \"max_tokens\": 50}";
int httpCode = http.POST(payload);
if (httpCode > 0) {
String response = http.getString();
String chatResponse = parseChatGPTResponse(response);
displayResponse(chatResponse);
} else {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("HTTP Error: ");
tft.print(httpCode);
}
http.end();
}
}
String parseChatGPTResponse(String json) {
int contentStart = json.indexOf("\"content\":\"") + 11;
int contentEnd = json.indexOf("\"", contentStart);
return json.substring(contentStart, contentEnd);
}
void displayResponse(String response) {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("ChatGPT: ");
int len = response.length();
for (int i = 0; i < len; i += 20) { // Wrap text every 20 chars
tft.setCursor(0, 20 + (i / 20) * 10);
tft.print(response.substring(i, min(i + 20, len)));
}
}
void handleWiFiSetup() {
// Wait for WiFi credentials to be entered
if (!enteringSSID && !enteringPassword && !wifiConnected) {
connectToWiFi();
}
}
error
C:\Users\Nelson\Downloads\sketch_feb26a\sketch_feb26a.ino:2:10: fatal error: HTTPClient.h: No such file or directory
#include <HTTPClient.h> // For API requests
^~~~~~~~~~~~~~
compilation terminated.
exit status 1
Compilation error: HTTPClient.h: No such file or directory