FlexCase

Python Scripting

  • Run Python scripts from terminal
  • Schedule scripts to run on powerup

This tutorial assumes that you have purchased a FlexCase with the Pro version with an MPU, and that you are using Windows to connect.

Prerequisites

  • Complete the quick start guide for connecting the FlexCase wire harness to your computer
  • Complete the SSH guide for connecting to the FlexCase terminal

Running Scripts

This tutorial assumes that you will be editing your python scripts in the editor or environment of your choosing, and then transferring the scripts to the FlexCase. If you chose, there are many options for editing your scripts directly on the FlexCase, such as:

  • Nano editor. Simple editor included in the operating system and accessible via the terminal by typing nano myScript.py
  • Built-in editor for SmarTTY, which also allows you to browse the file system. Accessible by clicking on the file icon and finding a file to edit

With your SSH session open, the terminal will default to the /home/pi directory. There should be a sample python script called spiLogger.py in this directory, which you can confirm by typing ls into the terminal:

This very basic script interacts with the SPI communication channel between the onboard Raspberry Pi and the microcontroller. The SPI communication can be modified as desired, but is out of scope for this tutorial, so please contact us for advice on how to achieve your specific objective.

Type python spiLogger.py to run the script. You can also press the tab key to autocomplete the script name. The script should show a very basic text interface, and allow you to log the SPI data from the microcontroller.

After ending your log, it will be saved in the logs directory, which you can view with the nano editor:

After showing the timestamp, the first byte of the SPI message is the status of the FlexCase On/Off switch (‘aa’ for on, ‘bb’ for off), and the next two bytes represent the supply voltage in millivolts. When the power switch is turned off, the Raspberry Pi detects the changed variable on SPI and begins its shutoff procedure.

Press CTRL+X to exit the editor.

You can transfer scripts you have already developed to the FlexCase with SmarTTY via the menu (SCP>Upload a file):

Verify that your script was transferred with the ls command. You can now run your script as before. If you want to continue using the terminal while the python script is running, you can open another tab in SmarTTY.

If the script becomes unresponsive, you can quit out of the execution using CTRL+C.

The script spiLogger.py simply changes the logging settings for the SPI data. The actually SPI communications are run in a script that is set up to run automatically at startup. Other customizations can be made to spiComm.py, which is usually located in /usr/local/. The relevant files are also available on the sample software page as needed.

Run Scripts on Powerup

Once your script is ready, you can set it up so that it runs automatically when you power up the FlexCase. There are multiple methods for doing this, but we will be using systemd.

WARNING: Make sure that you have tested your script from the terminal before setting it up to run at powerup. The script running unpredictably may make you unable to connect to the FlexCase.

Use the same directory as your Python script (named “myScript.py” in this example), type

Copy to Clipboard

In the nano editor, copy the following text, press CTRL+S to save the changes CTRL+X to exit:

Copy to Clipboard

This is the unit file, which tells systemd how to run the script automatically. Enter the following commands to activate the script service and start every time the FlexCase powers up (if myScript.py is already in /home/pi, you can skip the second command):

Copy to Clipboard
Copy to Clipboard
Copy to Clipboard
Copy to Clipboard

The next time the FlexCase powers up, the script will be run automatically. To test that the setup worked correctly, you can immediately start or stop the script with similar systemctl commands:

Copy to Clipboard
Copy to Clipboard

Some extra notes on scripts run using this method:

  • Scripts will run as the root user, not the pi user (can be changed if needed)
  • Any print() outputs or errors will be sent to a separate terminal that you cannot easily access. You can print script output to a log file by editing the unit file or create log files within your script
  • You can also disable the script from running at startup “enable” with “disable” in the systemctl command
  • If you want to keep your python script in the original directory, skip the move command for myScript.py and edit the unit file so that the ExecStart points to the correct folder