<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matt Daubneys Blog &#187; bluetooth</title>
	<atom:link href="http://daubers.co.uk/category/bluetooth/feed/" rel="self" type="application/rss+xml" />
	<link>http://daubers.co.uk</link>
	<description></description>
	<lastBuildDate>Sat, 31 Dec 2011 17:08:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Bluetooth XMPP Doorbell</title>
		<link>http://daubers.co.uk/2008/02/06/bluetooth-xmpp-doorbell/</link>
		<comments>http://daubers.co.uk/2008/02/06/bluetooth-xmpp-doorbell/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 22:29:42 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[bluetooth]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[jabber]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://daubers.co.uk/?p=20</guid>
		<description><![CDATA[Well, you know you have one of those really weird conversations along the lines of &#8220;We need a doorbell!&#8221; &#8220;What about a wireless mouse will that work?&#8221; You know it won&#8217;t end well. This ended with the destruction of a bluetooth keyboard, a trip to maplins and the creation of a Python based doorbell. Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Well, you know you have one of those really weird conversations along the lines of</p>
<p>&#8220;We need a doorbell!&#8221;</p>
<p>&#8220;What about a wireless mouse will that work?&#8221;</p>
<p>You know it won&#8217;t end well. This ended with the destruction of a bluetooth keyboard, a trip to maplins and the creation of a Python based doorbell. Here&#8217;s how we did it&#8230;.</p>
<p>I initially investigated using a mouse to act as a button, as a wireless mouse was kicking around, but problems with curses and the mouse caused a rethink. A bit of investigation led me towards the fact that hacking a keyboard is quite easy. The flimsy on a keyboard just crosses some terminals that selects the character pressed from a kind of table. So if I connected a switch to two of these I could press a button which would be recognised as the letter &#8220;e&#8221;. Easy! Curses can check for a key press and do stuffs!</p>
<p>Here&#8217;s a few piccies of it being built.</p>
<p><img src="http://farm3.static.flickr.com/2154/2246633109_9737364a06.jpg?v=0" alt="With the added battery pack, and the box" align="middle" height="335" width="500" /></p>
<p>You can kind of see the switch in the lid of the box here.</p>
<p><img src="http://farm3.static.flickr.com/2089/2247426888_dd5b157f9d.jpg?v=0" alt="All connected" height="335" width="500" /></p>
<p>Here you can see the switch, battery pack and pcb wired up.</p>
<p><img src="http://farm3.static.flickr.com/2353/2246631815_c48edd9f1e.jpg?v=0" alt="On the door" align="middle" height="500" width="335" /></p>
<p>Here it is attached to the door.</p>
<p>The python code is very very basic! I&#8217;ll start to update it slowly and make it a bit better, but it works at the moment. I took some code from the <a href="http://pymedia.org/tut/play_wav.html">pymedia tutorial</a>  and some from <a href="http://snippets.dzone.com/posts/show/618">here</a>. There are two files, one controls the sound, the other does everything else! They can be found (with usernames/passwords stripped) <a href="http://daubers.co.uk/~matt/bell.tar.gz">here</a>. It&#8217;s also below for completeness!</p>
<p>main.py</p>
<pre>import curses, xmpp, snd, time
emails = ['contact1','contact2']

wavfile = 'chooseyourownbell.wav'
login = 'USER' # @gmail.com
pwd   = 'PASSWORD'

cnx = xmpp.Client('googlemail.com', debug=['never'])
cnx.connect( server=('talk.google.com',5223) )
cnx.auth(login,pwd, 'daubbell')

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.scrollok(True)

while 1:
    c = stdscr.getch()
    if c == ord('q'): break
    elif c == curses.KEY_MOUSE :
        stdscr.addstr("mouse")
        stdscr.refresh()
    elif c == ord('e'):
        for addy in emails:
            cnx.send( xmpp.Message( addy ,"Door!" ) )
            stdscr.addstr(time.ctime() + ': pinged ' + addy + '\n')
        snd.playWAV(wavfile)
    else:
        stdscr.addstr(time.ctime() + ': Ummm... key not found \n')
        stdscr.refresh()
curses.endwin()</pre>
<p>snd.py</p>
<pre> def playWAV( fname ):
  import pymedia.audio.sound as sound
  import time, wave
  f= wave.open( fname, 'rb' )
  sampleRate= f.getframerate()
  channels= f.getnchannels()
  format= sound.AFMT_S16_LE
  snd1= sound.Output( sampleRate, channels, format )
  s= ' '
  while len( s ):
    s= f.readframes( 1000 )
    snd1.play( s )

  # Since sound module is not synchronous we want everything to be played before we exit
  while snd1.isPlaying(): time.sleep( 0.05 )</pre>
<p>And there you have it, still needs a little work, but a working Bluetooth Jabber Doorbell.</p>
]]></content:encoded>
			<wfw:commentRss>http://daubers.co.uk/2008/02/06/bluetooth-xmpp-doorbell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

