Chapter 9

From IPRE Wiki
Revision as of 18:09, 12 July 2007 by Natasha (Talk | contribs)

Jump to: navigation, search


Who ya gonna call?
-: Ray Parkey, Jr. in the song Ghostbusters.

Your robot doesn't have a direct way of communicating with another robot. But it can communicate with your computer, and your computer can communicate with other computers. There are many ways that one computer can send a message to another computer, for example email. If you wanted to carry on a conversation, email is a bit slow. But one could also use instant messaging, or IM for short.

Myro contains an IM interface so that you can write programs that send and receive messages. To test this out, you can try this with a friend. First you will need to log into the IM server with a name and a password. You can make both of these up, but you should remember the password/ID pair. If you forget, you will need to make up a new pair. Also, you will need to know the ID of the person that you want to send a message to.

First, let's begin by making a Chat object. Assume that your robot's name is Rosie:

>>> chat = Chat("rosie", "mypassword")
>>> chat.send("sandy", "Hi, how are you?")

Your friend, who's robot is Sandy, might do the same:

>>> ch = Chat("sandy", "xxyyz")
>>> ch.receive()
>>> [("", "Hi, how are you?")]
>>> ch.send("rosie", "I'm fine, thanks!")

And you would receive:

>>> chat.receive()
[("", "I'm fine, thanks!")]

Remote Robot Control

The robot that will be controlled (its name is "Rosie"):

>>> robot = Scribbler() # enter comX
>>> robot.initializeRemoteControl("mypassword")

To process just one command:

>>> robot.processRemoteControl()

To process commands continually:

>>> robot.processRemoteControlLoop()

What this does is allow your computer to continually be listening for commands, which it will (almost) instantaneously deliver to the robot.

Now let's say your friend, Sandy's 'parent', wants to control your robot. To do so, your friend types:

>>> chat = Chat("sandy", "xxyyz")
>>> chat.send("Rosie", "robot.turnLeft(.4)")
>>> chat.receive() 

There is also a RemoteRobot constructor which acts like a regular robot, but sends the commands to the other robot.

>>> robot = RemoteRobot("remoterobotname")
>>> robot.turnLeft(.4)

Sending Email

In order to send mail, you'll need to know your Simple Mail Transport Protocol (SMTP) server name. At Bryn Mawr College, it is

First, import the smtplib Python library:

>>> import smtplib

Then, you can make an SMTP object:

>>> s = smtplib.SMTP("")

And then use it to send mail. sendmail() takes three arguments: a from address, a list of recipient email addresses, and a text message.

>>> s.sendmail("", ["", "", ""], "Hi! Send money!")

This example will send the same message to three people, mom, dad and sis. If you want to put line breaks in your message, you'll need to use the \n (backslash followed by a small n).

(There are a few reasons while your email might not go through. For one, this could be flagged as "spam". Because email is this easy to send to millions of people at once, some have taken advantage of this fact to send unsolicited email, aka spam. Some installations might prevent email to come from any random computer without a special password. Check with your TA for details about your site.)

Previous Chapter: Chapter 8, Up: Introduction to Computer Science via Robots, Next Chapter: Chapter 10