ChatGPT Alarm Clock

by Smallhouse in Circuits > Raspberry Pi

571 Views, 1 Favorites, 0 Comments

ChatGPT Alarm Clock

Chat GPT Alarm - Raspberry Pi Pico W

An alarm clock that interfaces with ChatGPT and World Time to give you a joke to start your day.

Downloads

Supplies

Raspberry Pi Pico W

Neopixel Lights

3" Diameter 8 Ohm 1 Watt Speaker (though any PWM-compatible speaker will work)

ssd1306 OLED screen

2x Arcade Buttons

1/8 inch acrylic for the outer container

Breadboard and pin wires

Soldering iron and solder (optional)

Attach the Hardware

pic1.jpg

I used the following wiring for each piece of hardware. If you want to use my code, then your parts will need to be attached to the same pins.

ssd1306 OLED Screen: default I2C wiring (GND: any ground, VCC: pin 36, SCL: pin 7, SDA: pin 6)

Speaker: GP16 (pin 21)

Neopixel Lights: GP19 (pin 25)

Alarm Button: GP15 (pin 20)

GPT Button: GP14 (pin 19)

To verify correct wiring, it is useful to test each piece of hardware before moving forward. In my code, the display_init() and play_sound() functions are sufficient for testing the screen, speaker, and lights. Correct button functionality can be verified by using the last 6 lines of code in my "while True:" loop.

ChatGPT API

pic4.png

To connect the Pico W to wifi, you will need to edit the "settings.toml" file on your board. Add the following two lines, replacing My_WiFi_Name and My_WiFi_password with your

WIFI_SSID="My_WiFi_Name"

WIFI_PASSWORD="My_WiFi_Password"


Next, we will set up the ChatGPT API. For this step, you will need to create a paid account with OpenAI, the creator of ChatGPT. The account itself is free, but you pay based on the amount of text generated by the model. My code only generates short responses, so I only paid $0.03 between testing and showcasing this project.


Navigate to the "Personal" section of your account, and click "API keys" from the left sidebar. You should see the page that I have screenshotted above. Click the "Create new secret key" button to create a unique key that associates the responses generated with your account. Copy this key, and add a new line in your settings.toml file saying the following, replacing My_Key with the key you just generated:

OPENAI_API_KEY="My_Key"


Finally, add a fourth line to your settings.toml file with the desired prompt. Feel free to tweak this prompt to see what different responses the GPT model will produce. It should look as follows

MY_PROMPT = "Give me a short lighthearted joke to start my day"


My code takes care of the details of using the API to get something we can display on the screen (check out the get_joke(), get_joke_wrap(), iter_lines(), split_string(), and show_text() functions for specifics). But as a general overview, there are four steps:

  1. Post a request to ChatGPT using the prompt and API key from the settings.toml file.
  2. Parse the response object to strip all details and just get the text that we want.
  3. Split the joke into two parts: the question and the answer, making sure to add newline characters where appropriate so that the screen doesn't cut off any words.
  4. Switch from displaying the time to displaying the generated joke, pausing for proper comedic timing.

World Time API

To set up the clock with an accurate current time, I used the World Time API. Specifically, I set up the clock to be synced to the New York timezone since I'm located in Boston. We only need to access this once, since it is used to set the Pico W's internal clock that will keep the time after that.

Check out the get_time() and show_time() functions for details.

Build the Box

The attached .svg file will work to build the box using a laser cutter and 1/8-inch acrylic.

I cut out holes where appropriate for the screen, buttons, and power cable. I left the box half-assembled for some last testing to verify that my code was working properly. With some of the panels still removed, I soldered each part to my Pico W for a more permanent connection.

Next, I used superglue to attach the speaker, lights, pico/breadboard, screen, and buttons. Then I assembled the box, applying superglue in the joints to hold everything together. I avoided supergluing the top of the container so that I could remove it later if needed.

Enjoy your final product!

Downloads