How to make an internet Stuffed Toy with Raspberry Pi and Carriots

 

0. Introduction

This easy tutorial helps you to build an internet Stuffed Toy with Raspberry Pi and Carriots. Perhaps this can help you if you want to participate in our challenge

To do this, we use:

  • 1) A Raspberry Pi. We use this to detect when the toy buttons are pressed.
  • 2) The Carriots Engine to post tweets , change voice , download videos from your Dropbox account and play those videos.

In this project you will learn how to:

  • Connect a Raspberry Pi to Carriots to post tweets using listeners and the SDK.
  • Connect a Raspberry Pi to Carriots to send data streams and change
    the toy voice using Carriots HTTP REST API.
  • Connect a Raspberry Pi to Carriots and download videos from your Dropbox account using Carriots HTTP REST API.
  • Make an easy video player with mplayer and Raspberry Pi.

Difficulty:

Ingredients

  • 1 x Raspberry Pi. An alternative is to use an Arduino Uno
  • 1 x Tiny Breadboard
  • 4 x Resitance. Range between 200KΩ and 10KΩ
  • 4 x Push Button Switch
  • 1 x Meter of cable
  • 1 x Stuffed Toy
  • 1 x Screen
  • 1 x Speaker
  • 1 x Case. Optional. Not needed during experiment

Music to listen: The Domo Song

TOP

1. Project Schema

This project is a prototype of a stuffed-toy connected to Internet through the Carriots platform. When you push different parts of the Domo-kun, different actions will be run.

Here is overall diagram of the project:

  • Schema

Functionalities

  • Synchronize with your devices custom properties and reproduce audio
  • Synchronize with your Dropbox account and download your video files
  • Post a Tweet in your Twitter account
  • Play the downloaded videos

TOP

2. Raspberry Pi. Connections description

Every button is connected to a pin and the other side to 0V. The pin is connected to a resistor. When the pin is set to input mode and value is read, it is going to be read in a logic high value (or 1 in our number terms), and when the button is pressed the pin is going to connect to 0v, and then the input will read a logic low value, or zero.
Each button connection follows this way:
3,3 V -> 10K Resistor -> GPIO -> Switch Button -> GND.

The connections in Raspberry Pi are extremely simple. You can see in the diagram below.

  • Diagram

TOP

3. Teddy Construction

Choose your toy and empty the content

In this case, we have selected a Domo-kun. It's perfect because it's big and has a great mouth to put a screen.

  • Open the stuffed-toy
    • Open toy
  • Empty its content
    • Empty
  • Cut out the mouth to put the screen
    • Cut out the mouth

TOP

Make connections

In this case we have used a raspberry pi, a screen, a speaker, four buttons and a box. To power the raspberry pi and the screen, we use external batteries.

You can see the result below.

  • Diagram

TOP

4. Carriots Project and Device Registration

Device

If you're registered in Carriots, you have a default device already created for you.
Check your control panel and see what it looks like.
Basically you need the device id_developer that might be something like defaultDevice@myusername.
But if you want, you can create a new device and use it in this example.

Apikey

Now, go to your control panel “My account menu” and check your Apikey.
It's a big alphanumeric token like:
98346673a6377ef1fde2357ebdcb0da582b150b00cabcd5a0d83045425407ab4.
You need this number to complete the example.

TOP

5. Code implemented on Raspberry Pi

This is all the Python code for Raspberry Pi. Note that some values should be replaced by your own values.
In the following sections, each of the features in detail is explained.

This project has been tested on Raspbian "wheezy", and we have used the next components:

All packages can be installed using APT.

Also, for a cleaner code, we use clicarriots, a "api wrapper library" developed by one of our core developers. You can download and know more about it here: clicarriots

 

TOP

6. Synchronize with Carriots and reproduce audio

In this case, when you press one of the arms of the stuffed-toy, you get the phrase you want to play from Carriots.

Customize your device's properties

You have to create a new property to store the phrase your toy says .
For this, go to "Device Management"->"Devices" and select your device.

  • Select device

Now, to create a new property, click on the "edit" button and then click on the "add another property" button.
Choose a key to identify this property, in our case "voice", and write a phrase in "value" field. In our case "my first phrase". Click "save" button".

  • Add property

