Building a home security camera with thinged.io and 42 lines of Python!

Estimated time: 15 minutes

Prerequisites:

Twilio Account
Cloudinary Account
Thinged.io Account + Thing
Raspberry Pi + RPi Camera

This tutorial will step you through creating a thinged.io part that can take a photo with your Raspberry Pi, upload it to cloudinary and send it via mms using Twilio. We’ll guide you through creating your first thinged part and how to think about structuring them, so you can build your own extensions to the Thinged.io platform.

0: Setup your Raspberry Pi with Thinged.io

See here:
https://medium.com/@Thinged/getting-started-with-thinged-io-720e1cce6983

  1. Setup your Raspberry Pi to take a photo
  2. Upload the photo to Cloudinary
  3. Send a MMS using Twilio

Wrap up

  1. Create the required accounts and get all the API keys and tokens

Once you create your Twilio and Cloudinary accounts, you can get your Account Ids and Access Tokens from their respective dashboards:

Cloudinary Dashboard, access keys obscured
Twilio dashboard, access keys obscured

2. Setup your Raspberry Pi with the Python Prequisites:

Now you have your accounts created, we have to install requirements on the Raspberry Pi for communicating with Cloudinary and Twilio.

sudo apt-get install python3-pip

pip3 install --user cloudinary

pip3 install --user twilio

Done.

2. Configure your Raspberry Pi camera

Enable your Raspberry Pi’s camera module through the raspi-config:

sudo raspi-config

Select Interfacing Options , then Camera and enable it.

Reboot your Raspberry Pi.

Test your camera:

raspistill -v -o test.jpg

3. Test the script on your Raspberry Pi

We’ve created a github gist that you can use for taking these photos. Download this script onto your Raspberry Pi and edit the following variables:

cloudinary_cloud_name
cloudinary_api_key
cloudinary_api_secret
twilio_account_sid
twilio_auth_token
twilio_from_number
phone_number

The script itself is ultra simple and is comprised of a couple small functions:

  • take_photo(photo_name) which takes the photo from the raspberry pi using the raspistill library installed in Raspbian by default.
  • upload_photo_to_cloudinary(photo_file_name) which uploads a photo with the given name.
  • send_mms(to, message, media_url) which sends a your photo using Twilio.

4. Creating the Thinged.io Part

Just as the python script was a thin wrapper around the Cloudinary and Twilio, Thinged is a thin wrapper around shell commands.

A ‘Thing’ is comprised of ‘Parts’ which are just state that can be get and set through command line commands. The returned text of the command forms the basis of ‘get’ parts and placeholders are used to step in for arguments in ‘set’ commands.

The Part we are creating looks like the following:

Completed Part for taking photo

There is a lot of different keys there, but don’t worry- it is actually pretty simple. I’ve broken down the parameters below:

  • humanName human friendly name that you can use to identify the part.
  • description human friendly description so you can figure out how it works and add any other details.
  • partId randomly generated string that thinged.io uses to identify the part, and you use in the API to set values on it.
  • type Either input, output or inputOutput — defines whether the part is used to get state from your thing, set state on your thing, or a combination of the two (you can set it, but it could change elsewhere and should be read). Inputs require getCommand , outputs require setCommand.
  • setCommand The command that will be launched when setting a value. Here it just points to our python script. Adding{value} to this will be replaced everywhere with the parts value when you set commands via the API. The python script will have to be loaded into the raspberry pi through other means.
  • valueType This can be number, string or state. As mentioned earlier, Thinged is essentially a thin wrapper for command line commands, and so everything is translated into strings on the command line. valueType is important for any GUI that wants to present details about the state of a part. 
    Here, the valueType is ‘state’ which implies that there are a discrete number of state, and they can be enumerated into values. This allows the GUI to present a nice button for 2 state parts and a drop down for 3 or more state parts.
  • states Each state represents a different discrete possibility. As Thinged.io will only trigger a set when a value changes, here the discrete states are duplicates of each other, but with different values.
  • regexes Useful for input or inputOutput parts where you would like to preprocess the response of the command.

Create this part, either through the web portal, or through the API. from there you can add the part to your thinged… and now you are done!

Wrap Up:

If you’ve created the part, it should now look like this on the GUI:

If you have loaded the script into your pi, and thinged.io service is running, everything is done. Now for the pay off: Press the button!

Stuffed rabbit camera. Thanks mom!

In this tutorial we’ve gone through working with thinged.io output parts to create a spy camera with a raspberry pi, cloudinary and twilio accounts and a 42 lines of python holding it all together.

Stay tuned for more tutorials on thinged.io

Like what you read? Give thinged.io a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.