Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.words.xish import domish
- from wokkel.xmppim import MessageProtocol, AvailablePresence
- from twisted.words.protocols.jabber import jid
- from wokkel.client import XMPPClient
- from twisted.application import internet, service
- from twisted.web import server, xmlrpc
- from twisted.internet import ssl
- from twisted.internet import reactor
- from twisted.internet.serialport import SerialPort
- from twisted.protocols.basic import LineReceiver
- import traceback
- import string
- class EchoBotProtocol(MessageProtocol):
- def __init__(self, serial, *args, **kwargs):
- self.serial = serial
- # Set a reference to self on the serial protocol
- self.serial.protocol.echobot = self
- MessageProtocol.__init__(self, *args, **kwargs)
- def connectionMade(self):
- print "Connected! : "
- print self.send(AvailablePresence())
- mystatus = AvailablePresence()
- mystatus["to"]="ourroom@conference.jabber.internal/bino02"
- print self.send(mystatus)
- def connectionLost(self, reason):
- print "Disconnected!"
- def onMessage(self, msg):
- #Will just print the msg body to stdout
- print '<<<<>>>>>>>\n'
- print msg.body
- print '\n<<<<>>>>>>>\n'
- # Send something to serial port
- self.serial.transport.write('some message\n')
- class ArduinoReceiver(LineReceiver):
- def lineReceived(self, line):
- print "Line Received from Arduino: ", line
- #self.echobot.send('some xmpp message for which I do not know the format')
- class SerialService(service.Service):
- def startService(self):
- self.serial = serial
- #Create Application
- application = service.Application("Serial MultiService Example")
- #just for receive XMPP
- xmppclient = XMPPClient(jid.internJID("bino02@jabber.internal/bino02"), "bino02")
- xmppclient.logTraffic = True
- #Seting Serial
- serial = SerialPort(ArduinoReceiver(), '/dev/ttyUSB0', reactor,baudrate=9600)
- #Tell EchoBotProtocol to use "serial"
- echobot = EchoBotProtocol(serial)
- #Set echobot Handler Parent to xmppclient
- echobot.setHandlerParent(xmppclient)
- #Create serialService
- #as told at http://www.mentby.com/lucas-taylor/serialport-protocol-as-a-service.html
- serialService = SerialService()
- #Creating Multi Service
- multiService = service.MultiService()
- #Add our Services to multiservice
- serialService.setServiceParent(multiService)
- xmppclient.setServiceParent(multiService)
- #Set created application to be serviceparent of multiservice
- multiService.setServiceParent(application)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement