2009-12-18
Damn, I burnt my oatmeal again.
The concept of time is not something I have a great abundance of (anyone that has heard me play music can probably attest to this) and, quite often, I fail to be mindful of the time whilst cooking some delicious steel cut oats for breakfast.

If only there was some way to be reminded to check the stove after a certain amount of time; without having to purchase a Talkie Toaster. Yes, I know all about wind-up egg timers, but mine broke a week or four ago.

The Software Solution
In a nutshell, I needed a timer that will alert me when a set amount of time has expired. The easiest way to do this is to ssh into my (almost) always on media player machine and run
sleep 240; espeak "Hey Jezra, it is time to check the oatmeal"
Yea, it works, but what if I don't want to maintain an ssh session? It would be so much nicer if ,on my local machine, I could just run a script to send the time and text to the media player machine. Espeak, by the way, is a nice text to speech application.

Enter the Python
Using Python socket programming I wrote two pieces of software; one to listen and parse commands, and one to send commands from a user.

First is the code for the listener, which is launched at system startup
#!/usr/bin/env python import socket import subprocess import sys HOST '' #we don't care about the specific hostname               PORT 5000 #pick a port       '''define the Server class'''         class Server:     def __init__(self,hostport):         #create and bind to a socket         self.sock socket.socket(socket.AF_INETsocket.SOCK_STREAM)         self.sock.bind((HOSTPORT))         self.sock.listen(1)              def run(self):         waiting True         data=""         #get a connection created when the socket accecpts input         connaddr self.sock.accept()         print "connected"         while waiting:             # read a 4K of data from the connection              data conn.recv(4096)             if not data:                 # if there is no data, then we end the loop                 waiting False             else:                 print "received %s" % (data)                 #parse the data we have received                 result self.parse_data(data)                 #send a response to the sending client                 conn.send(result)         #close the connection         conn.close()         #run again to create a new connection         self.run()              def parse_data(self,data):         try:             #split the data into the seconds to delay, and the string to speak             seconds,string data.split(":")             #what command do we need to perform?             command "sleep %s; espeak \"%s\"" % (seconds,string)             #run the command             subprocess.Popen(command,shell=True)             return_string "OK"         except:             return_string "Error with data: %s" % (data)         print return_string         return return_string                   if __name__ == "__main__":     server Server(HOST,PORT)     server.run()


On to the sender
#!/usr/bin/env python import socket import sys #what happens where there is an error? def quitmessage ):     print message     sys.exit() #do we have enough args? if len(sys.argv)==2:     string sys.argv[1]     seconds "0"      elif len(sys.argv)==3:     string sys.argv[2]     seconds sys.argv[1]      else:     quit("sender requires 1 or 2 arguments, you passed %i" % (len(sys.argv)-1) )      #if we haven't quit, connect and send the data HOST 'player'  #what is the name/IP of the server we need to connect to PORT 5000  # what port are we connecting on? #create a socket sock socket.socket(socket.AF_INETsocket.SOCK_STREAM) #connect to the host:port sock.connect((HOSTPORT)) #format and send data sock.send("%s:%s" % (seconds,string) ) #get some data back data=sock.recv(4096) sock.close() print data
The sender script was named ttsend and it resides in my ~/bin directory. Now when I'm cooking my oatmeal, I enter the command
ttsend 240 "It is time to check the oatmeal"
Since I will probably be more interested in timing things by the minute than by the second, an update to take minutes as input seems to be in order.

Now stop reading, and CHECK THE OATMEAL!
Comments
2009-12-23 loopo:
Hi there Jezra. Nice work! Just getting into python myself. Enjoyed your Gimp post on Linux Outlaws.
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
4 plus 9
Answer:
required
  • Tags:
  • Python
subscribe
 
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008