Raspberry_Pi_Education_Manual

Notes:

rpc_client.py:

import xmlrpc.client s = xmlrpc.client.ServerProxy( 'http://localhost:8000' ) # call the 'hello' function with no parameters print (s.hello()) # call the 'hello' function with a name print (s.hello( 'XMLRPC' ))

# add two numbers together print ( '2+3=%s' % s.add(2,3))

To run this example, you need two separate IDLE windows – one for the server and one for the client. You need to run the server first so that it is waiting for requests from the client. To stop the server, press Ctrl-C. How does it work? The server program sits and waits for requests from clients. The client program, when run, calls the “hello” and “add” subroutines. The calls are passed from the client to the server program, where the functions are actually run. The result is then returned to the client program, which displays the result on screen. Where could this XMLRPC mechanism be used? Well, imagine that you have several Raspberry Pis, each one connected to a screen located in a different room. You also have a central computer running a control application with a Graphical User Interface (GUI) designed to talk to every room over the network. This central application could monitor and control every room by communicating with individual Raspberry Pis – telling them what to display on the screen, control devices via GPIO, and return data recorded from the room (such as light levels or temperature). You could even take pictures with a camera! Each Raspberry Pi would run as an XMLRPC server; the central control program would act as the client.

Over to you

You can read more about Python’s XMLRPC modules here: http://docs.python.org/py3k/library/xmlrpc.server.html http://docs.python.org/py3k/library/xmlrpc.client.html

There are, however, XMLRPC libraries available in lots of other programming languages.

Human-computer interfacing

119

Made with FlippingBook flipbook maker