Have you ever wondered how long your dog sleeps while you’re away at work? I have. As a hacker this seemed like the perfect excuse to strap a microcontroller to my dog, Gif, and see if I could find out. To make it a little more fun I thought I could have it text me when Gif wakes up to let me know how long he slept for. In this post I’ll show how to build your very own sleep tracker for your dog using Tessel and Twilio.
- Tessel Microcontroller + Accelerometer Module
- USB Battery Pack
- A Twilio Account (Sign up for Free!)
- A dog (I used this one)
We’ll start by setting some default values and requiring the libraries we need:
We set our twilio AccountSid and AuthToken and use them to initialize our Twilio Node module so we can send a text message. We then set some variables we’ll want to use when we send our message – the twilio number we texting from and the phone number you want to text to. We also need to install the Twilio library. Even though we’re doing this locally the library will get synced to the device when we push our code to the Tessel.
We’ll be using the Twilio library to send our message but to make things a bit easier later on we’re going to be wrapping this code in a function called sendText().
If you’ve worked with the Twilio node library before the rest of this code should look very familiar. We’re using the library to POST to the Messages resource to create a new SMS message. We do some basic error checking so we can make sure nothing went wrong.
Once you’re done, your code should look like this gist. Run this app on your tessel and send yourself a text message.
Now that we’ve built the functionality to send a text message from our Tessel we need to write the code to detect just how lazy your dog is being while you’re away. We’ll use the accelerometer to do this. First, plug in your accelerometer module to port A.
Since the accelerometer module isn’t included on the Tessel by default we need to install it. Specifically we’ll be using the accel-mma84 library to interact with the accelerometer:
Then initialize it in your code:
Let’s write some basic code to make sure our accelerometer is functioning correctly:
In this code we’re waiting until our accelerometer is ready and then we’re streaming the data as it comes in. The data we get back is an array of floats representing the acceleration on the X, Y and Z axes. If there’s an error, we’re logging that. If you’re getting an error try updating your firmware or resetting the Tessel by pressing the reset button right next to the microusb port.
Once we know our accelerometer is working we can start writing the code that detects if your dog is sleeping. We’ll be using a very basic algorithm to determine if our dog is asleep or awake. We’ll compare the last x-axis changed position to the current value of the x-axis on our accelerometer. If these values differ we know there’s been movement and determine how much time has passed . A movement after any period greater than 5 minutes we’ll consider sleep and send out a message about how long the dog slept.
Let’s set up a couple variables we’ll be using to track the movement intervals:
We’ll be using the last_movement and last_movement_time variables to track movement from the accelerometer and see how much time has passed between movements.
Let’s dive into the code we’ll use to detect when there’s movement with our accelerometer:
We want to update a couple ways our accelerometer is tracking data to optimize for our use case. Output rate determines how often we’re sampling data from the accelerometer. Since we’re interested in larger trends we’ll change the output rate to 1.56 Hz (the lowest available for the accelerometer) to sample less often. Similarly, we aren’t concerned with a super precise reading so we can set our scale to 8 Gs of acceleration. This may vary based on the dog you are using.
After the data starts streaming we compare our last movement to the current position of the x-axis on our accelerometer. We’re trimming this position to 1 decimal point because we’re not interested in very small movements. If there’s been a movement we’ll see how long it’s been since the last movement. This information is in milliseconds so we convert it to minutes. We probably only want to know if our dog has been sleeping for an extended amount of time, so we’re only triggering a text message if it’s been more than 5 minutes since the last movement.
If it’s been more than 5 minutes we call the sendText function we wrote earlier to notify you how long your dog has been sleeping.
Your final code should look like this. Let’s test it out by running it:
Once we’ve tested our app let’s use the tessel push command to push our app into Tessel’s flash memory. This will auto-run the app whenever the Tessel powers up.
Now strap this puppy on to your puppy (pun intended). I put mine on Gif’s Twilio hoodie so it would be more comfortable:
Now you should be notified how much your puppy sleeps. Here’s a peek into Gif’s sleeping patterns:
We successfully used the Tessel to enter the world of #DogHacking. What’s next? Maybe you could use the Tessel GPS module to find out your dog is somewhere they shouldn’t be or use the camera module to get updates of what your dog is seeing in real-time. Have any questions or want to show me how cute your dog is? Reach out on twitter or via e-mail.