Introduction: Internet of Ducks

About: Sharing knowledge and ideas. Constantly learning!

Stuffed animals with push notifications

The Internet of Ducks

What's better than a soft and fuzzy stuffed animal? An internet connected stuffed animal!

To learn more about protocols for push notifications, I wanted a tangible "Hello World" and created a public, web-based app that can send a 'quack' to a physical stuffed toy animal located behind my router at home.

And thus the Internet of Ducks was born!


Push notifications

We hear a lot about the "Internet of Things," the idea that ordinary devices around us will be connected to the internet and each-other, capable of sharing information and creating new kinds of interaction.

I've made a lot of things that communicate with each other, or report back or pull data from some web service, but I wanted something that could receive new messages like a phone does a text. I didn't want to fuss with port forwarding on my router or continuously ping an end-point for updates, which could quickly drain batteries.

The solution to this is to open a persistent connection and "push" new updates to your device, but how exactly do we do this?

MQTT

After some research I found something called MQTT. This protocol was designed in the 1990s to transport data to and from low-power devices in bandwidth sensitive applications. It is used on oil rigs, satellites, Facebook Messenger, is now a published open standard with implementations for many devices and languages.

MQTT operates over TCP and works a bit like a chat server. Your device connects and subscribes to a channel hosted by a broker, when new notifications are available they are pushed to it. A device can also publish to a given channel or channels. Many brokers support connection via web socket, so your web applications can connect and publish or subscribe.

(There are other approaches you can take to this problem: use a hosted platform like PubNub, or use XMPP, which is what Android and iOS use to deliver application push messages).

Step 1: What You Need

* A stuffed animal, you can either sew one with a space for your electronics, or remove some stuffing from an existing toy animal.

* An embedded device capable of connecting to the internet. A Raspberry Pi or other single board computer makes this really easy, especially if you want to play MP3s, but it's a bit on the power hungry side. Another good choice would be a Spark, or an Arduino and wifi shield.

* Lithium battery pack, if you want your animal to be wireless.

Step 2: Construct / De-construct Your Stuffed Animal

Our stuffed duck was created from an excellent free pattern and instructions by Connecting Threads. It includes a lined 'belly' for the electronics that we fasten using hook-and-eye closures.

If you're not feeling quite so ambitious you can take an old stuffed animal, open it using a seam ripper, remove enough stuffing to fit your electronic components and hand sew it back together.

There are severalother Instructables that detail modifying stuffed animals that you may find helpful.

Step 3: Wire Up Your Electronics

The Raspberry PI is a little bit much for such a simple project, but it makes it very easy to connect to wireless and play sound.

The basic elements of what you need are covered below, though you can get creative and add all kinds of sensors, or things like a vibrator or LED. I'm using:

  • Raspberry Pi (Model A/A+ will suffice)
  • USB Wifi Dongle
  • Lithium USB Battery Pack
  • Small Speaker connected to an audio jack (salvaged from an old laptop)

Improvements for version 2.0:

  • Two way communication: A tilt switch or capacitive touch sensor would allow the user to 'quack back' those pinging it from the web client.
  • Smaller, less power hungry brain, perhaps a Spark Core or Arduino

Step 4: All About MQTT Brokers and Clients

Finding a broker

To start you need a MQTT broker. You can use a public one for testing or low volume use. If you're creating a project that is high volume, or you want privacy you should probably run your own! For demonstration purposes I'm using test.mosquitto.org, but if you'd like to host your own here are links to popular server software:

Client software

You will need to install a MQTT client on your stuffed animal and to work with your web app. For my example application the Raspberry Pi will run a python based client. My web app communicates with a MQTT broker over web sockets.

For your stuffed animal:

For your web app (MQTT over Web Socket):

(You can see my example application here. If you don't want to use MQTT the sample can also talk with PubNub, a hosted platform that operates in a similar way. They offer a basic free tier and a more expansive, free evangelism program for makers, students, startups, etc)

Testing

Mosquitto includes some helpful command line utilities you can use to troubleshoot and experiment with publish and subscribe features of MQTT. Here are a few examples:

To subscribe to everything on the server:

mosquitto_sub -h test.mosquitto.org -t "#"

To subscribe to the topic "instructables/":

mosquitto_sub -h test.mosquitto.org -t "instructables/"

To publish to "instructables/"

mosquitto_pub -h test.mosquitto.org -t "instructables/" -m "Hello, world!"

Step 5: Configure the Raspberry Pi

This guide assumes that Raspbian is installed on your Pi and that you can SSH into it.

ssh pi@raspberrypi.local

Configure wireless. Adafruit and Debian provide detailed resources for doing this at the command line.


You'll need git, pip, mplayer and the mqtt python library. Install them like this:

sudo apt-get install python-pip mplayer git

sudo pip install paho-mqtt


Clone the example application from Github

git clone https://github.com/danasf/quackquack


Type nano ~/quackquack/pyclient/mqtt_client.py, set the host and channel:

# setup
host = "test.mosquitto.org"
sub_channel = "instructables/"


Test the client with:

sudo python mqtt_client.py

(You can quit using Control-C)


Ok, great, let's make the client run at startup:

sudo nano /etc/init.d/quack


Add the following (a bare bones startup script) and save:

#! /bin/sh
#/etc/init.d/quack
python /home/pi/quack/mqtt_client.py >/dev/null 2>&1 &


Then make it executable and add to start-up:

sudo chmod a+x /etc/init.d/quack

sudo update-rc.d quack defaults

Step 6: Configure the Web App

The web application is very easy to setup.

Upload the files in the www/ folder of the sample application to your web host.

On the web app, change settings in js/mqttapp.js to reflect the same channel and broker you specified on the Pi.

This script uses web sockets so you will need to make sure that your broker can support connections over web socket.

var settings = {
channel: 'instructables/',
server: 'test.mosquitto.org',
port: 8080,
uuid: createUUID()
};

Step 7: Place the Electronics Inside of Your Animal and Enjoy!

Happy quacking or mooing [or insert animal sound of your choice here]!

I hope you have found this Instructable fun and I would love to see what you've created in the comments!! :-)

[The qucking duck in the video is wireless and battery powered! It lives behind my router and is being quacked from the open web. I'm using an external, powered speaker in the recording so the that sound is loud and clear.]

Enchanted Objects

Participated in the
Enchanted Objects