Table Of Contents |
---|
Introduction |
Part 0: Prework |
Part 1: Pi and Web App |
Part 2: Motion Detection |
In Part 1 of this tutorial, you are going to set up your Pi and get a web application running locally. By the end of Part 1, you will have the web application communicating with the Pi so that you can click a button on the web app to take a picture with the Pi's camera.
Important: Make sure you complete the prework before diving in to Part 1.
- Fork and clone this repo to your own machine
- Set up a balena.io application
- Get the application running locally
- Configure your Pi with a balenaOS image
- Get the application talking to the Pi!
Since this tutorial is focused on working with a Pi, we've written the code for the web application for you. That way you don't have to fixate on debugging code when you want to learn about a Pi. If you want more of a challenge, you are welcome to modify or extend the existing code in any way you'd like.
After the download completes, you don't need to take any further action with Etcher at this time.
Detailed steps here if you need them!
- Fork the repo to your own account.
- Clone:
[email protected]:your-username/PiMotions.git
- Navigate into the
static
directory:cd PiMotions/app/static
- Rename the
.env.example
file to.env
by runningmv .env.example .env
in your terminal.
-
Sign in to your balena.io account. If you don't have one, complete the prework and then come back.
-
You'll see a prompt to create a new application.
- Set Device Type to "Raspberry Pi (v1 or Zero)"
- Give your application a name!
- Click "Create New Application"
-
Click "Add device" to configure a balenaOS image specifically for your application. Choose the recommend balenaOS version, then toggle on "Development" under "Select Edition" and "Wifi + Ethernet" under "Network Connection".
-
Enter the WiFi SSID and passphrase, and then click "Download".
While the balenaOS image is downloading, do the following:
- From your balena.io dashboard, set device configuration variables by clicking on "Fleet Configuration". Add the following two device config variables:
- NAME:
RESIN_HOST_CONFIG_gpu_mem
VALUE:128
- NAME:
RESIN_HOST_CONFIG_start_x
VALUE:1
These variables make sure that the camera works on the Pi.
- Add a balena.io remote repo by copying the git remote add command in the top right of your dashboard, and running it in the terminal.
While you're waiting for the balenaOS image download to finish, you can move on to the next step, which is to get the app running locally.
- Make sure you are in the
app/static
directory by typingcd app/static
in the conole while in the root directory. - Make sure Docker is running
- Run
npm run docker-build
This will build a Docker image based on our Dockerfile and start our containers. You can follow the process by watching your terminal output.
Once it finishes, your app will be running, so you can navigate to localhost:80
in your browser to check it out.
- Navigate to
localhost:80
in your browser. Click on "Webcam" under "Image Source" and then click "Take a Picture".
Note: We don't need to build the Docker image every time we want to start our app. If you want to start the app without rebuilding the image, you can run npm run start
, which runs docker-compose up
for us.
npm run docker-build
may take a few minutes. If your balenaOS image has downloaded (from step 2), while you are waiting on the app to build, you can move on to the next step.
Once the balenaOS image has downloaded, we'll use Etcher to flash the image onto our SD card. (You may be asked to grant admin permissions to Etcher.)
- Open Etcher.
- Insert your SD card into your computer.
- Using Etcher, click "Select Image" and find the image you downloaded in step 3 above.
- Click "Flash!"
Once Etcher finishes creating the bootable SD card, it will eject it for you.
- Insert the SD card into your Pi.
- Plug your Pi into your computer or a power source using a micro USB cable. (We need power!)
- It make take a few minutes, but your Pi should appear on your balena.io dashboard.
Once your Pi appears in your dashboard you are good to proceed!
-
Navigate to the root of your project (the
PiMotions/
directory). If you're in theapp/static
directory, runcd ..; cd ..
to get toPiMotions/
. -
Run
git push balena master
This is going to deploy the code in the server/
directory to our Pi.
If you're curious, you can look at the Dockerfile.template
file to see the commands that are run.
Note: this may ask you to add this host to your list of allowed hosts. Type 'yes'.
When you see a unicorn appear in your terminal, your push was successful!
Visit your device's balena page to monitor download progress. This will take ~15 minutes over wifi. While this takes place, continue with setup below!
We have an web app running locally that can take a picture using our computer's webcam. The next step is to get the web app and the Pi talking to each other, so that we can use the Pi's camera to take a picture.
Right now, the "R Pi" button under "Image Source" doesn't do anything. We're about to fix that.
- Enable a public url for your device. Go into "Actions" on your balena.io device dashboard and click "Enable All Public Device URLs".
Copy the public url and paste it into
app/static/.env
as the value forRASPI_URL
.
RASPI_URL=<your public url here>
-
Navigate to
app/static
directory by runningcd app/static
in your terminal. -
Run
npm run docker-down
, and then runnpm run docker-build
. -
Check your device's balena page -- if the download is complete, proceed!
-
Navigate to
localhost:80
in your browser. Click on "R Pi" under "Image Source", and then click "Take Picture".
🎉🎉🎉
Your web app is now set up to take pictures using your computer's webcam and using your Pi, give yourself a pat on the back!
In the Part 2, we'll write code to detect motion using the picamera.