A minimal, fast, Arduino‑friendly library for calling Google Gemini models from ESP32 boards — optimized for ESP32‑S3 stability, TLS performance, and clean JSON parsing.
AskGemini is designed for:
- Voice assistants
- Embedded AI devices
- Robotics
- IoT dashboards
- Creative LLM experiments on microcontrollers
- Simple one‑call API:
String reply = askGemini(prompt, instruction, temperature); - Works with Gemini 2.0 Flash and other text‑capable models
- Persistent TLS client for fast HTTPS on ESP32‑S3
- Clean JSON extraction (no heavy JSON libraries)
- Optional text sanitizer for TTS engines
- Three polished examples:
- BasicUsage
- InstructionMode
- GrammarCorrector
ESP32 Arduino Core 3.x (Required)
AskGemini uses the modern HTTPS API:
- WiFiClientSecure
- client.setInsecure()
- http.begin(client, url)
These functions do not exist in ESP32 Core 2.x.
- ESP32‑S3
- ESP32‑S2
- ESP32‑C3
- ESP32 classic (running Arduino Core 3.x)
- ESP32 Arduino Core 2.x
- Boards without HTTPS capability
- Install ESP32 Arduino Core 3.x
Arduino IDE → Boards Manager → search “esp32” → install 3.x.x - Download or clone this repository
- Place the folder into:
Documents/Arduino/libraries/AskGemini - Restart Arduino IDE
- Open: File → Examples → AskGemini
String Gemini_APIKey = "YOUR_API_KEY";
String Gemini_Model = "gemini-2.0-flash";
void errorHandler(int code) {
Serial.printf("AskGemini error: %d\n", code);
}
String reply = askGemini( "Tell me a fun fact about space.", "Respond with one concise sentence.", 0.2 ); Serial.println(reply);
String concise = askGemini( "What is a microcontroller?", "Respond with one short, clear sentence.", 0.1 );
String friendly = askGemini( "What is a microcontroller?", "Respond in a friendly tone suitable for beginners.", 0.4 );
String technical = askGemini( "What is a microcontroller?", "Respond with a technical description suitable for engineers.", 0.0 );
String reply = askGemini( "I did not see nuthen.", "You are a grammar-correcting assistant. Return only the corrected sentence.", 0.0 );
char* cleaned = sanitizeQuip(reply.c_str()); Serial.println(cleaned); free(cleaned); // Important: avoid memory leaks
sanitizeQuip() allocates memory using malloc(), so you must call free() after use.
The ESP32‑S3’s TLS stack is slower than classic ESP32. AskGemini includes several optimizations:
- Persistent WiFiClientSecure (avoids repeated TLS handshakes)
- Keep‑alive enabled
- http.setReuse(true) for connection reuse
- Efficient JSON extraction
- 20‑second read timeout for long responses
- Use shorter instructions
- Use smaller models (gemini-2.0-flash-lite)
- Limit output tokens (e.g., 16–64)
- Add small delays between back‑to‑back calls
- Fix: Updated AskGemini.cpp / AskGemini.h to stable Gemini 2.0 Flash implementation.
- Fix: Improved memory stability and HTTP path handling.
- Internal: Cleaned logging and clarified error behavior.
MIT License.
See LICENSE.txt for details.
Created by William E. Webb