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
HOST = '' #we don't care about the specific hostname
PORT = 5000 #pick a port
'''define the Server class'''
def __init__(self,host, port):
#create and bind to a socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
waiting = True
#get a connection created when the socket accecpts input
conn, addr = self.sock.accept()
# 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
print "received %s" % (data)
#parse the data we have received
result = self.parse_data(data)
#send a response to the sending client
#close the connection
#run again to create a new connection
#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
return_string = "OK"
return_string = "Error with data: %s" % (data)
if __name__ == "__main__":
server = Server(HOST,PORT)
On to the sender
#what happens where there is an error?
def quit( message ):
#do we have enough args?
string = sys.argv
seconds = "0"
string = sys.argv
seconds = sys.argv
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
#format and send data
sock.send("%s:%s" % (seconds,string) )
#get some data back
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!