Skip to content

carolynz/poetry-camera-rpi

Repository files navigation

Poetry Camera

A camera that prints poems of what it sees.

We started this project as newcomers to the world of hobby electronics. The following instructions are intended for complete beginners, as we were. We simplified some of the design to optimize for easily sourcing and assembling parts; as a result, it's less compact than our photographed versions. If you are comfortable with electronics and coding, we encourage you to experiment and remix even more.

⚠️ These instructions are still in progress. ⚠️ Try it out and let us know what's confusing, or doesn't work.

Hardware you'll need

1. Computer: Raspberry Pi Zero 2 W with headers

Raspberry Pis are simplified computers. They are lightweight, cheap, have limited processing power, and are more fragile than typical consumer electronic devices. It's very sensitive to the specific power sources you use — too much power and you'll fry the part, too little power and the software won't run. You also have to manually shut down the software before unplugging the power, to protect the software from being corrupted.

We chose the Pi Zero 2 for its balance of processing power (Pi Zeros are too slow) and compact size (most other Pis on the market are larger). The wire diagrams in this tutorial will apply to all Raspberry Pis, but there may be differences in software and camera compatibility, especially with older devices. We've tested this with a Pi 3b+ and it works fine, but a Pi 4 probably requires too much power to make it a viable portable solution.

Raspberry Pi Zero 2 is often sold without headers (those 2x20 black metal connectors). The headers let you easily connect the Pi to the printer and buttons with plug-in jumper wires. If you buy the Pi without headers, you'll need to separately buy a 2x20 header and solder them on yourself. If you don't want to solder, you can use hammer-on headers and this installation rig.

Raspberry Pis are also recovering from a supply shortage. Check rpilocator.com for live stock notifications on standalone parts (does not list accessory kits).

2. Accessories to connect the Raspberry Pi to stuff.

Mostly straightforward, but be careful of damaging the hardware. The Raspberry Pi camera is delicate and can be easily fried via static. We broke 3 cameras in the process of making this project. Just make sure to always store it in a static-shielding bag when you're not using it.

If you are connecting the camera to a Pi Zero 2, note that the Zero 2's camera connection collar is also very delicate. We broke a Pi Zero 2 camera collar in the process of making this as well and had to just get a new Pi 🥲

We have not tested these instructions with older models of Raspberry Pi cameras.

  • Camera accessories:
    • Camera case with tripod — Helps proteect the delicate camera hardware during development.
    • Camera cable sized specifically for Pi Zero & Zero 2 — If you are using a larger Pi, you only need default cable that comes with the camera. If you got your Zero 2 camera cable in a kit, it is likely the short ~2 inch cable. Make sure to get a long enough cable that gives you more flexibility in where you place your camera.
    • Optional: Tweezer for opening/closing the delicate camera collar on Pi Zero 2

We used the Adafruit thermal printer line for this project, but they have subsequently been discontinued. Similar printers exist on Amazon and Aliexpress; the ones we've bought work with the same printer drivers (which are also no longer maintained by Adafruit, but still seem to work).

The Nano Thermal Printer or Tiny Thermal Printer are more compact, but have slightly different wiring.

The important thing is that the thermal printer has a TTL serial connection so you can easily connect it to the Pi. Search "TTL embedded thermal printer" on Amazon or Aliexpress to find your parts.

5. Batteries:

If you want your camera to be portable, you'll need some batteries! The Pi requires a steady 5V of power @ 1.2A, while the printer needs 5-9V and draws ~2A while printing.

Recommended power supply: 6xAA batteries

It's not the lightest solution, but it's a beginner-friendly starting point.

Other solutions that could work:

  • Put the above circuit on a PCB so you just need to plug in the connectors instead of soldering — we did this for our project! Will upload gerber files in the future.
  • 7.2V lithium batteries, e.g. 2x 18650s
  • 7.4V NiMH batteries for RC cars
  • If you don't need it to be super portable, get a portable power station to plug in the Pi and printer
  • If you don't want to solder, you could power the Pi and printer through two separate batteries. However, we've run into overheating issues with the commonly-recommended PiSugar 3 battery.

Power supplies that DON'T work:

  • Typical 9V alkaline batteries do not work as they do not provide enough current
  • Standard phone banks don't provide continuous power to the Raspberry Pi, causing it to shut down after a few minutes
  • Just plugging both devices in to a single 5V power bank — it can't handle the current draw while printing. Either the Pi shuts down during printing, or the printer doesn't have enough power to print.

6. Shutter button & LED

Any LED + momentary pushbutton will work for the shutter button. We use the LED as a status indicator for things like ready, loading, etc.

7. Miscellaneous equipment

  • Wire cutter & stripper
  • Soldering iron & accessories
  • Jumper cables

