Page 99 - Programming the Raspberry Pi Getting Started with Python
P. 99

many times using the flash function.
          The Raspberry Pi Software
          The Python code to talk to the Arduino is even more simple and can just be typed into the Python
          console. But first, you need to install the PySerial package to allow the communication to take place.
          This is done in the same way as the other packages we have installed—just fetch the zipped tar file
          from http://sourceforge.net/projects/pyserial/files/latest/download?source=files.
             Next, extract the directory from the archive by entering the following command:


             Now that you have a new folder for the module, just cd into it and then run the install command
          (first, though, it is worth checking the instructions to see if anything else needs doing beforehand).
          You are now ready to run the module installer itself, as follows:



             Once it’s installed, you will be able to import the module from the Python shell. Now switch from

          the Linux terminal to a Python console and type the following:



             This opens the USB serial connection with the Arduino at the same baud rate of 9600. Now  you
          need to start a loop listening for messages from the Arduino:




             You  will  need  two  hit ENTER  twice  after  you  type  the  second  line. Messages should now start to
          appear! You can see in the blue writing where the Arduino is talking to the Pi and then some error
          trace as you press CTRL-C to interrupt the messages coming from the Arduino.
             Now type the following into the Python console:


             This should cause the LED to flash five times.
          Summary
          In  this  chapter  we  looked  at  just  some  of  the  wide  range  of  ways  of  adding  electronics  to  our
          Raspberry Pi projects. In the next two chapters, we create projects using two different approaches—
          first using the Adafruit Cobbler and breadboard and then using the RaspiRobotBoard as the basis for a
          small roving robot.
   94   95   96   97   98   99   100   101   102   103   104