|
| 1 | +/* |
| 2 | + * ESP32_AI_Connect - Switch AI Platform Example |
| 3 | + * |
| 4 | + * Description: |
| 5 | + * This example demonstrates how to use the ESP32_AI_Connect library to dynamically |
| 6 | + * switch between different AI platforms and models using the begin() method on an |
| 7 | + * ESP32 microcontroller via a WiFi network. It shows how to reconfigure the AI client |
| 8 | + * to work with OpenAI GPT, Anthropic Claude, Google Gemini, and DeepSeek models, |
| 9 | + * sending the same test message to each platform and comparing their responses. |
| 10 | + * This example is useful for testing multiple AI providers or creating applications |
| 11 | + * that can fallback between different AI services. |
| 12 | + * |
| 13 | + * Author: AvantMaker <admin@avantmaker.com> |
| 14 | + * Author Website: https://www.AvantMaker.com |
| 15 | + * Date: May 30, 2025 |
| 16 | + * Version: 1.0.1 |
| 17 | + * |
| 18 | + * Hardware Requirements: |
| 19 | + * - ESP32-based microcontroller (e.g., ESP32 DevKitC, DOIT ESP32 DevKit, etc.) |
| 20 | + * |
| 21 | + * Dependencies: |
| 22 | + * - ESP32_AI_Connect library (available at https://github.com/AvantMaker/ESP32_AI_Connect) |
| 23 | + * - ArduinoJson library (version 7.0.0 or higher (available at https://arduinojson.org/) |
| 24 | + * |
| 25 | + * Setup Instructions: |
| 26 | + * 1. Install the ESP32_AI_Connect library. |
| 27 | + * 2. Update the sketch with your WiFi credentials (SSID and password). |
| 28 | + * 3. Add your API keys for the platforms you want to test (keep them secure!). |
| 29 | + * 4. Upload the sketch to your ESP32 board and open the Serial Monitor (115200 baud). |
| 30 | + * |
| 31 | + * License: MIT License |
| 32 | + * Repository: https://github.com/AvantMaker/ESP32_AI_Connect |
| 33 | + * |
| 34 | + * Usage Notes: |
| 35 | + * - You can comment out platforms you don't have API keys for by setting their keys to empty strings. |
| 36 | + * - The example will skip platforms with empty API keys and show a message. |
| 37 | + * - Use Serial Monitor input to interactively test different platforms. |
| 38 | + * - Each platform switch demonstrates the begin() method reconfiguring the client. |
| 39 | + * |
| 40 | + * Compatibility: Tested with ESP32 DevKitC and DOIT ESP32 DevKit boards. |
| 41 | + */ |
| 42 | + |
| 43 | +#include <ESP32_AI_Connect.h> // Main library for AI API connections |
| 44 | +#include <WiFi.h> // ESP32 WiFi functionality |
| 45 | + |
| 46 | +// Network credentials - REPLACE THESE WITH YOUR ACTUAL CREDENTIALS |
| 47 | +const char* ssid = "your_SSID"; // Your WiFi network name |
| 48 | +const char* password = "your_PASSWORD"; // Your WiFi password |
| 49 | + |
| 50 | +// API Keys for different platforms - REPLACE WITH YOUR ACTUAL KEYS |
| 51 | +// Set to empty string ("") for platforms you don't want to test |
| 52 | +const char* openaiApiKey = "your_OpenAI_API_KEY"; // OpenAI API key |
| 53 | +const char* claudeApiKey = "your_Claude_API_KEY"; // Anthropic Claude API key |
| 54 | +const char* geminiApiKey = "your_Gemini_API_KEY"; // Google Gemini API key |
| 55 | +const char* deepseekApiKey = "your_DeepSeek_API_KEY"; // DeepSeek API key |
| 56 | + |
| 57 | +// Initialize AI client (will be reconfigured using begin() method) |
| 58 | +ESP32_AI_Connect aiClient("openai", openaiApiKey, "gpt-3.5-turbo"); |
| 59 | + |
| 60 | +// Test message to send to all platforms |
| 61 | +const String testMessage = "Explain what you are in exactly 2 sentences."; |
| 62 | + |
| 63 | +// Platform configurations |
| 64 | +struct PlatformConfig { |
| 65 | + const char* platform; |
| 66 | + const char* apiKey; |
| 67 | + const char* model; |
| 68 | + const char* displayName; |
| 69 | +}; |
| 70 | + |
| 71 | +PlatformConfig platforms[] = { |
| 72 | + {"openai", openaiApiKey, "gpt-3.5-turbo", "OpenAI GPT-3.5 Turbo"}, |
| 73 | + {"claude", claudeApiKey, "claude-3-5-sonnet-20241022", "Anthropic Claude 3.5 Sonnet"}, |
| 74 | + {"gemini", geminiApiKey, "gemini-2.0-flash", "Google Gemini 2.0 Flash"}, |
| 75 | + {"deepseek", deepseekApiKey, "deepseek-chat", "DeepSeek Chat"} |
| 76 | +}; |
| 77 | + |
| 78 | +const int numPlatforms = sizeof(platforms) / sizeof(platforms[0]); |
| 79 | +int currentPlatformIndex = 0; |
| 80 | + |
| 81 | +void setup() { |
| 82 | + // Initialize serial communication for debugging |
| 83 | + Serial.begin(115200); |
| 84 | + delay(1000); |
| 85 | + |
| 86 | + // Connect to WiFi network |
| 87 | + Serial.println("\n=== ESP32_AI_Connect - Platform Switching Demo ==="); |
| 88 | + Serial.println("Connecting to WiFi..."); |
| 89 | + WiFi.begin(ssid, password); |
| 90 | + |
| 91 | + // Wait for WiFi connection (blocking loop with progress dots) |
| 92 | + while (WiFi.status() != WL_CONNECTED) { |
| 93 | + delay(500); |
| 94 | + Serial.print("."); |
| 95 | + } |
| 96 | + |
| 97 | + // WiFi connected - print IP address |
| 98 | + Serial.println("\nWiFi connected!"); |
| 99 | + Serial.print("IP address: "); |
| 100 | + Serial.println(WiFi.localIP()); |
| 101 | + Serial.println(); |
| 102 | + |
| 103 | + // Test all platforms automatically |
| 104 | + Serial.println("=== Testing All Available Platforms ==="); |
| 105 | + testAllPlatforms(); |
| 106 | + |
| 107 | + // Start interactive mode |
| 108 | + Serial.println("\n=== Interactive Platform Switching ==="); |
| 109 | + Serial.println("Commands:"); |
| 110 | + Serial.println(" 1 - Switch to OpenAI"); |
| 111 | + Serial.println(" 2 - Switch to Claude"); |
| 112 | + Serial.println(" 3 - Switch to Gemini"); |
| 113 | + Serial.println(" 4 - Switch to DeepSeek"); |
| 114 | + Serial.println(" t - Test current platform"); |
| 115 | + Serial.println(" s - Show current platform status"); |
| 116 | + Serial.println(" a - Test all platforms again"); |
| 117 | + Serial.println("\nEnter a command:"); |
| 118 | +} |
| 119 | + |
| 120 | +void loop() { |
| 121 | + // Check for user input |
| 122 | + if (Serial.available() > 0) { |
| 123 | + String input = Serial.readStringUntil('\n'); |
| 124 | + input.trim(); |
| 125 | + |
| 126 | + if (input == "1") { |
| 127 | + switchToPlatform(0); // OpenAI |
| 128 | + } else if (input == "2") { |
| 129 | + switchToPlatform(1); // Claude |
| 130 | + } else if (input == "3") { |
| 131 | + switchToPlatform(2); // Gemini |
| 132 | + } else if (input == "4") { |
| 133 | + switchToPlatform(3); // DeepSeek |
| 134 | + } else if (input == "t") { |
| 135 | + testCurrentPlatform(); |
| 136 | + } else if (input == "s") { |
| 137 | + showCurrentStatus(); |
| 138 | + } else if (input == "a") { |
| 139 | + testAllPlatforms(); |
| 140 | + } else { |
| 141 | + Serial.println("Invalid command. Enter 1-4, t, s, or a"); |
| 142 | + } |
| 143 | + |
| 144 | + Serial.println("\nEnter a command:"); |
| 145 | + } |
| 146 | + |
| 147 | + delay(100); |
| 148 | +} |
| 149 | + |
| 150 | +void testAllPlatforms() { |
| 151 | + Serial.println("Testing all platforms with message: \"" + testMessage + "\"\n"); |
| 152 | + |
| 153 | + for (int i = 0; i < numPlatforms; i++) { |
| 154 | + if (strlen(platforms[i].apiKey) == 0) { |
| 155 | + Serial.println("--- " + String(platforms[i].displayName) + " ---"); |
| 156 | + Serial.println("❌ Skipped (API key not provided)\n"); |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + Serial.println("--- " + String(platforms[i].displayName) + " ---"); |
| 161 | + |
| 162 | + // Use begin() method to switch platform |
| 163 | + bool success = aiClient.begin(platforms[i].platform, platforms[i].apiKey, platforms[i].model); |
| 164 | + |
| 165 | + if (success) { |
| 166 | + Serial.println("✅ Platform switched successfully"); |
| 167 | + Serial.println("Platform: " + String(platforms[i].platform)); |
| 168 | + Serial.println("Model: " + String(platforms[i].model)); |
| 169 | + |
| 170 | + // Test the platform |
| 171 | + Serial.println("Sending test message..."); |
| 172 | + String response = aiClient.chat(testMessage); |
| 173 | + |
| 174 | + if (response.length() > 0) { |
| 175 | + Serial.println("Response: " + response); |
| 176 | + } else { |
| 177 | + Serial.println("❌ Error: " + aiClient.getLastError()); |
| 178 | + } |
| 179 | + } else { |
| 180 | + Serial.println("❌ Failed to switch platform"); |
| 181 | + Serial.println("Error: " + aiClient.getLastError()); |
| 182 | + } |
| 183 | + |
| 184 | + Serial.println(); |
| 185 | + delay(1000); // Brief pause between platforms |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +void switchToPlatform(int platformIndex) { |
| 190 | + if (platformIndex < 0 || platformIndex >= numPlatforms) { |
| 191 | + Serial.println("❌ Invalid platform index"); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + if (strlen(platforms[platformIndex].apiKey) == 0) { |
| 196 | + Serial.println("❌ Cannot switch to " + String(platforms[platformIndex].displayName) + " - API key not provided"); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + Serial.println("Switching to " + String(platforms[platformIndex].displayName) + "..."); |
| 201 | + |
| 202 | + // Use begin() method to switch platform |
| 203 | + bool success = aiClient.begin( |
| 204 | + platforms[platformIndex].platform, |
| 205 | + platforms[platformIndex].apiKey, |
| 206 | + platforms[platformIndex].model |
| 207 | + ); |
| 208 | + |
| 209 | + if (success) { |
| 210 | + currentPlatformIndex = platformIndex; |
| 211 | + Serial.println("✅ Successfully switched to " + String(platforms[platformIndex].displayName)); |
| 212 | + Serial.println("Platform: " + String(platforms[platformIndex].platform)); |
| 213 | + Serial.println("Model: " + String(platforms[platformIndex].model)); |
| 214 | + } else { |
| 215 | + Serial.println("❌ Failed to switch platform"); |
| 216 | + Serial.println("Error: " + aiClient.getLastError()); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +void testCurrentPlatform() { |
| 221 | + Serial.println("Testing current platform with message: \"" + testMessage + "\""); |
| 222 | + Serial.println("Please wait..."); |
| 223 | + |
| 224 | + String response = aiClient.chat(testMessage); |
| 225 | + |
| 226 | + if (response.length() > 0) { |
| 227 | + Serial.println("✅ Response received:"); |
| 228 | + Serial.println(response); |
| 229 | + } else { |
| 230 | + Serial.println("❌ Error communicating with AI"); |
| 231 | + Serial.println("Error details: " + aiClient.getLastError()); |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +void showCurrentStatus() { |
| 236 | + Serial.println("=== Current Platform Status ==="); |
| 237 | + if (currentPlatformIndex >= 0 && currentPlatformIndex < numPlatforms) { |
| 238 | + Serial.println("Active Platform: " + String(platforms[currentPlatformIndex].displayName)); |
| 239 | + Serial.println("Platform ID: " + String(platforms[currentPlatformIndex].platform)); |
| 240 | + Serial.println("Model: " + String(platforms[currentPlatformIndex].model)); |
| 241 | + } else { |
| 242 | + Serial.println("No platform currently active"); |
| 243 | + } |
| 244 | + |
| 245 | + Serial.println("\nAvailable Platforms:"); |
| 246 | + for (int i = 0; i < numPlatforms; i++) { |
| 247 | + String status = (strlen(platforms[i].apiKey) > 0) ? "✅ Available" : "❌ No API Key"; |
| 248 | + String current = (i == currentPlatformIndex) ? " [CURRENT]" : ""; |
| 249 | + Serial.println(" " + String(i+1) + ". " + String(platforms[i].displayName) + " - " + status + current); |
| 250 | + } |
| 251 | +} |
0 commit comments