Code

  • Make a call to external API Carriots which returns the device information. Here you can find more info : Device Management / Show
  • As a result you obtain a JSON OBJECT response. Look for the property previously saved on your device in this response. In our example the property previously saved is "voice".
  • Encode the value of the property previously save using URL Encoding and do a GET request to Google Translator API. This API returns an audio file format which you can convert a .mp3 file. Here you can find more info : https://developers.google.com/translate/?hl=en
  • Now you can play the mp3 file with mpg123.

The code that controls this functionality is this:

TOP

7. Synchronize with Dropbox and download videos

In this case, when you press one of the legs of the stuffed-toy, we are making a request to Carriots External Api. This will return a JSON with the URLs of the existing videos in your dropbox folder "App/Carriots".

Sync your Dropbox account with Carriots

First we have to sync a Dropbox account with Carriots, this is explained in the following link: (Authorize Carriots to use your Dropbox account)

Code

  • Make a call to external API Carriots Dropbox which returns a set of URLs to your videos. More info here : External apps / dropbox
  • As a result you obtain a JSON OBJECT response. You can explore the response with a simple loop like in the example.
  • Download the videos and store in your local folder. You can make a sorted playlist for mplayer.

The code that controls this functionality is this:

TOP

 

8. Post a Tweet in your Twitter account

In this case, when you press the other leg of the stuffed-toy, we are sending a stream to Carriots through the External Api.
For this example, we are sending a stream with key-> p1 and value->yes

Code

  • Send a stream using Carriots HTTP REST API. Read more : Send streams
  • Create a Listener to send a Tweet using the SDK. Connect your Twitter account to Carriots. More info here : Create a listener and App Twitter

The code that sends a stream is this:

Customize your device's properties

We have to create a new property to store the tweet to post.
For this, go to "Device Management"->"Devices" and select your device.

  • Select device

Now, to create a new property, click on the "edit" button and then click on the "add another property" button.
Choose a key to identify this property, in our case "voice", and write a phrase in "value" field. In our case "my first tweet". Click "save" button".

  • Add property

Create a listener to tweet

A listener in Carriots can be associated to any hierarchy entity, from Project to Device. If you associate a listener to a project, all devices below the services of that project will be affected. If you associate a listener to a service, all devices below the groups of that services will be affected. And so on.

In this example, we create a listener associated with our device. This listener waits for an event to occur in the context of the device and then evaluate the content.

To create a listener go to the control panel, then to “Device management” → “Devices”. Locate your device and click on the name. Then click on the new button in the Listeners tab.

  • Create a listener

Fill the fields with the following values and save the new listener

  • Name: The name that you prefer
  • Description: The description that you prefer
  • Entity type: Device
  • Entity id: Your id device
  • Event to listen: data_persisted
  • If expression:
    context.data.p1=="yes"
  • Then expression:
    import com.carriots.sdk.Device;
    import com.carriots.sdk.utils.Twitter;
    def device = Device.find('YOUR ID_DEVICE HERE');
    def tw = new Twitter();
    tw.status=device.device_properties.status;
    tw.send();
  • Then rule: leave empty
  • Else expression:leave empty
  • Else rule: leave empty
  • Be sure to have your listener enabled if you want it to be executed

Note that in "Then expression" we are using Carriots SDK. If you want to know more about it or execute another action when receiving this stream, see our SDK Utility libraries.

TOP

9. Play the downloaded videos

In this case, when you press one of the arms of the stuffed-toy, the videos which are stored in Domo-kun will be played.

Code

  • Now you should have already downloaded the videos from your Dropbox account. Let's play them!
  • In this example, when you press the button for the first time the video playlist starts.
    If you press the button again, the next video is played.
    When the playlist is over a message for the user is shown.
  • We use mplayer, auxiliary variables to control the code flow and linux pipes. You can read more : http://www.mplayerhq.hu/DOCS/HTML/en/index.html

The code that plays videos is this:

TOP

10. From here on: Enhancing the project

Ideas to enhance your stuffed toy:

  • You can add a thermometer to the toy and track the evolution of the baby's temperature to program alerts in case of fever
  • By adding a microphone or camera to the toy, you can have a baby monitor for your mobile phone
  • If the toy is for a child,you might program some apps to teach him maths, the alphabet or words and then report activity to his parents
  • Take a look at http://www.jerrythebear.com/ They created a great Stuffed Toy to help kids with Diabetes. Do you know any other case of disease where a toy can help the children to deal with it?
  • A simple GPS plus the toy will help you to keep track of your child location

TOP