From 6e841f7af9959f8e6f4483e52d7f3b1091cd599b Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Thu, 24 Sep 2020 17:24:26 -0500 Subject: [PATCH] Complete overhaul. Let's make this thing actually work. --- README.md | 50 ++++++++++++++++++++++++++++++++++++++-- config.yml | 21 +++++++++++++++++ inventory | 2 +- main.yml | 53 ++++++++----------------------------------- tasks/boot-config.yml | 17 ++++++++++++++ tasks/piwebcam.yml | 20 ++++++++++++++++ tasks/uvc-gadget.yml | 12 ++++++++++ 7 files changed, 129 insertions(+), 46 deletions(-) create mode 100644 config.yml create mode 100644 tasks/boot-config.yml create mode 100644 tasks/piwebcam.yml create mode 100644 tasks/uvc-gadget.yml diff --git a/README.md b/README.md index 4fd17f2..16d5303 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,53 @@ # Raspberry Pi Webcam -Inspired by David Hunt's blog post showing how to use a [Raspberry Pi Zero with a Pi Camera as a USB Webcam](http://www.davidhunt.ie/raspberry-pi-zero-with-pi-camera-as-usb-webcam/), I wanted to make my Raspberry Pi 4 do the same thing, but automated and with all the scripts wrapped in version control, since the blog post was a little bit vague in some areas. +Inspired by David Hunt's blog post showing how to use a [Raspberry Pi Zero with a Pi Camera as a USB Webcam](http://www.davidhunt.ie/raspberry-pi-zero-with-pi-camera-as-usb-webcam/), as well as [justinschuldt's gist](https://gist.github.com/justinschuldt/36469e2a89d95ef158a8c4df091e9cb4), I wanted to make my Raspberry Pi 4 do the same thing, but automated and with all the scripts wrapped in version control, since the blog post was a little bit vague in some areas. This Ansible playbook can be run on a Raspberry Pi to set it up as a USB OTG webcam. -And I'm still working on the playbook. So check back later :) +The playbook is meant to run on a brand new installation of Raspbian that has not had any configuration changes via `raspi-config` or any other tools, though it _should_ work correctly with an existing installation. + +There are a few things you should probably do manually at some point, including: + + - Changing the default password for the `pi` user account. + - Setting a WiFi country and enabling WiFi if you don't want to keep your Pi plugged into ethernet for remote access. + +Note that the playbook modifies your boot config, and as such you _should not run this playbook on a microSD or other boot volume you're not ready to reformat and re-flash!_ + +## Getting Started + + 1. Make sure you have Ansible installed on your computer. + 1. Flash the latest Raspberry Pi OS to your Raspberry Pi. Make sure you added an `ssh` file to the boot volume so SSH is enabled on first boot. + 1. Plug in your boot volume, and plug your camera into the camera connector. + 1. Boot the Raspberry Pi. + 1. Make sure you can log into the Pi via SSH. + 1. Update the IP address in `inventory` to match the IP address of your Raspberry Pi. + 1. Edit the `config.yml` file to your liking (the defaults should be fine though). + 1. Run the Ansible playbook: + + ``` + ansible-playbook main.yml + ``` + +At the end of the playbook (assuming this is the first time you've run it), the Raspberry Pi should reboot itself. At this point, it _should_ (assuming everything worked) be set up as a USB webcam. + +Grab a USB cable and plug the Pi into a port on your computer. + +Open up any video recording/conferencing software, and go to the camera selection, and BOOM! Select the Pi. + +## Known issues + +Well... most of the known issues have to do with the other projects _this_ project relies on: + + - If you're not using a Mac, you may need to adjust the brightness setting in the `uvc-gadget.c` file and re-compile it manually. I'm looking into a better way to allow this to be configured. + - The Pi 4 currently locks up when you try to use USB OTG and enable the webcam for some reason. (I've only successfully tested this on a Pi Zero and Pi Zero W). + - This stuff is a little bit of a complicated ball of string, so future Raspberry Pi OS and kernel updates could cause issues. I would not run this on a Raspberry Pi that is controlling a breathing machine or something like that. + +Yadda, yadda, standard "if something breaks don't blame me" disclaimer. The worst thing I've done to my Pi in testing this so far is accidentally breaking off the locking connector for the camera cable. Oops. + +## License + +MIT. + +## Author + +I'm [Jeff Geerling](https://www.jeffgeerling.com), and I approve of this repository. diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..5165c89 --- /dev/null +++ b/config.yml @@ -0,0 +1,21 @@ +--- +# The repository from which uvc-gadget will be cloned. +uvc_gadget_repo: https://github.com/climberhunt/uvc-gadget.git + +# Whether to enable piwebcam at boot or not. +piwebcam_boot_enabled: true + +# Changes to be made to the config.txt file. +config_txt_changes: + # Enable USB OTG mode. + - regexp: '^dtoverlay=.+' + line: 'dtoverlay=dwc2' + # Enable the camera. + - regexp: '^start_x=.+' + line: 'start_x=1' + # Ensure GPU has enough memory for camera. + - regexp: '^gpu_mem=.+' + line: 'gpu_mem=128' + # Enable HDMI 'safe mode' + - regexp: '^#?hdmi_safe=.+' + line: 'hdmi_safe=1' diff --git a/inventory b/inventory index 65c8ca6..ec6522c 100644 --- a/inventory +++ b/inventory @@ -1,2 +1,2 @@ [pi] -10.0.100.136 ansible_user=pi +10.0.100.94 ansible_user=pi diff --git a/main.yml b/main.yml index c94b57c..5312699 100644 --- a/main.yml +++ b/main.yml @@ -1,49 +1,16 @@ --- - hosts: pi become: true + gather_facts: false - tasks: - - name: Clone uvc-gadget to the Pi. - git: - repo: https://github.com/climberhunt/uvc-gadget.git - dest: /home/pi/uvc-gadget - version: master - become: false - - - name: Build uvc-gadget. - make: - chdir: /home/pi/uvc-gadget - become: false - - - name: Copy piwebcam service into place. - copy: - src: piwebcam.service - dest: /etc/systemd/system/piwebcam.service - owner: root - group: root - mode: 0644 + vars_files: + - config.yml - - name: Ensure piwebcam service is enabled at boot. - systemd: - name: piwebcam - enabled: true + handlers: + - name: reboot_pi + reboot: - - name: Ensure OTG functionality is enabled in cmdline.txt - lineinfile: - path: /boot/cmdline.txt - backrefs: true - regexp: '^(console=.*)(?