Skip to content

Commit

Permalink
✨ ⬆️ Update to go modules (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bndw authored May 16, 2020
1 parent d6bae4b commit 51a510c
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 154 deletions.
70 changes: 0 additions & 70 deletions Gopkg.lock

This file was deleted.

38 changes: 0 additions & 38 deletions Gopkg.toml

This file was deleted.

9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
GOARGS=GOOS=linux GOARCH=arm CGO_ENABLED=0

.PHONY: build
build:
GOOS=linux GOARCH=arm CGO_ENABLED=0 go build -o ./root/usr/local/bin/cctv_upload ./cmd/cctv_upload

.PHONY: install
install:
go get -u github.com/golang/dep/cmd/dep
dep ensure -v
@cd ./uploader \
&& $(GOARGS) go build -o ../root/usr/local/bin/uploader .
63 changes: 28 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,15 @@ be helpful for that.

A USB webcam is used for this project. Any USB camera should do, I'm using a [Logitech C920 Webcam](https://www.amazon.com/gp/product/B006JH8T3S).

## Golang

You'll need a working Go environment on your local machine to build the code.

* Go v1.11
* [dep](https://github.com/golang/dep)

# Device Setup

## Install dependencies


```
sudo apt-get update
ssh [email protected]
# usb camera support
sudo apt-get install fswebcam
# Install motion
sudo apt-get install motion
# usb camera support and motion
sudo apt-get update -y && sudo apt-get install -y fswebcam motion
```

Test the camera with `fswebcam`
Expand All @@ -77,8 +66,8 @@ Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'image.jpg'.
```
Now we know our webcam is at `/dev/video0`. If you look at `image.jpg` you'll see the picture it took.

Now we know our webcam is at `/dev/video0`. If you look at `image.jpg` you'll see the picture it took.

## Configure motion

Expand Down Expand Up @@ -125,7 +114,7 @@ to AWS S3 and notifying a Slack channel.
"Version": "2012-10-17",
"Statement": [
{
"Sid": "cctv_upload",
"Sid": "securitycamera",
"Effect": "Allow",
"Action": [
"s3:PutObject",
Expand All @@ -144,36 +133,38 @@ to AWS S3 and notifying a Slack channel.

# Uploading images

Edit the `on_picture_save` script with your AWS and Slack secrets:
```
# ./root/usr/local/bin/on_picture_save
When motion is detected a new image is created and the `on_picture_save` script is invoked. This calls the `uploader` which uploads the image to S3. Download the [latest release](https://github.com/bndw/security-camera/releases/latest) of the uploader and save it in `./root/usr/local/bin/uploader`.

SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/slack/webhook
S3_BUCKET_NAME=my-bucket-name
S3_BUCKET_REGION=us-west-2
AWS_ACCESS_KEY=xxx
AWS_SECRET_KEY=xxxxx
Or, build from source if you have Go 1.13 or later:
```
make build
```

Build the [cctv_upload](./cmd/cctv_upload) program that ties the system together.
It's called by the `on_picture_save` script when a new image is created.
The program uploads the image to S3 and then sends a webhook to Slack with the image url.

Next, upload both programs to the pi
```
make install
make build
scp ./root/usr/local/bin/* [email protected]:/tmp/
```

Upload both programs to the pi
SSH to the pi to complete configuration
```
rsync -avz root/usr/local/bin/ pi@your_ip:/tmp
ssh pi@raspberrypi.local
cp /tmp/cctv_upload /usr/local/bin/
cp /tmp/on_picture_save /usr/local/bin/
# Copy the scripts into the PATH
sudo mv /tmp/{uploader,on_picture_save} /usr/local/bin/
```

Finally, open the motion config again and configure it to call the `on_picture_save` script everytime it creates an image.
Edit `/usr/local/bin/on_picture_save` and add your AWS and Slack secrets:
```
# /usr/local/bin/on_picture_save
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/slack/webhook
S3_BUCKET_NAME=my-bucket-name
S3_BUCKET_REGION=us-west-2
AWS_ACCESS_KEY=xxx
AWS_SECRET_KEY=xxxxx
```

Open the motion config again and configure it to call the `on_picture_save` script everytime it creates an image.
```
# /etc/motion/motion.conf
on_picture_save /usr/local/bin/on_picture_save %f
Expand All @@ -183,3 +174,5 @@ Restart motion
```
systemctl restart motion
```

You should be good to go. Check out the motion detection settings in `/etc/motion/motion.conf` for customizing thresholds, etc.
3 changes: 1 addition & 2 deletions root/usr/local/bin/on_picture_save
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ AWS_SECRET_KEY=xxxxx
# End configuration

IMAGE=$1
CCTV_UPLOAD=/usr/local/bin/cctv_upload

SLACK_WEBHOOK_URL="$SLACK_WEBHOOK_URL" \
S3_BUCKET_NAME="$S3_BUCKET_NAME" \
S3_BUCKET_REGION="$S3_BUCKET_REGION" \
AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY" \
AWS_SECRET_ACCESS_KEY="$AWS_SECRET_KEY" \
$CCTV_UPLOAD" "$IMAGE"
/usr/local/bin/uploader "$IMAGE"
File renamed without changes.
8 changes: 8 additions & 0 deletions uploader/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/bndw/security-camera/uploader

go 1.13

require (
github.com/aws/aws-sdk-go v1.30.29
github.com/google/uuid v1.1.1
)
25 changes: 25 additions & 0 deletions uploader/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
github.com/aws/aws-sdk-go v1.30.29 h1:NXNqBS9hjOCpDL8SyCyl38gZX3LLLunKOJc5E7vJ8P0=
github.com/aws/aws-sdk-go v1.30.29/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
File renamed without changes.
5 changes: 2 additions & 3 deletions cmd/cctv_upload/s3.go → uploader/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/satori/go.uuid"
"github.com/google/uuid"
)

func UploadToS3(region, bucket, filepath string) (string, error) {
Expand All @@ -19,15 +19,14 @@ func UploadToS3(region, bucket, filepath string) (string, error) {
}

// Change the filename to a UUID
newFilename := fmt.Sprintf("%s.jpg", uuid.Must(uuid.NewV4()))
newFilename := fmt.Sprintf("%s.jpg", uuid.New())

// Upload it
err = s3Put(s, bucket, filepath, newFilename)
if err != nil {
return "", err
}

// Return the URL
return fileURL(region, bucket, newFilename), nil
}

Expand Down
File renamed without changes.

0 comments on commit 51a510c

Please sign in to comment.