Raspberry Pi

How I Taught My Dog To Text Selfies

How I Taught My Dog To Text Selfies

by Greg Baugues

In his talk at RubyConf 2016, Greg Baugues shares an innovative and humorous approach to hardware hacking by explaining how he taught his dog, Kyra, to send him selfies via text message. The presentation is well-suited for Ruby developers who are hesitant about diving into hardware projects. Baugues outlines his journey from software to hardware hacking, using the Arduino Yun and eventually transitioning to the Raspberry Pi due to its affordability and capabilities.

Key Points Discussed:
- Introduction and Background: Baugues begins by expressing gratitude for the Ruby community and shares personal anecdotes about his family and his dog, Kyra, a rescue dog who inspired this project.
- Behavioral Conditioning: The speaker discusses how he trained Kyra using methods from the book 'Don't Shoot the Dog,' emphasizing the importance of positive reinforcement and behavioral conditioning.
- Initial Hardware Learning: After successfully training Kyra to turn on a lamp, Baugues became intrigued by how to get her to send text messages. He learned about the Arduino Yun, which is Wi-Fi enabled and capable of running Ruby scripts.
- Transition to Raspberry Pi: Due to limitations with Ruby on the Arduino Yun, he shifted to using the Raspberry Pi 3, highlighting its cost-effectiveness and functionality. He mentions that the Raspberry Pi performs well for projects involving cameras and button controls.
- Project Components: Baugues outlines the necessary components for the project, including a camera, a large button, and a housing box, all sourced affordably and typically repurposed.
- Code Development: The speaker live codes the project, explaining how to create scripts to take pictures, upload them to Dropbox, and send them via Twilio SMS. He demonstrates the integration of these services through API calls and live coding examples.
- Audience Interaction: Baugues engages the audience to send texts to test the setup, allowing them to see immediate responses and illustrating the real-time functionality of the project.
- Conclusion and Encouragement: He concludes by encouraging attendees to explore hardware hacking, noting that anyone with basic programming skills can tackle similar projects using accessible tools and APIs.

This talk underscores the exciting possibilities of combining coding with hardware projects and inspires Ruby developers to experiment with their own ideas, reinforcing that engaging in hardware hacking doesn't require deep technical expertise but a willingness to learn and experiment.

