September 18, 2018 · Golang Linux

Building a Webcam with GoLang and Raspberry PI 3 B+

Raspberry Pi

Build List:

  1. Logitech HD Pro Webcam C920 $44.95
  2. Raspberry Pi 3 - Model B+ - $35.00
  3. 5V 2.5A Switching Power Supply $7.50
  4. Adafruit Raspberry Pi B+ / Pi 2 / Pi 3 Case $7.95
  5. 32 GB microSDHC Card $9.99

Total: $105.39


Installing Raspbian OS

You’ll need to download the Raspbian operating system on your PC or Mac and follow an Install Guide to get started. To use your Raspberry Pi, you’ll need to connect it to your monitor with an HDMI cable, and plug in your keyboard and mouse. Put the microSD card in the port and connect the power.

Dependencies

sudo apt-get install git
sudo apt-get install golang
cd $GOPATH/src
git clone https://github.com/scrivy/ratcam-go.git
cd ratcam-go.git

You’ll need to edit the ratcam.service file and set the correct source code path for ExecStart. Once you corrected the path move the file to systemd service directory:

mv  $GOPATH/src/ratcam-go/ratcam.service  /lib/systemd/system/ratcam.service

Next connect the camera and start the service:

sudo systemctl daemon-reload
sudo systemctl start ratcam
sudo systemctl status ratcam
sudo systemctl stop ratcam
sudo systemctl enable ratcam

Connect the Internet & Determine your IP address

This was the most challenging part for me because my AT&T router did not set a public IP address. You need to figure out how to configure your router to give your Pi a public IP address. Next open up your browser and ask google “whats my IP address”. You should be able to connect to your webcam!


Connect your domain with AWS

I use an aws t2.nano instance with Amazon Linux. The purpose of this server is to proxy requests to my IP address and connect my domain. Once you’ve spun up your instance, set up the following security group settings:

AWS

Hop into your instance and type sudo vim /etc/nginx/nginx.confto edit your nginx conf. The IPV6 stuff is specific to my AT&T connection but this will give you a starting point. You’ll need to include your domain and your server_name.

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}

http {
   access_log  /var/log/nginx/access.log;
    server {
        listen 80;
        listen [::]:80 ipv6only=on;
        server_name webcam.YOURDOMAIN.COM;
        # include /etc/nginx/default.d/*.conf;
                location / {
                    proxy_set_header X-Real-IP $remote_addr;
                    # proxy_set_header Host $http_host;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade";
                    proxy_set_header Host $host;
                    proxy_pass http://YOUR:IP:ADDRESS:HERE:8080;
                    # proxy_redirect  off;        
        }

     }
}

To start nginx sudo service nginx start.

To finish the plumbing you’ll need to jump into Amazons Route 53 and connect your domain and set up a Hosted Zone CNAME that routes to your AWS instance. Mine looks like this:

route53

Congratulations You should have a fully functional webcam running on an ARM chip, with GO, behind your own domain on AWS. You won’t need a monitor, keyboard or mouse to start it because systemd starts the process automatically. All for ~100 bucks.