How to make an internet Stuffed Toy with Raspberry Pi and Altair SmartWorks

0. Introduction

This easy tutorial helps you to build an internet Stuffed Toy with Raspberry Pi and Altair SmartWorks. 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 Altair SmartWorks 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 Altair SmartWorks to post tweets using listeners and the SDK.
  • Connect a Raspberry Pi to Altair SmartWorks to send data streams and change
    the toy voice using Altair SmartWorks HTTP REST API.
  • Connect a Raspberry Pi to Altair SmartWorks and download videos from your Dropbox account using Altair SmartWorks 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

TOP

1. Project Schema

This project is a prototype of a stuffed-toy connected to Internet through the Altair SmartWorks 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. Its perfect because its 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. Altair SmartWorks Project and Device Registration

Device

If youre registered in Altair SmartWorks, 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.
Its 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 Altair SmartWorks 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 Altair SmartWorks.

Customize your devices 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 Altair SmartWorks 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 Altair SmartWorks 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 Altair SmartWorks

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

Code

  • Make a call to external API Altair SmartWorks 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 Altair SmartWorks through the External Api.
For this example, we are sending a stream with key-> p1 and value->yes

Code

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

The code that sends a stream is this:

Customize your devices 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

We have enhanced the listener creation process with our Flow Tool.

A listener in Altair SmartWorks 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 “Rules” → “Listeners”. Then click on the new button located after the list.

Fill the fields with the following values and continue to the next step.

  • Name: The name that you prefer
  • Description: The description that you prefer
  • Entity type: Device
  • Entity id: Your id device
  • Event to listen: Event Data Persisted
  • Be sure to have your listener enabled if you want it to be executed
  • Click on the "Next Step: design the code" button to visually design the listener functionality
  • Listener creation

In the Flow editor we'll need to follow these steps:

  • Step 1: Drag and drop in the editor workspace a "switch" node and Double-click it to configure it with the values:
    Label: descriptive name of the node in the editor view. For example "If tweet"
    If condition: select "if" and fill in the following field with the code context.data.p1=="yes"
    Click on the button "Done" to apply the changes.
  • Step 1: Switch node
  • Step 2: Drag and drop in the editor workspace a "find entity" node and Double-click it to configure it with the values:
    Label: descriptive name of the node in the editor view. For example "Find device"
    Entity: select "Device"
    Id_developer: here you could directly type the id_developer of the searched entity (defaultDevice@exampleuser.exampleuser). But let's be more generic and configure this node to use the device id_developer contained in the metadata of the streams that will trigger this listener: select "metadata" in the dopr-down associtaed to this field and then "Device".
    Custom var: descriptive name of the variable where the device will be stored. For example "my_device".
    Click on the button "Done" to apply the changes and connect the nodes.
  • Step 2: Find entity node
  • Step 3: Drag and drop in the editor workspace a "send tweet" node and Double-click it to configure it with the values:
    Label: descriptive name of the node in the editor view. For example "Send Tweet"
    Status: we want to tweet "status" property of the device we searched for and stored in "my_device" in the previous step. For this, select "$ variables" in the drop-down (so that the field content will be treated as a variable name, not as a string) and type my_device.device_properties.status
    Click on the button "Done" to apply the changes and connect the nodes.
  • Step 3: Send tweet node
  • Step 4: Click on the "Save" button and check the listener data in the "Show" screen. You can have a look at the generated source code and use it later for debugging purposes.
  • Step 5: Resulting listener + source code

It's time to test our listener!!!

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. Lets 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 babys 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