Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- #include <BluetoothSerial.h>
- // Wi-Fi credentials
- const char* ssid = "your_SSID";
- const char* password = "your_PASSWORD";
- const char* api_key = "your_OPENAI_API_KEY"; // Your OpenAI API Key
- const char* api_url = "https://api.openai.com/v1/completions"; // OpenAI API URL
- // GPIO pin for controlling light (using a relay)
- const int lightPin = 15;
- // Bluetooth and microphone objects
- BluetoothSerial btSerial;
- void setup() {
- Serial.begin(115200);
- pinMode(lightPin, OUTPUT);
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to Wi-Fi...");
- }
- Serial.println("Connected to Wi-Fi.");
- // Initialize Bluetooth for feedback
- btSerial.begin("ESP32_Home_Automation");
- Serial.println("Bluetooth device ready to pair.");
- // Setup microphone here (specific code depends on your microphone library/setup)
- }
- void loop() {
- String userCommand = getVoiceInput(); // Capture voice input and convert to text
- // Send the user command to ChatGPT for processing
- String response = getChatGPTResponse(userCommand);
- // Handle ChatGPT response
- Serial.println("ChatGPT Response: " + response);
- // Parse response and control GPIO for the light or other devices
- if (response.indexOf("turn on the lights") != -1) {
- digitalWrite(lightPin, HIGH); // Turn on the light
- sendBluetoothFeedback("Lights turned ON.");
- } else if (response.indexOf("turn off the lights") != -1) {
- digitalWrite(lightPin, LOW); // Turn off the light
- sendBluetoothFeedback("Lights turned OFF.");
- }
- delay(10000); // Wait before next command
- }
- String getVoiceInput() {
- // This function should capture voice input using the microphone and convert it to text
- // In this example, we simulate the input with a placeholder
- return "Turn on the lights"; // Replace with actual speech-to-text processing
- }
- String getChatGPTResponse(String userInput) {
- HTTPClient http;
- http.begin(api_url);
- http.addHeader("Content-Type", "application/json");
- http.addHeader("Authorization", String("Bearer ") + api_key);
- // Create JSON payload for the request
- StaticJsonDocument<256> doc;
- doc["model"] = "text-davinci-003";
- doc["prompt"] = userInput;
- doc["max_tokens"] = 50;
- String requestBody;
- serializeJson(doc, requestBody);
- int httpResponseCode = http.POST(requestBody);
- String response = "";
- if (httpResponseCode == 200) {
- response = http.getString(); // Get response from ChatGPT
- } else {
- Serial.println("Error: " + String(httpResponseCode));
- }
- http.end(); // Close connection
- return parseChatGPTResponse(response);
- }
- String parseChatGPTResponse(String response) {
- // Parse the response JSON from ChatGPT
- StaticJsonDocument<1024> doc;
- deserializeJson(doc, response);
- String result = doc["choices"][0]["text"].as<String>();
- result.trim();
- return result;
- }
- void sendBluetoothFeedback(String message) {
- // Send voice feedback to the user via the Bluetooth speaker
- if (btSerial.hasClient()) {
- btSerial.println(message);
- Serial.println("Bluetooth Feedback: " + message);
- } else {
- Serial.println("No Bluetooth connection.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement