How I Taught My Dog to Text Selfies with an Arduino and Twilio

July 27, 2016
Written by

dog-button

When Twilio launched Programmable MMS, I started to wonder if we could teach our dog to text us selfies. Thanks to the Arduino Yun and a whole bunch of treats, the answer is a resounding “Yes!”, as you can see in this amazing video from Keith Hopkin over at Mashable:

My dog texts me a selfie

What you see in the video is a cigar box (your local cigar shop probably sells empties for $2) housing a “massive arcade button” and an Arduino Yun. The webcam is plugged into the USB port of the Yun, and a micro-USB cable runs out to a power source.

cigar-box-exterior

cigar-box-interior

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.

The Arduino sketch simply:

  • waits for a button press
  • runs a shell command to take a picture
  • runs a Python script to upload the picture to Dropbox and send the MMS

#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();
}

The Python script uses the Dropbox SDK and Twilio helper library to:

  • upload the picture to Dropbox
  • get a publicly accessible url for the picture
  • use that url to send an MMS via Twilio

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)

If you’d like some more detail on how we got here, we’ve documented the entire process from Arduino Yun unboxing to sending MMS with Twilio in these three tutorials:

Onward!

What’s most exciting to me about this project, aside from the sheer novelty of dogs 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: 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 who want 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?

Whatever it is, I’d like to hear about it. Hit me up at @greggyb or gb@twilio.com.

Many thanks to Keith Hopkin over at Mashable for shooting that amazing video.