The circuit in question appears to be a smart device that utilizes an ESP32-C3 microcontroller to interface with various peripherals including an OLED display, a pushbutton, multiple red LEDs, a breadboard power supply module, and potentially a camera module. The ESP32-C3 is responsible for handling Wi-Fi connectivity, user input via the pushbutton, and displaying information on the OLED screen. The red LEDs serve as indicators, likely for status or output from the device's operations.
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_PIN D6
#define INDICATOR_LED D5
#define LED_A D0
#define LED_B D1
#define LED_C D2
#define LED_D D3
#define LED_E D4
#define I2C_SCL D10
#define I2C_SDA D9
const char* ssid = "ssid";
const char* password = "wifi password";
const char* api_endpoint = "https://api.openai.com/v1/chat/completions";
const char* api_key = "API KEY";
void setup() {
// Initialize Serial, Wi-Fi, OLED, and pins
Serial.begin(115200);
Wire.begin(I2C_SDA, I2C_SCL);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust address if needed
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
pinMode(INDICATOR_LED, OUTPUT);
pinMode(LED_A, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(LED_C, OUTPUT);
pinMode(LED_D, OUTPUT);
pinMode(LED_E, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Connecting to Wi-Fi
display.clearDisplay();
display.setCursor(0, 0);
display.print("Connecting to WiFi");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.print(".");
display.display();
}
Serial.println("Connected to Wi-Fi");
display.clearDisplay();
display.setCursor(0, 0);
display.print("Ready for Input");
display.display();
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(INDICATOR_LED, HIGH); // Turn on indicator LED
// Display phase: Capture image
display.clearDisplay();
display.setCursor(0, 0);
display.print("Capturing Image...");
display.display();
// Capture and send image, then get answer
String answer = captureAndSendImage();
// Display answer phase
display.clearDisplay();
display.setCursor(0, 0);
display.print("Answer: ");
display.setCursor(8, 0);
display.print(answer);
display.display();
// Light up corresponding answer LED
resetLEDs();
if (answer == "A") digitalWrite(LED_A, HIGH);
else if (answer == "B") digitalWrite(LED_B, HIGH);
else if (answer == "C") digitalWrite(LED_C, HIGH);
else if (answer == "D") digitalWrite(LED_D, HIGH);
else if (answer == "E") digitalWrite(LED_E, HIGH);
// Display phase: Process complete
display.setCursor(0, 1);
display.print("Process Complete");
display.display();
delay(1000); // Keep LEDs on for a second
digitalWrite(INDICATOR_LED, LOW); // Turn off indicator LED
// Reset screen after a delay
delay(2000);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Ready for Input");
display.display();
}
}
void resetLEDs() {
digitalWrite(LED_A, LOW);
digitalWrite(LED_B, LOW);
digitalWrite(LED_C, LOW);
digitalWrite(LED_D, LOW);
digitalWrite(LED_E, LOW);
}
String captureAndSendImage() {
// Display phase: Sending to ChatGPT
display.clearDisplay();
display.setCursor(0, 0);
display.print("Sending to ChatGPT");
display.display();
HTTPClient http;
http.begin(api_endpoint);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", String("Bearer ") + api_key);
// Placeholder for actual image data - replace <encoded_image_data> with real data
String payload = "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"Analyze the image and respond with one of the options: A, B, C, D, or E.\"}]}";
int httpResponseCode = http.POST(payload);
String answer;
if (httpResponseCode > 0) {
String response = http.getString();
answer = parseAnswer(response); // Extract the answer from the response
} else {
Serial.println("Error in HTTP request");
answer = "Error"; // Default in case of error
display.setCursor(0, 1);
display.print("Error: No Response");
display.display();
}
http.end();
return answer;
}
String parseAnswer(String response) {
// Parse JSON response to extract "A", "B", "C", "D", or "E"
int start = response.indexOf(":") + 2;
int end = response.indexOf("}") - 1;
return response.substring