Updated IOT Dog Sensor: Adafruit IO

Earlier this year in a previous post I outlined a fun little project I did to help alert my wife and I when the dog needs to go outside, using a couple Adafruit Feather Huzzah boards, some components, and a small laravel site. Since then I’ve simplified the project a ton replacing the “receiver” board which would play a sound by instead having the process call a cell phone to alert us using a mixture of Adafruit IO and IFTTT. It halves the amount of hardware needed and eliminates the need for spinning up a small laravel website as well. With it being more manageable, I figured it’d be a great time to open source this project as well. Keep reading to see how this project is made and how you make it yourself.

The goal of the product again was that when my dog rang her bells attached to the back door to go outside, my wife and I would get an alert about it. We were having problems with hearing her using the bell at night. So in this version, the dog bumps the bells with her nose, a sensor is attached to the bells which connects to Adafruit IO to record this, and we’ll use an IFTTT integration with Adafruit IO to call my cellphone to let me know the dog wants out. While I made this to monitor my dog needing to go out, it could be easily adapted to any soft of sensor triggering a phone call. Let’s get started.

Materials needed

Here’s a list of the materials used in creating this. While you could get this from a number of different places, I got all of this from https://www.adafruit.com.

Step 1 – Assembling the Electronics

The electronics setup on this is actually pretty easy because the Wifi connectivity we’ll be using is integrated into the Feather Huzzah. Really, we just need to connect the LIS3DH accelerometer to the Feather using some wires. Check out both the Fritzing diagram below, as well as the photo of my soldered together board. While you could do this on a standard breadboard, it’ll work better having it all soldered together, especially since it’s going to get bumped around and we don’t want any wires coming loose.

With mine, I needed to solder the headers onto the LIS3DH before I soldered it to the perma-proto board. If you need help with this, check out this awesome tutorial at https://learn.adafruit.com/adafruit-lis3dh-triple-axis-accelerometer-breakout/assembly.

 

Dog Sensor Fritzing diagram

 

Step 2 – Setting up Adafruit IO

The backbone of this project is going to be running on https://io.adafruit.com which is a sort of general REST api. Previously I had this running on a custom laravel site, but by using Adafruit IO, it’s one less thing I need the maintain, and one less thing for someone else to setup. We’ll have our Feather Huzzah communicate with Adafruit IO to record when the dog bumps the accelerometer. Head over to https://io.adafruit.com and create a new free account.

Creating a Feed

Once you’ve got an account, the first thing you’ll need to do is create a new feed to hold our data. Think of a feed as just a container for data. You can supply it a value and it’ll store it for a given time (it can do more, but that’s what we’ll be using it for here). Click on the Feeds link, and then go to Actions > Create a new Feed. In the modal window that pops up, give it the name “Dog Sensor” and hit the create button. It really doesn’t matter what the name of the feed is, but that’s the name I’ll be using here and in the sample code.

Creating a Dashboard

Dashboards within Adafruit IO are ways to visualize the data from feeds, or interact with it. While not required to make this project work, we’ll use this to help visualize that the sensor is actually working correctly. Click on the Dashboards link and go to Actions > Create a new Dashboard. Give it the name “Dog Sensor”, and then click on your newly created Dashboard, and you should be presented with a pretty unimpressive back screen. In the buttons in the upper right, select the plus icon to add a new “block” to your dashboard.

There’s a lot of these little block widgets you can add to your dashboard, but we’ll just be using the Stream block (the thumbnail looks like green text). Click on the icon. Next, you’ll choose which feed to use as the data for that block. Check the box next to the Feed we just created and hit the Next Step button. The final step in setting up the presentation of the widget. You should be able to leave the defaults as they are and hit the Create block button. That’s it! You’ve now got a little place to store your data and display it when the dog bumps into the sensor.

Step 3 – Setting up IFTT

So far we’ve got this setup so that the data will be stored and we could view it if we wanted, but that doesn’t really get us to a phone call. So to do that, we’ll be using IFTTT to bridge the gap between Adafruit IO and the phone. Start by heading over to https://ifttt.com and create a new free account. Once you’re signed up, start by making a New Applet. You’ll be presented with a screen like that below. Click on the blue THIS text.

ifttt-step-1

From here’s you’ll choose the service we want to integrate with, and really, what’s going to kick off this process. Find the Adafruit service and click on it. Next, click on the block for Monitoring a feed.

Next you’ll be presented with a simple form to connect to the Feed you created in Adafruit IO. Select that feed, set the relationship to Equal To, and the Value to 1, and then hit the Create Action button. Next, we’ll need to set what happens when our Feed gets a value of 1. On the next screen, click the big blue THEN text to choose an action. Choose the Phone Call service (if you wanted, you could also have it send a text message!). In the options for this action, you can specify text that will be spoken when you answer the phone. Since my dog’s name is Kona, I set the text as “Kona needs to go outside!”. Make it whatever you want, and create the action. That’s it! When Adafruit records a new datapoint in that dog sensor feed that equals 1, you should get a phone call.

