🎉 Black Friday is Here! Enjoy Up to 35% Off ~ Offer Ends November 30th!
In this build, we'll explore how to configure the CodeCell's ESP32-C3 to use Google's Gemini AI for Arduino prompt-based interactions. You'll learn how to send a prompt via the Serial Monitor and, in a second example, how the CodeCell can automatically trigger a joke based on proximity sensing. This project is ideal for anyone looking to add AI capabilities to their IoT projects.
In this example, we use Google’s Gemini model for generating content based on user input or sensor data. Through out this tutorial we will use and modify the code example made by 'techiesms' - Watch the full tutorial here.
With the ESP32-C3's WiFi capabilities, you can make HTTP requests to Google’s Gemini API, allowing real-time interaction with the AI. Whether you’re asking for text responses or generating creative outputs like jokes, this integration is straightforward to implement.
In the first example, you’ll send prompts directly via the Serial Monitor, and the CodeCell will send this input to Google Gemini AI for processing. The AI's response is printed back to the Serial Monitor, limited by 100 tokens. In the second example, the CodeCell’s proximity sensor will trigger a prompt to the AI, asking it to generate a joke when it detects an object. This setup can be used for fun interactive projects where the device responds to its environment using AI-based content.
Before we integrate the Gemini AI into our ESP32-C3 setup, we first need to generate an API key and test it. Follow the steps below to create your API key and then you can also test it using a software like Postman.
Now that we have the API key, we can test it using the Postman application. Postman is a free tool that allows you to make HTTP requests and see the responses.
https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY
Once you’ve entered the URL, we need to set up the request headers and body.
{
"contents": [
{
"parts": [
{
"text": "Who are you?"
}
]
}
],
"generationConfig": {
"maxOutputTokens": 100
}
}
In this example, we are asking the AI a simple question: "Who are you?" and setting the maximum number of tokens to 100. Tokens control the length of the response generated by the AI. If you lower the token limit (e.g., 20 tokens), the response will be shorter. You can experiment with different values for maxOutputTokens
to see how it affects the response length.
"I am a large language model trained by Google."
Once you've generated and verified that the API works, you can proceed with the next step: integrating this API into your CodeCell project.
Below is the example code to get you started. In this example, the AI will respond to text prompts you send via the Serial Monitor. Remember to replace the placeholders with your WiFi credentials and Gemini API token.
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <CodeCell.h>
CodeCell myCodeCell;
const char* ssid = "SSID"; //Enter your SSID
const char* password = "PASSWORD"; //Enter your password
const char* Gemini_Token = ""; //Enter your Gemini token
const char* Gemini_Max_Tokens = "100";
String res = "";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
while (!Serial);
// Wait for WiFi connection
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
while (!Serial.available());
while (Serial.available()) {
char add = Serial.read();
res += add;
delay(1);
}
int len = res.length();
res = res.substring(0, len - 1);
res = "\"" + res + "\"";
HTTPClient https;
if (https.begin("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + String(Gemini_Token))) {
https.addHeader("Content-Type", "application/json");
String payload = "{\"contents\": [{\"parts\":[{\"text\":" + res + "}]}],\"generationConfig\": {\"maxOutputTokens\": " + String(Gemini_Max_Tokens) + "}}";
int httpCode = https.POST(payload);
if (httpCode == HTTP_CODE_OK) {
String response = https.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
String answer = doc["candidates"][0]["content"]["parts"][0]["text"];
answer.trim();
Serial.println(answer);
} else {
Serial.printf("[HTTPS] POST failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
res = "";
}
This example uses the CodeCell's proximity sensor to trigger a prompt when an object is detected nearby. The AI will respond with a joke.
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <CodeCell.h>
CodeCell myCodeCell;
const char* ssid = "SSID"; //Enter your SSID
const char* password = "PASSWORD"; //Enter your password
const char* Gemini_Token = ""; //Enter your Gemini token
const char* Gemini_Max_Tokens = "100";
String res = "";
void setup() {
Serial.begin(115200);
myCodeCell.Init(LIGHT); // Initializes proximity sensing
WiFi.mode(WIFI_STA);
WiFi.disconnect();
while (!Serial);
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (myCodeCell.Run()) {
uint16_t proximity = myCodeCell.Light_ProximityRead();
if (proximity > 100) {
Serial.println("Here's a new joke...");
myCodeCell.LED(0, 0xFF, 0); // Set LED to Green when proximity is detected
res = "\"Tell me a unique joke\"";
HTTPClient https;
if (https.begin("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + String(Gemini_Token))) {
https.addHeader("Content-Type", "application/json");
String payload = "{\"contents\": [{\"parts\":[{\"text\":" + res + "}]}],\"generationConfig\": {\"maxOutputTokens\": " + String(Gemini_Max_Tokens) + "}}";
int httpCode = https.POST(payload);
if (httpCode == HTTP_CODE_OK) {
String response = https.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
String answer = doc["candidates"][0]["content"]["parts"][0]["text"];
answer.trim();
Serial.println(answer);
} else {
Serial.printf("[HTTPS] POST failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
res = "";
}
}
}
This project showcases how to integrate AI responses into your CodeCell projects using Google’s Gemini API. By leveraging the ESP32-C3’s WiFi capabilities, you can create interactive devices that react to user input or environmental factors, making your IoT builds smarter and more engaging.
Experiment with the code and customize the prompts to suit your projects!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.