How to Connect an Arduino UNO WiFi R4 to OpenAI API

by AustinS89 in Circuits > Arduino

888 Views, 3 Favorites, 0 Comments

How to Connect an Arduino UNO WiFi R4 to OpenAI API

chat_gpt_arduino.png

In this Instructable, we'll learn how to connect an Arduino UNO WiFi R4 to the internet and make a request to the OpenAI API (Chat GPT). This project is perfect for those looking to integrate AI capabilities into their Arduino projects.

For reference, here's a link to my project if you want to get started using it:

https://app.cirkitdesigner.com/project/6c561288-cb6c-43b2-bb27-2ce5b1217712

Supplies

  1. Cirkit Designer IDE (https://app.cirkitdesigner.com)
  2. Arduino UNO WiFi R4
  3. A WIFI network
  4. An Open AI API Key

Set Up Your Cirkit Designer / Arduino Environment

compilation.PNG

Here is a link to my project: https://app.cirkitdesigner.com/project/6c561288-cb6c-43b2-bb27-2ce5b1217712

  1. Open up this link
  2. Click on the "Code" tab at the top
  3. Click "Verify" to make sure that this code compiles successfully for you. If the code compiles correctly, you should see a green output in the console that says Compilation complete.

Add Your WIFI + Open AI API Credentials

At the top of the code file, you should see the following code.

You'll need to insert your WIFI name and password, since this code requires a connection to the internet.

You will also need to insert your Open AI API key. If you haven't done so, you will need to create an Open AI API account by navigating to their home page (https://openai.com/), selecting Products -> API Login and setting up your account. I will update this post soon with more detailed instructions for how to setup your account and get an API key.


// WiFi credentials
#define SECRET_SSID "Insert WIFI Name (SSID)"
#define SECRET_PASS "Insert WIFI Password"
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
char server[] = "api.openai.com";
WiFiSSLClient client;
// OpenAI API Key
const char* openai_api_key = "Insert Open AI API Key";


Connect Arduino R4 to Computer Via USB

select port.PNG

Next, you will connect your Arduino R4 to your computer with a USB cable.

Within the Cirkit Designer code editor, you will need to select the port of your Arduino by clicking Select Port

Upload Code to Arduino

upload.PNG

Next, you will click the Upload button to upload your code to the Arduino. If this step completes successfully, you should see a green output in the Code console.

Open Serial Monitor to Test

serial.PNG

To test your code, open the Serial Monitor (button is in the top-right). You may need to click the Reset button on the arduino now.

Finally, you should see the following output in your Serial Monitor:


Please upgrade the firmware
Attempting to connect to SSID: ElCapitan_2.4
SSID: ElCapitan_2.4 IP Address: 10.0.0.206
signal strength (RSSI):-67 dBm

Starting connection to OpenAI API...
Connected to OpenAI API server
Raw JSON Response: { "id": "chatcmpl-A74dnLdSXfCaVZex55P17tBX8m7FO", "object": "chat.completion", "created": 1726249499, "model": "gpt-4o-2024-05-13", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "1 + 1 equals 2.", "refusal": null }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 15, "completion_tokens": 8, "total_tokens": 23, "completion_tokens_details": { "reasoning_tokens": 0 } }, "system_fingerprint": "fp_25624ae3a5"}

Assistant's response: 1 + 1 equals 2.

disconnecting from server.

That's It!

If you've gotten to this point, then you've successfully connected your Arduino to Chat GPT!!

Have fun building some cool projects :)

Best,

Austin

Founder, Cirkit Designer