beanz Magazine

How I Taught My Dog to Text Me Selfies

Greg Baugues on Twilio

A trainable puppy plus treats plus technology equals a dog that can send selfies. Here's how.

A few weeks after we got our puppy, we taught her how to turn on a light.

Turns out Kaira will do just about anything if you can clearly communicate your desires and have a treat in your hand. There’s an Ikea lamp in our bedroom that’s activated by stepping on a floor switch. We started her training by placing her paw on the switch, saying “Light,” and giving her a treat. Once she got that, we’d press on her paw and withhold the treat until she heard a click. Eventually, we could say “Light” from across the room and Kaira would run over and do the job.

Shortly afterwards I started thinking, “I’ve got a dog that can press a button. What can I do with that?”

Doggy Selfies

When Twilio launched Programmable MMS, I started to wonder if we could teach Kaira to send selfies. I’m pleased to say that thanks to the Arduino Yun and a big red button, the answer is a resounding “Yes!”

What you’re seeing in the video is a cigar box housing a massive arcade button and an Arduino Yun. The second box serves as a stand for a webcam that’s plugged into the Yun. (My local cigar shop sells empties for $2 which make for sturdy and stylish hardware enclosures).

The WiFi enabled Arduino Yun has two microprocessors: one does all the pin interaction you typically associate with an Arduino. The second runs a stripped down version of Linux called OpenWRT which can run programs in your favorite scripting language (Python comes pre-installed, but you could put Ruby or Node on there if you so please). This project has one program running on each processor. Together, they are less than 60 lines of code.

Onward!

What’s most exciting to me about this project, aside from the sheer novelty of my dog sending selfies, is how simple each component is. The button press is literally the second example from Massimo Banzi’s Getting Started with Arduino. The Python script is practically cut-and-paste from the Dropbox getting started guide and the Twilio SMS and MMS quickstart.

Hardware hacking can be intimidating if you’ve never done it before, but remember that the most impressive hacks are often just simple building blocks stacked on top of one another. Wifi enabled devices like the Arduino Yun have drastically lowered the barrier to entry for web developers to dip their toes into the Internet of Things.

So let’s say you had a box that could interact with both the physical world and any web-based API using the programming language you already know. What could you do with that?

The Code

Here are the two scripts used for this project:

Arduino Sketch Code

#include <Bridge.h>
#include <Process.h>
 
const int BUTTON = 7;
 
void setup() {
  pinMode(BUTTON, INPUT);
  Bridge.begin();
}
 
void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    takePicture(); 
    uploadAndSend(); 
  }
}
 
void takePicture() {
  Process p;
  p.begin("fswebcam");
  p.addParameter("/mnt/sda1/pic.jpg");
  p.addParameter("-r 640x480");
  p.run();
}
 
void uploadAndSend() {
  Process p; 
  p.begin("python");
  p.addParameter("/mnt/sda1/arduino/upload-and-send.py");
  p.run();
}

upload-and-send.py Script Code

import datetime
import dropbox
from twilio.rest import TwilioRestClient
 
dropbox_access_token = "YOURDROPBOXTOKEN"
twilio_phone_number = "YOURTWILIOPHONENUMBER"
twilio_account_sid = "YOURTWILIOACCOUNTSID"
twilio_auth_token = "YOURTWILIOAUTHTOKEN"
cellphone = 'YOURCELLPHONE'
 
timestamp = datetime.datetime.now().strftime("%h-%m-%S")
filename = "kaira-" + timestamp + ".jpg"
 
f = open("/mnt/sda1/pic.jpg")
dropbox_client = dropbox.client.DropboxClient(dropbox_access_token)
response = dropbox_client.put_file(filename, f)
url = dropbox_client.media(response['path'])['url']
 
twilio_client = TwilioRestClient(twilio_account_sid, twilio_auth_token)
twilio_client.messages.create(
  to = cellphone,
  from_ = twilio_phone_number,
  body = "Woof.",
  media_url = url)

These tutorials describe the entire process from Arduino Yun unboxing to sending MMS:

Learn More

How I Taught My Dog to Text Me Selfies

https://www.twilio.com/blog/2015/03/how-my-dog-sends-selfies.html
http://twilio.com/blog/2015/02/arduino-wifi-getting-started-arduino-yun.html
http://twilio.com/blog/2015/02/arduino-powered-photobooth-arduino-yun-a-webcam-and-dropbox.html
http://twilio.com/blog/2015/02/send-sms-and-mms-from-your-arduino-yun.html

Also In The December 2016 Issue

Hour of Code and EU Code Week are events designed to introduce kids, young adults, and others to programming and computer science.

Real life treasure hunts are a way to get outdoors, learn map skills, and have fun finding hidden caches near you.

A trainable puppy plus treats plus technology equals a dog that can send selfies. Here's how.

An app to help kids remember important stuff like feed your pets, brush your teeth, and smile.

These books include lots of great projects to work on by yourself or with others, from Scratch and Minecraft to fun maker space projects.

The mBot robotics kit is an excellent comparatively low-cost way to begin working with robots.

There are maybe a bazillion Raspberry Pi projects online. Here are really fun projects plus links to find more.

The Wayback Machine lets you travel back in time to see old websites. Plus the Internet Archive has thousands of vintage games, software, books, and more.

Eating dog food doesn't sound like much fun but it's an important part of creating software.

The ability to identify patterns, decompose large problems into small parts, develop algorithms to solve problems, and generalize to find solutions.

To celebrate this wonderful time of the year, let’s create some holiday music using Sonic Pi on our Raspberry Pi.

This project shows how to use the pygame code library to move simple animations with the Python programming language.

This project, shows you how to create your own random password generator in the C# programming language.

These projects mix science and technology in interesting ways. Sewing and electronics, for example, is a different way to learn about electronics.

Links from the bottom of all the December 2016 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for December 2016.

What sounds like a country western dance actually is an efficient way to sort large sets of data randomly.