Topic: Python and Nabaztag

I've added a Python program that sends messages to the rabbit depending on which ear has been moved.  As well as the weather reader I mentioned in the other thread.  Check them out at www.mcgurrin.com/nabaztag.  They run wherever Python is installed (Mac, Windows, Linux, whatever).

Both also have Windows executables posted for Windows users that haven't installed Python.

Re: Python and Nabaztag

Have been playing with these two modules and the weather one is great - I've edited it to make my bunny speak in a slightly more EvilMiffy way though - but there's the beauty of it, it can be customised !
However when I run the ear sensor module I get this error from the python shell
"Traceback (most recent call last):
  File "(full correct file path on my machine edited out),EarSensor.py", line 4, in <module>
    import easygui as gui
ImportError: No module named easygui"

If I take that line of code out (I mean, I know not what I am doing, I just thought I'd give it a go and see what happened !) I get an error message from the api saying my serial number and token are not correct (but I know they are) so I'm guessing that line in the code is kinda vital (lol). Any thoughts ? Bearing in mind I'm totally stumbling about in the dark here, this is my first dip into Python !

Re: Python and Nabaztag

Thanks for trying it out!  Your instinct was correct, the import easygui line was a left over from another program I haven't posted yet, and isn't needed.  I've reposted the code without it. 

As for the other error, I'm not sure what the problem is.  Try to run python from the python interactive shell or IDE (either IDLE or PythonWin), and after you get the error, type sn and then return, and token and then return.   Your serial number and token should be printed out.  If not, let me know what you get instead.  That should help me debug it. 

Are you running Windows, Mac, or Linux?  That may also help me out.

P.S.: If you get the right serial number and token, except the last digit is missing on each, delete the [:-1] from the lines that say
sn=data[0][:-1] 
token=data[1][:-1]

and try that. 

Let me know what happens in any case!

Last edited by ViennaMike (2007-11-28 03:59:29)

Re: Python and Nabaztag

Just tried it and it works fine - no problems with tokens or serial numbers (I am running osx 10.5, should you wonder). The killer rabbit wakes. smile Thanks for making the code available !

Re: Python and Nabaztag

Just noticed something about the ear sensor script. It seems that even when the rabbits ears haven't changed position, the tss is still sent. If I run it in IDLE (which seems the only way for me to get it to run for some reason, if I use python launcher nothing seems to happen for me), the python shell log thing reports something like
"http://api.nabaztag.com/vl/FR/api.jsp?token=(my token)&sn=(my serial)&key=&ears=ok
<?xml version="1.0" encoding="UTF-8"?><rsp><message>POSITIONEAR</message><leftposition>5</leftposition><rightposition>14</rightposition></rsp>"
then it sends a tts to the api via the api url. When I run it again without changing the ear position, I get exactly the same thing
"http://api.nabaztag.com/vl/FR/api.jsp?token=(my token)&sn=(my serial number)&key=&ears=ok
<?xml version="1.0" encoding="UTF-8"?><rsp><message>POSITIONEAR</message><leftposition>5</leftposition><rightposition>14</rightposition></rsp>"
and yet it still then calls the api with the tss (sorry if I'm getting the terminology wrong, hope you can understand what I mean).
Not that I know about python at all, but it seems to me that it's not reporting the oldearposition file properly, or if it is, the tts is getting sent anyway.
Am I doing something wrong ?

Also, I've been trying to figure out how to send messages to a different rabbit if I move my rabbits ears. I have figured that changing the serial and token to that of a different bunny in the wxBunnyFile won't work because the program will be looking for THAT bunny's ear position, not mine, am I right ? I'm guessing some in depth code editing would need to be done to be able to do this !

Last edited by rachaelnex (2007-11-30 12:52:59)

Re: Python and Nabaztag

rachaelnex wrote:

Just noticed something about the ear sensor script. It seems that even when the rabbits ears haven't changed position, the tss is still sent. Am I doing something wrong ?

Try this: Open up the program in IDLE, and just before the comment line:
# Get ear positions

type:
print "old ear positions: ", oldPositions[0], oldPositions[1]

Then save and run.  This will print out whatever the program thinks the old positions are, and then we can work from there.  I just tried downloading the code I posted, and it's working fine for me, but this should help track down the problem.  Also, are you getting only the killer rabbit text to speech message, or also the "technology is beautful" mp3 file?  If just one, then apparently it's only thinking 1 ear has moved. 

Also, I've been trying to figure out how to send messages to a different rabbit if I move my rabbits ears. I have figured that changing the serial and token to that of a different bunny in the wxBunnyFile won't work because the program will be looking for THAT bunny's ear position, not mine, am I right ? I'm guessing some in depth code editing would need to be done to be able to do this !

Actually, just a little code editing, at least for a quick and dirty method:
After the

sn=data[0][:-1]  
token=data[1][:-1]

myNab = Nabaztag(sn, token)

lines of code, add:

otherSN = "put the SN for other rabbit in quotes"
otherToken = "put the token for other rabbit in quotes"

otherNab = Nabaztag(otherSN, otherToken)

then, near the end of the program, instead of myNab.say and myNab.send, use otherNab.say and otherNab.send

This creates two instances of the Nabaztag API object, one for your bunny, and one for the other.  It will poll your bunny for its ear positions, and if changed, send the appropriate message(s) to the other bunny.

Re: Python and Nabaztag

Thanks for your reply. This is all new to me as I said so - thanks for bearing with me.

I inserted the line as you said. The shell reported "old ear positions:  /u0 /u0", and sent both the tts messages (I changed my version of the code to say two messages rather than an mp3, one for each ear, as I can't figure out how to find the id number of my mp3s on the nab site since they changed it from being part of the url).
So I changed the ear position on the bunny and ran it again. Again it reported "old ear positions:  /u0 /u0". And sent the messages.
So I did it without changing ears this time and it reported the same ear position "/u0 /u0" and yet still the tts got sent to the api.

Any ideas ?

I put the otherNab code bits in and it sends the messages to my bf's bunny now, but still always despite the ear positions being reported as /u0 /u0.
As it goes, if I try to open the oldearposition file in text edit (just to see what it says) it's blank. It is supposed to be blank ?

Re: Python and Nabaztag

rachaelnex wrote:

Thanks for your reply. This is all new to me as I said so - thanks for bearing with me...  Any ideas ?

Rachael,
Try the file at www.mcgurrin.com/nabaztag/EarSensorTest.py, and let me know the results. It's hard to try to debug remotely, because I can't reproduce the problem on my machine.  If there was a problem reading the old data, the program defaulted to /u0/u0 (positions 0,0) for the ears.  This test version takes out that default, so if that's where the problem is occurring, you should now get an error message, which you can forward and we can work from there. 

Also, just before storing the new positions, it will print out the old and new positions, to help debug. 

When I run it without moving the ears, I get:

>>> http://api.nabaztag.com/vl/FR/api.jsp?token=##########&sn=############&key=&ears=ok
<?xml version="1.0" encoding="UTF-8"?><rsp><message>POSITIONEAR</message><leftposition>1</leftposition><rightposition>1</rightposition></rsp>
 
old positions:  [u'1', u'1']
new positions:  [u'1', u'1']

Then when I move the ears, I get:

 
>>>http://api.nabaztag.com/vl/FR/api.jsp?token=##########&sn=############&key=&ears=ok
<?xml version="1.0" encoding="UTF-8"?><rsp><message>POSITIONEAR</message><leftposition>14</leftposition><rightposition>14</rightposition></rsp>
 
old positions:  [u'1', u'1']
new positions:  [u'14', u'14']
http://api.nabaztag.com/vl/FR/api.jsp?token=##########&sn=############&key=&pitch=&tts=Hay%21++Who+woke+me+up%3F...+++Has+someone+been+moving+my+ears%3F++You+better+watch+out.+I+am+a+killer+rabbit.+I+have+a+vicious+streak+a+mile+wide%2C+and+nasty%2C+big%2C+pointy+teeth%21+.+.+Just+kidding%21&voice=Aaron22s&speed=
<?xml version="1.0" encoding="UTF-8"?><rsp><message>TTSSEND</message><comment>Your text has been sent</comment></rsp>
 
http://api.nabaztag.com/vl/FR/api.jsp?token=##########&sn=############&key=&idmessage=4063557
<?xml version="1.0" encoding="UTF-8"?><rsp><message>MESSAGESEND</message><comment>Your message has been sent</comment></rsp>

Hope this helps, and thanks for giving the programs a try!

Last edited by ViennaMike (2007-12-03 00:15:58)

Re: Python and Nabaztag

For Python programmers, I've significantly expanded the Nabaztag.py code written over at the Nabazlib project, so that it now supports all the capabilities of the Nabaztag API, including the ability to combine several commands into a single call.  It's available at http://www.mcgurrin.com/nabaztag/.

Re: Python and Nabaztag

Hi Mike,

Downloaded the code and gave it a try. I do get an error - here it is:

Traceback (most recent call last):
  File "/Users/rachael/Desktop/EarSensor-2/EarSensorTest.py", line 29, in <module>
    oldPositions = pickle.load(earFile)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 1370, in load
    return Unpickler(file).load()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 858, in load
    dispatch[key](self)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 880, in load_eof
    raise EOFError
EOFError"

Seems to point to something amiss with my pickles, lol. And as far as I know, they're things stored in vinegar and kept in a jar ... smile

Re: Python and Nabaztag

I'm still trying to figure out the problem.  Back when you ran the original program, I assume you ran it multiple times in a row, and still got the same problem?  Is that correct?  It seems like there's a problem in the pickle file (where it stores the ear positions between executions of the program), but with the original program, even if the original file was corrupted, it should default to oldpositions of 0 and 0, run once, send the messages, and then store the new positions in a valid file.  So when it runs again, it should have a good file to work from. 

We might want to take the debugging discussion off-line.  You can email me via the link on this thread, or to the email address on my website.

Re: Python and Nabaztag

I've completed the beta version of the Nabaztag Control Panel (NCP).  It's available at http://www.mcgurrin.com/nabaztag.  You must install Python on your machine, but Python is available (for free) for Windows, Mac's, Linux, etc.  It provides a graphical user interface to let you access at least 90% of the functionality of the Nabaztag/tag API, including building, saving, and sending choreographies, waking your bunny up (or telling him to sleep), sending text to speech, checking ear positions, moving the ears, etc.  It can remember the serial numbers and tokens for multiple bunnies, which are saved on your PC so that you don't have to re-enter them every time you run the program.

Try it out and let me know what you think, and also if you encounter any problems.

See my page for more information. 

P.S. Earlier probelms with the ear sensor program have been fixed.

Re: Python and Nabaztag

Tried out this Python based control panel as my first one.  got the python from python.org (win64 amd64 version) loaded it and the py and p files from viennamike here and it worked like a charm.

My plan now is to take a copy of this stuff to my computer at work so from there I can send ear messages back home for stuff like "im running late" or "be home at the usual time" etc.

I think this is going to be fun.

Thanks for creating the app!

Re: Python and Nabaztag

Sorry being a bit thick here not very techy. I have the Python shell but how do I get the py and p files into it? Python is a DOS application is it not....or am I being really stupid

Re: Python and Nabaztag

cymon wrote:

Sorry being a bit thick here not very techy. I have the Python shell but how do I get the py and p files into it? Python is a DOS application is it not....or am I being really stupid

You don't need to open a shell at all.  If you have Python on your machine, and have it set up to associate .py files with Python (which usually occurs automatically when you install Python), just unzip all the files into the same directory and then double-click the NabaztagGUI.py file, and it should run. 

The first time, you'll need to select "Add Bunny" and input your bunny's name, serial number, and token.  Then select "Select Bunny" and select the bunny by the name you just entered.  After the first time, you just need to Select Bunny upon startup, because the information you put in the 1st time is stored on the PC. 

If you want to monitor whats happening as it runs, open IDLE or PythonWin Editor (if you have it), and then open the NabaztagGUI.py file in IDLE or PythonWin and then run it.  But this is only really needed if you're debugging, modifying the code, or just curious.

Re: Python and Nabaztag

Great I got it to work...thanks very much.

Re: Python and Nabaztag

I've updated the python wrapper I wrote for the Nabaztag API (Nabaztag.py) so that it now correctly supports calls to play URL's of MP3 files. 

As reported elsewhere, the Nabaztag servers want a pure mp3 URL, not a pls file or other playlist type of file.  But for valid mp3 URL's like http://www.jonathancoulton.com/mp3/Chir … 0Prime.mp3 or
http://207.200.96.228:8000 it's working fine.

This also means that you can input the URL's using the Nabaztag Control Panel I wrote to go along with Nabaztag.py.

The code is available for download at http://www.mcgurrin.com/nabaztag/index.htm

My thanks to Philippe Guillet  for pointing out this shortcoming in the old version, and for giving me some good hints into how to make the wrapper and control panel fully Unicode compatible for international users (it's not there yet, hopefully next version).

Last edited by ViennaMike (2010-02-02 02:43:20)