├── .gitattributes └── ESP32_Gemini └── ESP32_Gemini.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ESP32_Gemini/ESP32_Gemini.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const char* ssid = "SSID"; 9 | const char* password = "PASS"; 10 | const char* Gemini_Token = "GEMINI_API_KEY"; 11 | const char* Gemini_Max_Tokens = "100"; 12 | String res = ""; 13 | 14 | 15 | void setup() { 16 | Serial.begin(115200); 17 | 18 | WiFi.mode(WIFI_STA); 19 | WiFi.disconnect(); 20 | 21 | 22 | while (!Serial) 23 | ; 24 | 25 | 26 | // wait for WiFi connection 27 | WiFi.begin(ssid, password); 28 | Serial.print("Connecting to "); 29 | Serial.println(ssid); 30 | while (WiFi.status() != WL_CONNECTED) { 31 | delay(1000); 32 | Serial.print("."); 33 | } 34 | Serial.println("connected"); 35 | Serial.print("IP address: "); 36 | Serial.println(WiFi.localIP()); 37 | } 38 | 39 | void loop() 40 | { 41 | Serial.println(""); 42 | Serial.println("Ask your Question : "); 43 | while (!Serial.available()) 44 | ; 45 | while (Serial.available()) { 46 | char add = Serial.read(); 47 | res = res + add; 48 | delay(1); 49 | } 50 | int len = res.length(); 51 | res = res.substring(0, (len - 1)); 52 | res = "\"" + res + "\""; 53 | Serial.println(""); 54 | Serial.print("Asking Your Question : "); 55 | Serial.println(res); 56 | 57 | HTTPClient https; 58 | 59 | //Serial.print("[HTTPS] begin...\n"); 60 | if (https.begin("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + (String)Gemini_Token)) { // HTTPS 61 | 62 | https.addHeader("Content-Type", "application/json"); 63 | String payload = String("{\"contents\": [{\"parts\":[{\"text\":" + res + "}]}],\"generationConfig\": {\"maxOutputTokens\": " + (String)Gemini_Max_Tokens + "}}"); 64 | 65 | //Serial.print("[HTTPS] GET...\n"); 66 | 67 | // start connection and send HTTP header 68 | int httpCode = https.POST(payload); 69 | 70 | // httpCode will be negative on error 71 | // file found at server 72 | 73 | if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { 74 | String payload = https.getString(); 75 | //Serial.println(payload); 76 | 77 | DynamicJsonDocument doc(1024); 78 | 79 | 80 | deserializeJson(doc, payload); 81 | String Answer = doc["candidates"][0]["content"]["parts"][0]["text"]; 82 | 83 | 84 | 85 | // For Filtering our Special Characters, WhiteSpaces and NewLine Characters 86 | Answer.trim(); 87 | String filteredAnswer = ""; 88 | for (size_t i = 0; i < Answer.length(); i++) { 89 | char c = Answer[i]; 90 | if (isalnum(c) || isspace(c)) { 91 | filteredAnswer += c; 92 | } else { 93 | filteredAnswer += ' '; 94 | } 95 | } 96 | Answer = filteredAnswer; 97 | 98 | 99 | Serial.println(""); 100 | Serial.println("Here is your Answer: "); 101 | Serial.println(""); 102 | Serial.println(Answer); 103 | } else { 104 | Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); 105 | } 106 | https.end(); 107 | } else { 108 | Serial.printf("[HTTPS] Unable to connect\n"); 109 | } 110 | res = ""; 111 | } --------------------------------------------------------------------------------