Building Your Dog A 💩 Button with a Raspberry Pi, Node.js and Twilio

June 09, 2017
Written by

FeaturedDoggo

File this under: better to have than not.

Trevor Miller built a button that his dog, Oli, presses when he has to 💩.

Trevor hasn’t revealed Oli’s training regimen which led him to being such a good boy. But, Trevor will gladly show you how to build a IoT powered button using Twilio and Raspberry Pi in Node.

Here’s the code that does a lion’s share of the work.

require('dotenv').config()
const Gpio = require('onoff').Gpio
const twilio = require('twilio')
const debounce = require('debounce')

const {
  ACCOUNT_SID, 
  AUTH_TOKEN, 
  FROM_PHONE_NUMBER, 
  TO_PHONE_NUMBER,
} = process.env

const button = new Gpio(3, 'in', 'falling')

const phone = new twilio(ACCOUNT_SID, AUTH_TOKEN)

const sendMessage = debounce(() => {
  phone.messages.create({
    from: FROM_PHONE_NUMBER,
    to: TO_PHONE_NUMBER,
    body: 'I need to go to the bathroom!', 
  }) 
}, 10000, true)

button.watch((err) => {
  if (err) {
    throw err
  }
  sendMessage()
})

process.on('SIGINT', () => {
  button.unexport() 
})

Here’s how it works on a high level. When the button is pressed, the Raspberry Pi kicks off a set of Node.js code that shoots a request to Twilio’s API and sends a text to Trevor. Simple enough.

The parts are pretty minimal, too.

  • Raspberry Pi 3
  • 8+ GB micro SD card pre-loaded with NOOBS
  • Raspberry Pi 3 case
  • 2.5A 5V micro USB power supply
  • Momentary button with built-in resistor and GPIO wires
  • An HDMI cable, USB keyboard, and USB mouse.

All the code you need has been graciously shared by Trevor on GitHub right here.

The possibility of letting little doggos communicate with humans on a more nuanced level excited Trevor and inspired him to build this hack. He knew he wasn’t alone.

Trevor wrote an incredibly thorough post on his blog walking you through assembling the Raspberry Pi, and all the ins and outs, with video walk-throughs.

“People have been intrigued with the idea, especially with the possibilities for dogs (that they could communicate with us more). The programming community on Twitter and through my newsletter has liked seeing how to use the latest versions of Node and npm on a Raspberry Pi.”

Thank you to Trevor for building this hack. And to Oli, good boy.