00:00:15.199 Alright, well thank you so much for coming. My name is Greg, and if you couldn't tell by the video, I'm giving the talk on how I taught my dog to text selfies.
00:00:20.580 First off, I just want to say it's a huge honor to be here. The first time I ever gave a talk at a tech conference was at the Mountain West Ruby Conference about three and a half years ago. I spoke on developers and depression, and I had only been coding Ruby for about a year.
00:00:33.870 The community was so amazing and has continued to be as I have gone to other Ruby conferences. I now serve on the developer evangelism team for a company called Twilio and have the privilege of attending many conferences across different languages. Yet, the Ruby community still feels very much like home to me. I've had the privilege of speaking at RailsConf before, but this is my first time at RubyConf, and it really is a bit of a dream to be here.
00:01:05.760 So thank you all, and I appreciate the organizers for having me here. As for a bit about myself, I now live in New York. My wife, daughter, and I moved to Brooklyn about four months ago after spending 11 years in Chicago, and we are absolutely loving it! We wanted to try something new while our daughter, who will turn two on Monday, was still young. In this way, we wouldn’t have to worry too much about school.
00:01:33.149 This is not our entire family, though, as you may have guessed from the title. About a year before Emma came along, we got Kyra. She is 12; this is the day we brought her home when she was about 12 weeks old. She's a rescue, and we're not really sure what breed she is, but we think she might be part Shiba Inu and possibly part Australian Kelpie.
00:01:57.539 She's really smart, but she also has the stubbornness that often comes with that. Early on, we realized we would need to put in some effort if we wanted to train her properly. There were still challenges, but we found this awesome book titled 'Don't Shoot the Dog.' It's written by a woman who used to train dolphins at SeaWorld and has done a whole bunch of other animal training.
00:02:20.850 The crazy thing about reading this book is that as you get a few chapters in, you realize it's not actually about pet training; it’s about behavioral conditioning! The tricks they teach you can also work on small children, coworkers, and pretty much anyone else. It's all about positive reinforcement. As it turns out, yelling at your dog just doesn’t work.
00:02:58.950 The trick is to find something in their behavior that's worth complimenting and rewarding. You then continue to encourage that behavior while gradually moving the goalpost. So, being the rebellious type, the first thing I did after reading this book was to shoot my dog (not literally, of course) to teach her how to do that.
00:03:16.110 One night, while laying in bed, I noticed a floor lamp next to our bed from Ikea that has a little switch you stomp on to turn it on and off. I was done reading and looked at this lamp, thinking that I shouldn't have to get out of bed just to turn it off. So, we started on the path of trying to teach Kyra to turn the light on and off.
00:03:44.550 At first, we took her paw and placed it on the switch. If she made contact, we rewarded her with a treat. Once she could walk over and actually press it, we added cues by saying 'light' and then rewarding her. After a couple of weeks, we got to the point where we could stand across the room and say 'light,' and she would turn it on for us.
00:04:34.110 This made me wonder about other things Kyra could do. Since I work for Twilio—known for its API that makes sending and receiving text messages quite easy—I thought, how do we get her to press a button and send me a text message? I realized I had never done any hardware hacking before; I only took one electronics engineering class in college and was terrible at it.
00:05:11.669 I liked software because of the instant gratification it provides, while the hardware world seemed intimidating. After some Googling, I discovered something called the Arduino Yun. I asked how many people have played with an Arduino before. A good number of hands went up.
00:05:57.169 The big difference between the Arduino Yun and the standard Arduino is that the Yun is Wi-Fi enabled and has a second processor that runs a stripped-down version of Linux called OpenWRT. Essentially, you can use it to execute Ruby scripts and interact with web APIs. The hello world of hardware hacking is simply making a light blink; the next step is doing that with a button.
00:06:46.199 The circuitry involved in pushing a button is straightforward and found early in the getting started guide. We control the Arduino Yun from the software side by SSHing into it. I started using the Arduino Yun primarily over the past year and a half, mostly with Python and a bit with PHP. I then started experimenting with Ruby, my language of choice.
00:07:22.080 Unfortunately, while it could barely run Python and PHP, it had limitations with Ruby, especially with installing gems. I was curious about other options and experimented with the Raspberry Pi, especially when others asked if I could do the same things with it as I did with the Arduino Yun. It turns out that the Raspberry Pi 3 costs half as much as the Arduino Yun—about $35 compared to the Yun's $70.
00:08:52.530 Arduino discontinued the Arduino Yun about a month ago, so I began playing around with the Raspberry Pi 3, and it has been an absolute joy! What I wanted to discuss for the rest of this talk is building a device that allows my dog to text me selfies.
00:09:14.040 Let's quickly go over the components we need, and then we'll write some Ruby code to power this project. First, we need a camera. The Raspberry Pi has an official branded camera that costs $40, which is more than the Raspberry Pi itself. Instead, I ended up using a standard USB camera that I had lying around, which only cost $20.
00:10:20.260 The Raspberry Pi 3 has four USB ports and simply works with the USB camera without needing to install anything extra. Kyra doesn’t have the ability to type, no matter how many treats we give her, so we needed a big red button for her to press, which cost $10. We then housed everything in a cigar box, which you can buy for about $2 from a local cigar shop.
00:10:55.580 Once they sell cigars, they treat the boxes as trash, making them beautiful and sturdy for hardware projects. This is what the internals look like. On the left, we have the Raspberry Pi, and you can see the button connected to the circuit. The circuit is simple—when you press the button, it sends a signal to one of the GPIO pins to register that current is flowing.
00:11:35.250 To make all this hardware work, we just need three steps: take a picture, upload the picture to the cloud, and send a text message with that picture. So, I thought we would write some Ruby code to accomplish this.
00:12:02.480 We created a new Ruby script called 'take_picture'. This is actually pretty simple. In reality, we are using a command-line utility called 'fswebcam' on Linux, which allows you to capture images directly.
00:12:28.500 By passing fswebcam a desired resolution and file name, we can easily take pictures. I attached the webcam to the Raspberry Pi, so I ran the command, and the webcam captured a picture of everyone in the audience.
00:12:55.780 Next, we needed to upload that picture to the cloud. While this wasn't strictly necessary for our use case, we wanted to use a service that could handle the load without requiring a low-power computer. We decided to use Dropbox because their API and documentation are very user-friendly.
00:13:38.310 To do this, we created a file called 'upload_picture' using the Dropbox SDK. After creating a Dropbox application and obtaining an access token, we created a new client in our Ruby code. With this client, we could open the picture file and upload it to Dropbox automatically.
00:14:42.190 Once uploaded, the Dropbox API gives us a path to the file. We can take that path and make another API call that gives us a public URL link to access the file. If we then run our code, we get a URL back to the uploaded image.
00:16:13.390 Next, we need to send the text message using Twilio. I purchased a Twilio phone number, which costs about a dollar a month. I then wrote a small Sinatra app that will handle HTTP requests. When someone texts the Twilio number, it will trigger the app and respond with a message, which thanks them for their text.
00:17:40.300 We use a simple webhook to connect Twilio to the Sinatra app. When you text the number, Twilio sends a request to our app that will send an HTTP response back containing the instruction to reply with our message.
00:19:21.580 Now, we must ensure Twilio can access the server running on my local machine. I opened a tunnel to the app using Ngrok, which provides a public URL to test my application. I configured this URL to point to my Sinatra app's '/message' endpoint so that we can receive texts.
00:20:10.520 Now, if you want to text your name to the Twilio number we purchased, you'll be able to see the requests come through on Ngrok as well as the Sinatra server. This integration allows you to text the Raspberry Pi and receive text messages in response.
00:20:46.500 I called for audience participation, encouraging people to text their names to see their responses or even any messages. This feature can also be extended for home automation, LED control, and much more, allowing you to text your Raspberry Pi.
00:21:48.520 Afterward, I created a new script called 'send_sms.rb' using the Twilio Ruby helper library. This script allows us to respond to incoming texts by sending a predetermined message back to the sender’s number.
00:22:57.590 Now that we have this back-and-forth communication set up, we can make it all work at the push of a button! I created another Ruby file named 'push_it.rb' that integrates everything into a simple workflow.
00:24:40.460 This file includes what we wrote earlier: taking pictures, uploading them, and sending text messages using Twilio. I also utilized the PiPiper library, which lets us interact with the GPIO pins on the Raspberry Pi.
00:25:31.350 So, I set it up so that when the button is high (pressed down), it triggers the functions to take a picture, upload the picture, and send the text message seamlessly.
00:26:44.510 During the live demo, I showed how pressing the button resulted in a picture of the audience sent through as a text message. The goal was to update the function to send pictures from Kyra, further illustrating the project.
00:28:00.520 I capped off the presentation by emphasizing that anyone can engage in hardware hacking with just a little bit of coding knowledge and willingness to experiment. Using simple tools, libraries, and APIs can yield incredible results—even teaching your dog to send you selfies.
00:30:30.550 All the services we used—Dropbox, Twilio, and the Raspberry Pi—are approachable and can break down complex tasks. I encouraged everyone to try hardware hacking; if you know just a bit of programming, you're equipped to tackle projects similar to what I've demonstrated. My name is Greg; thank you all for hanging out with me!