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
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
On to the sender
Now stop reading, and CHECK THE OATMEAL!
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
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.sleep 240; espeak "Hey Jezra, it is time to check the oatmeal"
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,host, port): #create and bind to a socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind((HOST, PORT)) self.sock.listen(1) def run(self): waiting = True data="" #get a connection created when the socket accecpts input conn, addr = 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
The sender script was named ttsend and it resides in my ~/bin directory. Now when I'm cooking my oatmeal, I enter the command#!/usr/bin/env python import socket import sys #what happens where there is an error? def quit( message ): 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_INET, socket.SOCK_STREAM) #connect to the host:port sock.connect((HOST, PORT)) #format and send data sock.send("%s:%s" % (seconds,string) ) #get some data back data=sock.recv(4096) sock.close() print data
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.ttsend 240 "It is time to check the oatmeal"
Now stop reading, and CHECK THE OATMEAL!
Comments
Hi there Jezra. Nice work! Just getting into python myself. Enjoyed your Gimp post on Linux Outlaws.