An important piece to note is that you might see a delay between that data being recorded and the phone call occurring because you’re at the mercy of how often IFTTT polls Adafruit for you. I’ve found the delay is typically anywhere between 4 and 15 seconds so if this is something that requires immediate response, IFTTT might not be the best option for you.

Step 4 – Code

So far the hardware is done and our cloud related integration is done, but we’ve yet to get any code on our board to make the whole thing work. Since the Feather Huzzah supports it, I decided to do this project using Circuit Python instead of the normal Arduino code + IDE as a way to test out the process, which has been pretty slick. However, there is a bit of a setup to get this to work and get Circuit Python flashed onto the board. If this was using one of the cool M0 boards like the new Trinket M0 from Adafruit we wouldn’t have to do this as they come with Circuit Python already loaded (plus you can view the files like an external drive), but with this Feather Huzzah that’s not the case. So the first step will be flashing the Feather Huzzah to use Circuit Python. There’s already a pretty good guide over at https://learn.adafruit.com/micropython-basics-how-to-load-micropython-on-a-board?view=all#esp8266 that you should follow. The directions are for Micropython but work fine for Circuit Python (in fact, Circuit Python is a fork off of Micropython). Basically you’ll be using a command line tool called esptool to do the actually flashing of the board. Once you’ve got the board flashed, let’s proceed onto installing Ampy.

Now ampy is the command line tool that’s used to interact with the python files and folders on the board. You’ll edit these on your computer, and then “push” them over to the board itself. This article on Adafruit has a great walkthrough on installing Ampy on your computer: https://learn.adafruit.com/micropython-basics-load-files-and-run-code/install-ampy. Once you’ve got ampy installed, you’ll need to download the project code and libraries we’ll need as well.

Start by downloading the following code / libraries:

  1. The main dog sensor code: https://github.com/stevenquinn/circuit-dog-sensor/archive/master.zip.
  2. Micropython libraries: https://github.com/micropython/micropython-lib/archive/master.zip
  3. Adafruit LIS3DH library: https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH/archive/master.zip
  4. Adafruit Bus Device: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice/archive/master.zip

Next, let’s load the libraries we need onto the board first. We’ll do that using the ampy tool that was installed earlier. We’re not going to need all of these libraries, but just a few within these packages. Let’s start by adding the umqtt library which is found inside the micropython-lib library within the umqtt.simple folder. Use the following command, but you’ll need to make sure you update the path to the library, as well as the appropriate port for your board:

ampy --port /dev/tty.SLABUART put micropython-lib/umqtt.simple/umqtt /

Next, we’ll do the same thing to add the bus_device and LIS3DH libraries:

ampy --port /dev/tty.SLABUART put Adafruit_CircuitPython_LIS3DH-master/adafruit_lis3dh /

ampy --port /dev/tty.SLABUART put Adafruit_CircuitPython_BusDevice-master/adafruit_bus_device /

Cool, now you should have all the libraries we need on the board. Next, we just need to modify the main dog sensor code before loading it on the board. Go ahead and open circuit-dog-sensor/main.py in the text editor of your choosing. If you scroll to the bottom of the file, you’ll see a number of variables declared that you’ll need to update. Start by updating the wifi_ssid and wifi_password with the name and password of your WIFI network your Feather Huzzah will be connecting to. Next, update adafruit_username with your Adafruit IO username, and adafruit__aio_key with your AIO key. Finally, update adafruit_feed_key with the key / name of the Feed you created on Adafruit IO. Mine was named dog-sensor. With that updated, run the following command to add this file to your board. Circuit python will automatically run the file named main.py when the board gets power.

ampy --port /dev/tty.SLABUART put circuit-dog-sensor-master/main.py /main.py

Step 5 – Test

You should be good to plug in your board and give it a test. When powering on, it waits 30 seconds before attempting to read a “bump” so you don’t set it off right away. Wait 30 seconds, and then give it a nudge. If it all works, you should see the data point created in your AdafruitIO dashboard and receive a phone call! To make it a little more obvious for me when the call was coming in from the sensor, I went ahead and made a new contact on my phone and just called it “Dog Sensor” so I don’t have to actually answer the call to figure out what’s going on.

If you’re having trouble, you can plug your Feather Huzzah into your computer and use the screen /dev/tty.port_name_here 115200 to read debugging messages from the code. Enjoy and check back for more tutorials!

SaveSave

SaveSave

SaveSave

SaveSave

SaveSaveSaveSaveSaveSaveSaveSave

SaveSave