Have you ever wanted to program a rabbit to spin it’s ears round and have it’s tummy flash? Of course you have! We have all been there, and using this tutorial I will be showing how using PHP and the Nabaztag API.

For those of you unsure of what I am talking about, perhaps thinking I am going to shove a lightbulb in a rabbit, have no fear as I am talking about the Violet.net product: Nabaztag. It is a wireless enabled device that connects to the internet independently and can communicate with servers to download information. It comes prebuilt with weather, stocks and other modules though the really interesting part comes when you begin to program your own applications using their API.

By sending a POST request you can start sending messages to your rabbit and it will convert it on-the-fly into speech with the option of a male or female voice and numerous languages. Within a few hours of getting it out the box I was able to create an application wrapper and a basic program to tell me when my friends signed onto Xbox Live. The example I will be going through below to create this application wrapper only uses simple functionality of the API to send basic messages, though full choreographic movements and light-shows, as well as music can be added.

Firstly you will need to activate your Nabaztag to be able to receive external requests and messages and you will need to get a copy of your Nabaztag serial number and token. Once you have registered your Nabaztag you can view this information from his ecosystem page.

We start by creating a PHP class that can be extended from, this will allow for all subsequent applications to quickly have access to this functionality and make the process much faster. We set up a small number of private variables that will store information such as our rabbits ID and application token.

<?php
class Nabaztag
{
	// Base connection URL
	private	$_conn_url	= 'http://api.nabaztag.com/vl/FR/api.jsp';

	// Login credentials ( CHANGE ME! )
	private	$_sn		= 'xxxxxxxxxxxx';
	private	$_token		= 'xxxxxxxxx';

	// Stores voice setting
	private	$_voice		= 'UK-Mistermuggles';
}
?>

Now that we have the basic variables set up we can start building some functions to allow us to interact with the class. We will start by adding some setter functions to allow us to update the private variables and perform error checking if need be.

	public function link( $sn, $tok )
	{
		$this->_sn		= $sn;
		$this->_token	= $tok;
	}

	public function set_voice( $v )
	{
		$this->_voice = $v;
	}

With access to our class variables we can start putting in the internal functions that will send calls to the Nabaztag API. In this tutorial I am just going through how to send a simple voice message to your Nabaztag, which will automatically generate a light and ear choreography for you. The API works using POST requests, so we can put together a somewhat simple cURL call that will process the request and retrieve the output. This next chunk of code is quite large so I have commented where logical to try and help.

	/**
	 *	Takes the message you want to be sent and creates query to send to Nabaztag API
	 *	The response will be in the form of an XML structure
	 **/
	public function simple_message( $text )
	{
		// Create query URL
		$this->_query	 = $this->_conn_url;

		// Nabaztag serial details and token
		$this->_query	.= '?sn=' . $this->_sn;
		$this->_query	.= '&token=' . $this->_token;

 		// Set end points of ears ( in default case, upright )
		$this->_query	.= '&posleft=0';
		$this->_query	.= '&posright=0';

        	// Random message token
		$this->_query	.= '&ismessage=' . rand( 0, 10000 );

        	// Pass application ID, not currently used in API
		$this->_query	.= '&idapp=0';

        	// Encode message
		$this->_query	.= '&tts=' . urlencode( $text );

        	// Perform query and return response
		return $this->_ping_url( $this->_query );
	}

	/**
	 *	Very simple cURL call to preform request
	 **/
	public function _ping_url( $query_url )
	{
    		// Create cURL instance
       		$ch = curl_init();

       		// Set parametres to run query and return the content as string
		curl_setopt( $ch, CURLOPT_URL, 				$query_url	);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER,	1			);

        	// Execute cURL call
		$output = curl_exec( $ch );

        	// Close cURL connection
		curl_close( $ch );

		return $output;
	}

And there we have it! Now to send a simple message to your Nabaztag you can simply run a class such as..

<?php
require_once( 'Nabaztag.class.php' );

class HelloWorld extends Nabaztag
{
	public function __construct()
	{
		parent::simple_message( "Hello World! Give me a carrot!" );
	}
}

$hw = new HelloWorld;
?>

And his ears will be spinning in no time. In the next parts to my Nabaztag posts I will be covering some real world examples and expanding on the wrapper class. If you would like to skip writing it all up yourself, you can download a copy of the wrapper (please note, this file is a work in progress ).

Tagged as: , ,