Interesting Applications of ChatGPT on ESP32

Combining ChatGPT with ESP32 can lead to more interesting applications in the Internet of Things, such as chatbots, voice assistants, and natural language interfaces. Below, I will use the ChatGPT API in ESP32.

To get responses from ChatGPT on ESP32, we need to follow these steps:
  1. Register on the OpenAI website and install the necessary libraries on ESP32;
  2. Create a new project on the OpenAI API and generate an API key;
  3. Use the API key to authenticate requests to the OpenAI API;
  4. Send text input to the OpenAI API using HTTP requests and receive responses in JSON format;
  5. Parse the response and use it to control the ESP32 microcontroller.

Now, I will implement the above steps on ESP32:

1. First, we need to include the necessary libraries so that the ESP32 can communicate over Wi-Fi, make HTTP requests, and parse JSON data.

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

2. Define the network credentials for the Wi-Fi network to which the ESP32 will connect, as well as the OpenAI API key.

const char* ssid     = "your_SSID";
const char* password = "your_PASSWORD";
const char* apiKey = "your_API_KEY";
3. Define the setup() function, which is where the ESP32 connects to the Wi-Fi network and sends an HTTP POST request to the OpenAI API.
void setup() {
    //
}
4. In the setup() function, we will first initialize the serial port.
Serial.begin(9600);
5. Next, we will connect to the WiFi network.
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
6. Continue filling in the setup() function, using the HTTPClient library to send an HTTP Post request to the OpenAI API endpoint for communication.
// Send request to OpenAI API
String inputText = "Hello, ChatGPT!";
String apiUrl = "https://api.openai.com/v1/completions";
String payload = "{\"prompt\":\"" + inputText + "\",\"max_tokens\":100, \"model\": \"text-davinci-003\"}";

HTTPClient http;
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer " + String(apiKey));

The inputText string defines the prompt for the API, which in this case is “Hello, ChatGPT!”.

The apiUrl string specifies the address of the API.
The payload string is a JSON object containing the prompt and other parameters, such as the maximum number of tokens to generate and the model used. In our case, we are using the “Text-Davinci-003” model, allowing for a maximum of 100 tokens.
Then, the HTTPClient object is initialized, and the begin() function is used to specify the API endpoint URL.
Next, we add HTTP headers, such as Content-Type, where we specify that we will be using JSON data, and the Authentication header to authenticate with the ChatGPT API using the API_KEY.
7. Finally, we use the http.POST() function to send an HTTP POST request to the OpenAI API service.
int httpResponseCode = http.POST(payload);
if (httpResponseCode == 200) {
  String response = http.getString();

  // Parse JSON response
  DynamicJsonDocument jsonDoc(1024);
deserializeJson(jsonDoc, response);
  String outputText = jsonDoc["choices"][0]["text"];
  Serial.println(outputText);
} else {
  Serial.printf("Error %i \n", httpResponseCode);
}

http.POST() will return the HTTP code of the response. If it is HTTP 200, we will parse the JSON and print it to the serial port.

If the HTTP code returned is not 200, we will print “Error: HTTP code”; for example, if your API token is invalid, it will print “Error: 401”.
8. At this point, we have successfully connected to ChatGPT, and we can do some interesting things in the loop() function.
void loop() {
    //
}

For the complete code, please reply with esp32_chatgpt in the backend of the “Embedded Base” public account to obtain it.

Original link:https://www.espboards.dev/blog/chatgpt-in-esp32/
Interesting Applications of ChatGPT on ESP32

END

Source: Embedded Base
Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
Linux China announces cessation of operations!
Cracking software on the subway, surrounded by a crowd!
Implementation of magic code for CCTV Spring Festival Gala (never will be exposed)!

→ Follow to avoid getting lost ←

Leave a Comment

×