Well, you know you have one of those really weird conversations along the lines of
“We need a doorbell!”
“What about a wireless mouse will that work?”
You know it won’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’s how we did it….
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 “e”. Easy! Curses can check for a key press and do stuffs!
Here’s a few piccies of it being built.
You can kind of see the switch in the lid of the box here.
Here you can see the switch, battery pack and pcb wired up.
Here it is attached to the door.
The python code is very very basic! I’ll start to update it slowly and make it a bit better, but it works at the moment. I took some code from the pymedia tutorial and some from here. There are two files, one controls the sound, the other does everything else! They can be found (with usernames/passwords stripped) here. It’s also below for completeness!
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()
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 )
And there you have it, still needs a little work, but a working Bluetooth Jabber Doorbell.