128 lines
3.2 KiB
Plaintext
128 lines
3.2 KiB
Plaintext
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
|
|
// OLED config
|
|
#define SCREEN_WIDTH 128
|
|
#define SCREEN_HEIGHT 64
|
|
#define OLED_RESET -1
|
|
#define I2C_SDA 21
|
|
#define I2C_SCL 22
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
|
|
|
// WiFi
|
|
const char* ssid = "your wifi id";
|
|
const char* password = "your wifi password";
|
|
const String gemini_api_key = "your api key";
|
|
|
|
// MAX9814 microphone input pin
|
|
#define MIC_PIN 34
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(1000);
|
|
|
|
Wire.begin(I2C_SDA, I2C_SCL);
|
|
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
|
|
Serial.println("OLED init failed!");
|
|
while (1);
|
|
}
|
|
|
|
displayMessage("Connecting WiFi...");
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
displayMessage("WiFi connected!");
|
|
pinMode(MIC_PIN, INPUT);
|
|
delay(500);
|
|
displayMessage("Ready! Speak loud...");
|
|
}
|
|
|
|
void loop() {
|
|
static unsigned long lastTrigger = 0;
|
|
|
|
int micValue = analogRead(MIC_PIN);
|
|
Serial.println(micValue);
|
|
|
|
if (micValue > 1500 && millis() - lastTrigger > 5000) {
|
|
lastTrigger = millis();
|
|
|
|
displayMessage("Detecting...");
|
|
|
|
String response = askGemini("who are you.");
|
|
displayMultilineText(response);
|
|
}
|
|
|
|
delay(100);
|
|
}
|
|
|
|
void displayMessage(String msg) {
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(SSD1306_WHITE);
|
|
display.setCursor(0, 0);
|
|
display.println(msg);
|
|
display.display();
|
|
}
|
|
|
|
void displayMultilineText(String text) {
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(SSD1306_WHITE);
|
|
display.setCursor(0, 0);
|
|
|
|
int maxCharsPerLine = 21;
|
|
int lineHeight = 10;
|
|
int y = 0;
|
|
|
|
while (text.length() > 0 && y < SCREEN_HEIGHT) {
|
|
String line = text.substring(0, maxCharsPerLine);
|
|
text = text.substring(min((unsigned int)maxCharsPerLine, text.length()));
|
|
display.setCursor(0, y);
|
|
display.println(line);
|
|
y += lineHeight;
|
|
}
|
|
|
|
display.display();
|
|
}
|
|
|
|
// Gemini API call
|
|
String askGemini(String prompt) {
|
|
HTTPClient http;
|
|
String endpoint = "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash-002:generateContent?key=" + gemini_api_key;
|
|
http.begin(endpoint);
|
|
http.addHeader("Content-Type", "application/json");
|
|
|
|
prompt.replace("\"", "\\\"");
|
|
|
|
String requestBody = "{ \"contents\": [ { \"parts\": [ { \"text\": \"" + prompt + "\" } ] } ] }";
|
|
int code = http.POST(requestBody);
|
|
String reply = "";
|
|
|
|
if (code == 200) {
|
|
String payload = http.getString();
|
|
StaticJsonDocument<2048> doc;
|
|
DeserializationError error = deserializeJson(doc, payload);
|
|
|
|
if (!error) {
|
|
reply = doc["candidates"][0]["content"]["parts"][0]["text"].as<String>();
|
|
reply.replace("\\n", "\n");
|
|
} else {
|
|
Serial.println("JSON Error: " + String(error.c_str()));
|
|
reply = "I am Gemini, your AI assistant.";
|
|
}
|
|
} else {
|
|
Serial.println("Gemini API error: " + String(code));
|
|
// Fallback reply when API fails
|
|
reply = "I am Gemini, your AI assistant.";
|
|
}
|
|
|
|
http.end();
|
|
return reply;
|
|
} |