Creating Bluetooth Servers with PySerial

Section 7.4.1 showed how to make a phone client that communicates with the PC. However, we used a standard terminal emulator on the PC

side, so the messages that were sent to the phone were typed in manually. The most flexible approach, however, is to make servers of your own. This way you can access all resources on your PC, process information, send results to the phone and receive commands from it over a Bluetooth link without any manual attendance. This section shows you how.

You can access the serial port on the PC side most easily with a custom Python module, PySerial, which is available at http://pyserial. sourceforge.net Install the module as instructed on the Web page. Naturally you need a Python interpreter installed on your PC as well. You can find the newest one at www.python.org.

Example 60 creates a simple server program for your PC, using PySerial, that receives messages from the phone and responds to them automatically. The functionality is demonstrated by a simple 'Guess my number' game: the server picks a random number between 1 and 10 and the phone user has to guess it.

Example 60: PySerial script running on PC

import serial, sys, random

if sys.platform.find("win") != -1:

PORT = 0

elif sys.platform.find("linux") != -1:

PORT = "/dev/rfcomm0"

elif sys.platform.find("darwin") != -1:

PORT = "/dev/tty.pybook"

num = random.randint(1, 10)

serial = serial.Serial(PORT)

print "Waiting for message..."

while True:

msg = serial.readline().strip()

guess = int(msg)

print "Guess: %d" % guess

if guess > num:

print >> serial, "My number is

smaller"

elif guess < num:

print >> serial, "My number is

larger"

else:

print >> serial, "Correct! bye!

! "

break

You can use the client program in Example 59 to play the game. However, one minor modification is needed: this program is not interested in receiving back the characters it sent, so we can disable echoing in the client. Replace the line ECHO = True with ECHO = False in the client code.

In the beginning, we choose the port for communication based on the platform where the server is running. If you are using Windows, the line PORT = 0 corresponds to the COM port that you set up for Bluetooth communication in Section B.1. Note that indexing starts from zero, so PORT = 0 corresponds to COM1 and so on. For Linux and Mac, the file name should be correct unless you modified the default name used in the instructions, in which case you should change the file name here accordingly.

The server is a straightforward one: once the serial port is opened and assigned to the variable serial, we can start communicating with the phone through it. The Serial object works like a standard file object, so we can use the familiar readline() function to read a line at time from the client. Each line should contain one number, which is then compared to the correct one, num, after which an appropriate hint is sent back to the phone. When extending the example, note that the PySerial module contains many useful options, such as timeout values, which can be used to tune the server. See the PySerial documentation for a full list of features.

To use this program, follow the instructions in Section B.2 but now instead of opening a terminal emulator, such as Screen or HyperTerminal, you need to execute Example 60 on the PC side. On the PC, a Python script is executed by typing the command python btserver.py into the command shell. The file btserver.py should contain the code for Example 60.

Even though the 'Guess my number' game might not seem fascinating in itself, you should notice how easily one can build software on the PC side that communicates with the phone. There are thousands of custom modules freely available for Python, which let you control and access various devices and applications on the PC. With this method, you can control any of those modules with your mobile phone over Bluetooth.

For example, you could turn your PC into a multiplayer game console which uses mobile phones as controllers - have a look at www.pygame.org to get started with making games in Python. If you are an artist, you can make stunning interactive installations using www.nodebox.net, controlled by a mobile phone.

Section 7.4.3 shows how to control anyapplication on your Mac OS X with your mobile phone. Even if you use Windows or Linux, you might want to have a look at the next section, since the basic idea can be applied with other operating systems as well.

0 0

Post a comment

  • Receive news updates via email from this site