Description
The ESP32_AI_Connect constructor is the primary method for creating and initializing an instance of the ESP32_AI_Connect library. This constructor establishes the connection parameters for communicating with various AI platforms including OpenAI, Claude, DeepSeek, and Gemini. It automatically configures the WiFi client for secure connections and initializes the appropriate platform handler based on the specified platform identifier.
Syntax and Usage
The ESP32_AI_Connect constructor offers two different usage patterns:
1. Standard Constructor
For connecting to official AI platform endpoints:
ESP32_AI_Connect(const char* platformIdentifier, const char* apiKey, const char* modelName)
2. Constructor with Custom Endpoint
For connecting to custom or self-hosted AI endpoints:
ESP32_AI_Connect(const char* platformIdentifier, const char* apiKey, const char* modelName, const char* endpointUrl)
Arguments
1. Required Arguments
platformIdentifier (const char*)
Specifies which AI platform to connect to. This parameter determines which platform handler will be initialized and used for API communications.
apiKey (const char*)
Your API authentication key for the specified platform. This credential is required to authenticate requests with the AI service provider.
modelName (const char*)
The specific AI model to use for requests. Each platform offers different models with varying capabilities, performance, and pricing.
Tip: Refer to each platform’s documentation for the most current list of available models and their capabilities.
2. Optional Arguments (for custom endpoint constructor)
endpointUrl (const char*)
Custom API endpoint URL for OpenAI Compatible AI Platfomrs or self-hosted services, requests will be sent to this URL instead of the platform’s default endpoint.
Use Cases:
- OpenAI Compatible API providers (e.g., Open Router, etc.)
- Self-hosted AI models (e.g., local OpenAI-compatible servers)
Return Value
Constructors do not return values in the traditional sense. Instead, they create and initialize an ESP32_AI_Connect object instance. The platform initialization success can be verified by calling getLastError() immediately after construction:
- Empty string – Indicates successful platform handler initialization
- Error message – Indicates that the specified platform is not supported, not enabled in the configuration, or encountered initialization issues
Example Code
Example 1: Basic Constructor Usage with OpenAI
This example demonstrates the most common usage pattern for connecting to OpenAI’s GPT models.
#include <ESP32_AI_Connect.h>
#include <WiFi.h> // ESP32 WiFi functionality
// Initialize connection to OpenAI with GPT-4
ESP32_AI_Connect aiClient("openai", "your-openai-api-key", "gpt-4");
void setup() {
Serial.begin(115200);
// Initialize WiFi connection (required before making actual API calls)
WiFi.begin("your-ssid", "your-password");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
// Check if platform initialization was successful
if (!aiClient.getLastError().isEmpty()) {
Serial.println("Platform initialization failed: " + aiClient.getLastError());
Serial.println("Check that USE_AI_API_OPENAI is enabled in ESP32_AI_Connect_config.h");
return;
}
Serial.println("OpenAI client initialized successfully!");
}
void loop() {
// Your main code here
}
Example 2: Constructor with Custom Endpoint
This example demonstrates using a custom endpoint URL, useful for self-hosted AI services or alternative API providers.
#include <ESP32_AI_Connect.h>
#include <WiFi.h> // ESP32 WiFi functionality
// Initialize with custom endpoint for self-hosted OpenAI-compatible service
ESP32_AI_Connect aiClient("openai-compatible",
"your-api-key",
"custom-model-name",
"https://your-custom-api.com/v1/chat/completions");
void setup() {
Serial.begin(115200);
// Initialize WiFi connection
WiFi.begin("your-ssid", "your-password");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
// Check platform initialization status
String error = aiClient.getLastError();
if (error.isEmpty()) {
Serial.println("Custom endpoint client initialized successfully!");
} else {
Serial.println("Failed to initialize custom endpoint client: " + error);
}
}
void loop() {
// Your main code here
}
Notes
- WiFi connectivity must be established before making actual API requests
- The constructor automatically sets the WiFi client to insecure mode for HTTPS connections
- Platform identifiers are case-insensitive (automatically converted to lowercase)
- Custom endpoints must be compatible with the specified platform’s API format
- API keys should be stored securely and never hardcoded in production code
- Platform support must be enabled in
ESP32_AI_Connect_config.husing the appropriateUSE_AI_API_*defines - Check
getLastError()immediately after construction to verify platform handler initialization - The constructor performs platform handler setup but not network connectivity checks
- All Arduino sketches require both
setup()andloop()functions