Software

This code currently uses OpenAI's models to turn the image into a poem. It also uses thermal printer drivers from Adafruit's thermal printer Python library.

You'll need to get your own OpenAI account & API key. Each request costs a couple of cents.

Currently, the main.py script running on the Pi:

  • Takes a photo when you click the shutter button
  • Sends the photo to GPT-4 Vision to caption the photo
  • When we receive the caption, we ask GPT-4 to turn the caption into a poem
  • When we receive the poem, print the poem out on thermal receipt printer

Putting it all together

This was adapted from the following tutorials:

Part 1. Check that your Raspberry Pi & camera works

  1. Connect your Raspberry Pi to your Camera module.

  2. Insert your SD card with a fresh install of any Raspberry Pi OS onto the Pi.

  3. Connect your Pi to a monitor via mini HDMI.

  4. Plug in power. You should see a green light on the Pi, and a start-up screen on the monitor.

  5. Once the Pi is on, open up the Terminal on your Pi to start making changes.

  6. Set up Raspberry Pi hardware to take Camera & Serial inputs:

sudo raspi-config
  1. You'll want to adjust the following settings:

    • Glamor: ON (for Camera setup on newer versions of Raspbian OS)
    • Serial Port ON (lets you access receipt printer inputs)
    • Serial Console OFF (idk what this does)

    Restart the system as needed.

[Tutorial TODO: include a basic camera test script & show desired behavior]

Part 2. Check that your printer works

  1. Update the system and install requirements. I'm not sure you even need all of these; I can go over these again later and trim out the unnecessary ones.
$ sudo apt-get update
$ sudo apt-get install git cups build-essential libcups2-dev libcupsimage2-dev python3-serial python-pil python-unidecode
  1. Install some software required to make the Adafruit Thermal Printer work.
$ cd
$ git clone https://github.com/adafruit/zj-58
$ cd zj-58
$ make
$ sudo ./install
  1. Clone this repo, which contains our Poetry Camera software:
$ cd
$ git clone https://github.com/carolynz/poetry-camera-rpi.git
  1. Set up your thermal printer, connecting it to power and your Pi. See diagram and instructions in this tutorial. Test that it works. Pay attention to your printer's baud rate (e.g. 19200). We will use this later on.

  2. Open our poetry-camera-rpi directory:

$ cd poetry-camera-rpi
  1. If your printer's baud rate is different from 19200, open main.py and replace that number with your own printer's baud rate:
# main.py:

# instantiate printer
printer = Adafruit_Thermal('/dev/serial0', 19200, timeout=5)

[TODO] need a setup script to test that the printer works

Part 3. Set up the AI

  1. Set up an OpenAI account and create an API key.

  2. Navigate to your directory with the Poetry Camera code and create a .env file, which will store sensitive details like your OpenAI API key: nano .env

  3. In the .env, add your API key: OPENAI_API_KEY=pasteyourAPIkeyhere

[TODO] add an openai test script

Part 4. Get it working end-to-end

[TODO] include wiring diagram

  1. Connect buttons

  2. Run the poetry camera script.

$ python main.py
  1. Check that the shutter button lights up, indicating that the camera is ready to take a picture

  2. Click the shutter button and wait for the poem to print out.

[TODO] troubleshooting instructions different common error messages

Part 5. Automatically run the Poetry Camera code when the camera turns on

  1. Set up a cron job to run your python script at startup. First, open your crontab file to your default editor:
$ crontab -e

2 Then add the following line to your crontab, to run the script when you boot up the computer.

# Run poetry camera script at start
@reboot python /home/pi/poetry-camera-rpi/main.py >> /home/pi/poetry-camera-rpi/errors.txt 2>&1

The >> {...}errors.txt 2>&1 writes any error messages to errors.txt for debugging. A common failure mode is files cannot be found. Make sure that all your filepaths are absolute filepaths and have the right usernames and directory names.

  • Reboot the system for this to take effect
sudo reboot

Now reboot your camera and wait for the LED light to turn on!

Part 6. Make the power circuit

[TODO] clean this up & explain steps :)

image

Part 7: Change wifi networks on-the-go

The camera needs wifi to work. You could always hardcode in your mobile hotspot by editing wpa_supplicant.conf. If you want to connect to new wifi networks on the fly, just follow this simple tutorial with plug-and-play code. (You can auto-run the tutorial's Flask app and our main camera code as two cron jobs at the same time.)

To do the above tutorial, you'll need a second wifi adapter, plugged into your microUSB port. Definitely get a plug-and-play wifi adapter that works for Linux/Raspberry Pi.

Wifi adapter options that seem to work:

MicroUSB to USB adapters:

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages