[Twisted-Python] Question : Combining wokel XMPP and twisted.internet.serial

Lucas Taylor ltaylor.volks at gmail.com
Thu Dec 22 01:12:58 EST 2011


On Dec 21, 2011, at 9:52 PM, bino oetomo wrote:

> Dear All ..
>
> A year ago, I need send some defined message to a jabber conference.
> The message content will depend on a "button-press".
> My trick is to use ttyctrl (http://ttyctrl.sourceforge.net/).
>
> So basicaly my "system" is builded from several parts:
> 1. TTYCTRL : I made some small python XMLRPC client script, that will
> send a msg to my local twisted server
> 2. Twisted server, contain :
> a. an XMLRPC server
> b. XMPP Client ... use to send msg to remote jabber conference room
>
> I made a pastebin of that twisted script at : http://pastebin.com/iSRVZNwS
>
> With that script the flow alway one-way only :
> serial====>Twisted_XMLRPC--->Twisted_XMPP=====>Jaber
>
> Now I need to do something different, since it's hard to find a Board
> with RS232 onboard.
> I Need to replace TTYCTRL with Arduino MCU.
> Arduino will Send serial data to my PC.
> So I'll need my twisted to able to receive data via serial
> There will also incoming XMPP msg from Jabber that will need to be  
> send
> to arduino via Serial port.
>
> so the flow will be like :
> serial<====>Twisted_Serial<--->Twisted_XMPP<=====>Jaber
>
>
> My question is, How to make :
> 1. twisted.internet.serial to send data via an instance of XMPP  
> client ? and
> 2. my EchobotProtocol.onMessage (see pastebin), to write data to the
> same serial port used by Point#1 above.
>


This is a relevant FAQ for this situation:
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother

More specifically:
1. Create a Protocol class for your SerialPort that will communicate  
with the Arduino. Instances of this class will have access to the  
serial transport and can write out to the serial port. Incoming  
messages from the Arduino will be handled by the dataReceived method  
of the protocol.

2. Give your EchoBotProtocol a reference to the SerialPort somehow  
(e.g. via init), and set a reference to the EchoBotProtocol on the  
SerialPort.

Here's a quick sketch:

# Basic line receiver protocol
class ArduinoReceiver(LineReceiver):
	def lineReceived(self, line):
		print "Line Received from Arduino"
		self.echobot.send('some xmpp message for which I do not know the  
format')

# Wire up the serial port and protocol...note there are more  
parameters to create the SerialPort (baudrate, etc.)
serial = SerialPort(ArduinoReceiver, '/dev/tty.usbserial', reactor)

# EchoBot gets a reference to the serialport
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 onMessage(self, msg):
		self.serial.transport.write('some message\n')

echobot = EchoBotProtocol(serial)

...and an update to your pastebin that may be helpful for context: http://pastebin.com/2EJ22wXa



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20111221/a9fdf72a/attachment.htm 


More information about the Twisted-Python mailing list