Advertisement
pleasedontcode

ESP32 Code for ChatGPT Integration

Sep 30th, 2024
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.30 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <ArduinoJson.h>
  4. #include <BluetoothSerial.h>
  5.  
  6. // Wi-Fi credentials
  7. const char* ssid = "your_SSID";  
  8. const char* password = "your_PASSWORD";  
  9. const char* api_key = "your_OPENAI_API_KEY";  // Your OpenAI API Key
  10. const char* api_url = "https://api.openai.com/v1/completions";  // OpenAI API URL
  11.  
  12. // GPIO pin for controlling light (using a relay)
  13. const int lightPin = 15;
  14.  
  15. // Bluetooth and microphone objects
  16. BluetoothSerial btSerial;
  17.  
  18. void setup() {
  19.   Serial.begin(115200);
  20.   pinMode(lightPin, OUTPUT);
  21.  
  22.   // Connect to Wi-Fi
  23.   WiFi.begin(ssid, password);
  24.   while (WiFi.status() != WL_CONNECTED) {
  25.     delay(1000);
  26.     Serial.println("Connecting to Wi-Fi...");
  27.   }
  28.   Serial.println("Connected to Wi-Fi.");
  29.  
  30.   // Initialize Bluetooth for feedback
  31.   btSerial.begin("ESP32_Home_Automation");
  32.   Serial.println("Bluetooth device ready to pair.");
  33.  
  34.   // Setup microphone here (specific code depends on your microphone library/setup)
  35. }
  36.  
  37. void loop() {
  38.   String userCommand = getVoiceInput();  // Capture voice input and convert to text
  39.  
  40.   // Send the user command to ChatGPT for processing
  41.   String response = getChatGPTResponse(userCommand);
  42.  
  43.   // Handle ChatGPT response
  44.   Serial.println("ChatGPT Response: " + response);
  45.  
  46.   // Parse response and control GPIO for the light or other devices
  47.   if (response.indexOf("turn on the lights") != -1) {
  48.     digitalWrite(lightPin, HIGH);  // Turn on the light
  49.     sendBluetoothFeedback("Lights turned ON.");
  50.   } else if (response.indexOf("turn off the lights") != -1) {
  51.     digitalWrite(lightPin, LOW);  // Turn off the light
  52.     sendBluetoothFeedback("Lights turned OFF.");
  53.   }
  54.  
  55.   delay(10000);  // Wait before next command
  56. }
  57.  
  58. String getVoiceInput() {
  59.   // This function should capture voice input using the microphone and convert it to text
  60.   // In this example, we simulate the input with a placeholder
  61.   return "Turn on the lights";  // Replace with actual speech-to-text processing
  62. }
  63.  
  64. String getChatGPTResponse(String userInput) {
  65.   HTTPClient http;
  66.   http.begin(api_url);
  67.   http.addHeader("Content-Type", "application/json");
  68.   http.addHeader("Authorization", String("Bearer ") + api_key);
  69.  
  70.   // Create JSON payload for the request
  71.   StaticJsonDocument<256> doc;
  72.   doc["model"] = "text-davinci-003";
  73.   doc["prompt"] = userInput;
  74.   doc["max_tokens"] = 50;
  75.  
  76.   String requestBody;
  77.   serializeJson(doc, requestBody);
  78.  
  79.   int httpResponseCode = http.POST(requestBody);
  80.  
  81.   String response = "";
  82.   if (httpResponseCode == 200) {
  83.     response = http.getString();  // Get response from ChatGPT
  84.   } else {
  85.     Serial.println("Error: " + String(httpResponseCode));
  86.   }
  87.  
  88.   http.end();  // Close connection
  89.   return parseChatGPTResponse(response);
  90. }
  91.  
  92. String parseChatGPTResponse(String response) {
  93.   // Parse the response JSON from ChatGPT
  94.   StaticJsonDocument<1024> doc;
  95.   deserializeJson(doc, response);
  96.  
  97.   String result = doc["choices"][0]["text"].as<String>();
  98.   result.trim();
  99.   return result;
  100. }
  101.  
  102. void sendBluetoothFeedback(String message) {
  103.   // Send voice feedback to the user via the Bluetooth speaker
  104.   if (btSerial.hasClient()) {
  105.     btSerial.println(message);
  106.     Serial.println("Bluetooth Feedback: " + message);
  107.   } else {
  108.     Serial.println("No Bluetooth connection.");
  109.   }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement