Skip to content

Commit

Permalink
Complete overhaul. Let's make this thing actually work.
Browse files Browse the repository at this point in the history
  • Loading branch information
geerlingguy committed Sep 24, 2020
1 parent 7ba6281 commit 6e841f7
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 46 deletions.
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion inventory
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pi]
10.0.100.136 ansible_user=pi
10.0.100.94 ansible_user=pi
53 changes: 10 additions & 43 deletions main.yml
Original file line number Diff line number Diff line change
@@ -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=.*)(?<!libcomposite)$'
line: '\1 modules-load=dwc2,libcomposite'

- name: Ensure OTG functionality is enabled in config.txt
lineinfile:
path: /boot/config.txt
insertafter: EOF
regexp: 'dtoverlay=dwc2'
line: 'dtoverlay=dwc2'

- name: Set up serial device for login at boot.
file:
src: /lib/systemd/system/[email protected]
dest: /etc/systemd/system/getty.target.wants/[email protected]
state: link
tasks:
- include_tasks: tasks/uvc-gadget.yml
- include_tasks: tasks/boot-config.yml
- include_tasks: tasks/piwebcam.yml
17 changes: 17 additions & 0 deletions tasks/boot-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Ensure OTG functionality is enabled in cmdline.txt
lineinfile:
path: /boot/cmdline.txt
backrefs: true
regexp: '^(console=.*)(?<!libcomposite)$'
line: '\1 modules-load=dwc2,libcomposite'
notify: reboot_pi

- name: Make necessary changes in config.txt
lineinfile:
path: /boot/config.txt
insertafter: EOF
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
with_items: '{{ config_txt_changes }}'
notify: reboot_pi
20 changes: 20 additions & 0 deletions tasks/piwebcam.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Set up serial device for login at boot.
file:
src: /lib/systemd/system/[email protected]
dest: /etc/systemd/system/getty.target.wants/[email protected]
state: link

- name: Copy piwebcam service into place.
copy:
remote_src: true
src: /home/pi/uvc-gadget/piwebcam.service
dest: /etc/systemd/system/piwebcam.service
owner: root
group: root
mode: 0644

- name: Ensure piwebcam service is enabled at boot.
service:
name: piwebcam
enabled: "{{ piwebcam_boot_enabled }}"
12 changes: 12 additions & 0 deletions tasks/uvc-gadget.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Clone uvc-gadget to the Pi.
git:
repo: "{{ uvc_gadget_repo }}"
dest: /home/pi/uvc-gadget
version: master
become: false

- name: Build uvc-gadget.
make:
chdir: /home/pi/uvc-gadget
become: false

0 comments on commit 6e841f7

Please sign in to comment.