Python Terminal for Cheap Pi Arduino Connection

by russ_hensel in Circuits > Raspberry Pi

6929 Views, 32 Favorites, 0 Comments

Python Terminal for Cheap Pi Arduino Connection

PythonTerm.png

The Raspberry Pi is a Linux machine so there are probably a ton of terminal applications for it. But I have written a new one in Python, why did I bother? Read on. If you are doing a project that uses both the Pi and the Arduino you probably need a way for the two of them to talk to each other. Since the Pi will run the Arduino development environment, and it has a pretty nice terminal, you should probably start out with that one. But my Python terminal is useful because.

  • The Pi is really intended to run on user written programs using Python so it is nice to have a Python terminal.
  • My terminal is written with some special features for working with micro controllers, like the Arduino.
  • This terminal can itself be programmed, the Arduino IDE serial monitor, cannot be programmed.
  • If you do a custom project you will likely want a custom communications program to control it, you can take the terminal program and cut and paste its parts to make one.
  • Currently the terminal only supports RS232 style communication but I hope to extend it to support SPI and I2C protocols ( perhaps you would like to contribute ).
  • You can use the terminal on petty much any operating system, so if you are using a Linux, Mac or Windows machine ( where I originally developed it ) you should be fine. Everything here is open source so cheap here means free.

Notice: This is a really old instructable. The code has been considerably improved. See: Python Smart Terminal for write up of current status, and links to current code.

​Tools

python.png
  • PC - pretty much any OS, Linux, Windows OSX..... Rasberry PI is perfect.
  • Basic knowledge of Python
  • Arduino

Installation

Virgil_Solis_-_Apollo_Python.jpg

Get the files, ( or just read them with a couple of clicks ), from github Project's GitHub Files ( where updates will also be posted as they are developed ) Put the files ( unzipped if that is your chosen download ) in a single directory in your Python development environment.

Run It

Terminal.gif
parms.gif

The main program is in smart_terminal.py you can just run it, you should see the terminal window display.

You may discover that you are missing some Python components, if so track them down and install. Look at the import statements for clues as to what is needed.

To get a successful connection you will probably ( almost surely ) have to tweak some of the code.
Configuration is done from a Python file called parameters.py. Most importantly you should look at the values for baudrate and port. Adjust these to fit you setup. Look at some of the other values to see if they might apply to you. Then when you run again everything should work.

Make sure you open the port prior to use, it does not open automatically. It is a good idea to close it on exit, although that should happen automatically.

At some point, especially if you are having trouble read the code in parameters.py it is well commented ( if you think not let me know ) and you should be able to adjust the parameters without further instructions.

​Modifying for Your Own Preferences

spyder.gif

Before modifying the code it is best to understand how it works. Here is an overview of the general plan, details can be filled out by reading the code.

The general architecture is called the model view controller or MVC. In this program MyApp ( in mcuterminal.py ) is responsible for all overall control of the program behavior, it is the location of the main program, it creates the other important objects.

The view component is called GUI ( in gui.py ). It creates all the visible components, and relays user input to the controller.

The model component is the component that actually does the communication it is called RS232Driver ( in rs232driver.py ) and like the GUI is controlled by the controller.

The GUI is not allowed to directly communicate with the model and vise versa. Thus you can unplug them from the application and plug in new components. Don't like the GUI? You could modify mine, or you could make a modification and choose which one to use. This is sort of like a skin for an application. You can even set up to run with no GUI at all. The RS232Driver like the GUI easy to remove and replace in the program, its use has been parameterized in to the Parameter object, so to use SPI instead of RS232 all we have to do is write an SPI object and change the values in Parameter.

Two other important components are called Logger ( in logger.py ) and Parameters ( in parameters.py ). The controller creates one of each, and make them available to the other components. The other components can interact with them, and uses them respectively for logging events, and getting access to parameters ( those aspects of the application that are particularly easy to change ).

With this background you should be ready to read and modify the code. There are lots of comments, also a fair amount of commented out debugging code. Questions? Let me know.

Possible Expansion/Modifications, these are the things that I am currently thinking about:

  • The GUI could be much nicer, clean it up, fix colors, fonts..... -- some done
  • Add clear buttons to the send areas. -- done
  • Move at least some of the parameters to a text ini file. -- dropped as not useful
  • Add other communications protocols.
  • Make the application search the available comm ports to find any attached arduino. -- done
  • Let the user save the transmitted and received data to a file.
  • Display available comm ports. -- done