#!/usr/bin/python # # Python XMPP Script # ver 0.1 # Small script to receive data from a serial port and send it over through XMPP # by Stelios M. (steliosm@steliosm.net) # # NOTE: Remember to set the USERNAME, PASSWORD and SERVER # # Import all the basic stuff import xmpp, time, serial from threading import Thread class PicAxeClient(Thread): ThreadKill = False def __init__(self): Thread.__init__(self) def run(self): """Initialize the Jabber module and connect to the server""" print 'Connecting to the server...' self.con=xmpp.Client('SERVER', debug=[]) self.status = xmpp.Presence() # Connect to the server self.con.connect() # Authenticate (username, password, resource) self.con.auth('USERNAME','PASSWORD', 'Outerspace') # Make the PicAxe visible self.con.sendInitPresence() #self.setShow('away') # Register the handlers self.con.RegisterDisconnectHandler(self.DisconnectHandler) self.con.RegisterHandler('message', self.xmppMessage) self.con.RegisterHandler('presence', self.xmppPresence) # Process incoming XMPP messages while self.ThreadKill == False: self.con.Process(2) def DisconnectHandler(self): """If we get disconnected connect again!""" print "got disconnected. now reconnecting." self.con.reconnectAndReauth() def xmppMessage(self, con, event): """React on incomming messages""" fromjid, fromres = str(event.getFrom()).split('/', 1) if event.getBody(): print "Got message from: " , fromjid print 'Message: ' , event.getBody() self.con.send(xmpp.protocol.Message(event.getFrom(), 'You said: '+event.getBody(), typ='chat')) def xmppPresence(self, con, event): """React on incoming Presence messages. Subscribe the user and request subscription from him also""" #if event.getType == "subscribe": if event.getType(): #fromjid, fromres = str(event.getFrom()).split('/', 1) print 'Got SUBSCRIBE reqest.' self.con.send(xmpp.Presence(to=event.getFrom(), typ='subscribed')) self.con.send(xmpp.Presence(to=event.getFrom(), typ='subscribe')) def send(self, user, data): """Send the data to the selected user""" print 'Sending message to user' self.user = user self.data = data self.con.send(xmpp.Message(self.user, self.data)) def setStatus(self, data): """Set the Status according to the data received""" print 'Setting the status...' self.data = data self.status.setStatus(self.data) self.con.send(self.status) def setShow(self, data): """Set the Show according to the data received""" print 'Setting the show...' self.data = data self.status.setShow(self.data) self.con.send(self.status) def main(): # # Create an serial port object # Set the serial port the PicAxe is connected to # ser = serial.Serial('/dev/ttyS0', 4800) #Create the thread print 'Creating Thread...' picaxe = PicAxeClient() # Start the thread print 'Starting Thread...' picaxe.start() # Read the data from the serial port in a loop while (1): # Read data from the serial port (Attention: BLOCKING!) myData = ser.readline() # Strip off the last 2 characters (cr & lf) myData = myData[:-2] # DEBUG print '[DEBUG]',myData # Split the data into parts seperated by ':' myData = myData.split(':') # Check if the message is 'c', 's' or 'b' if myData[0] == "s": # Set the data as status message picaxe.setStatus(myData[1]) elif myData[0] == "c": # Send the data to a user picaxe.send (myData[2], myData[1]) # Start the program! try: if __name__ == "__main__": main() except (KeyboardInterrupt, SystemExit): PicAxeClient.ThreadKill = True raise