Pi: Python script for BtSync status LCD

Adding and LCD display to a Pi project can make it so much easier to use. Displaying current IP address and status of some task that you only have to interact with when something went wrong. In this example we have the 20×4 (20 characters x 4 lines) LCD status display of my BtSync Satellite that I built a while back. Since this box is going to sit on a DHCP network I wanted to display the IP-address so I know what to SSH against. I also wanted to display some status metrics about disk mounts, services and application specific performance counters.
In this scenario the box is running BtSync to keep an offsite encrypted backup off my NAS. For security reasons I have to SSH to the box after a power cycle or failure to enter the encryption key for the disk. That’s why I want it to display it’s current IP-address on the display. I also want to see the current status of the encryption mount, BtSync service and the upload/downloads going on. That way I know when I have to SSH into the box to sort something out.
So what does this script actually do? It runs an infinit loop until you kill the process. Every 45 seconds it checks the stuff that doesn’t need updating all that often and every 3 seconds it checks the current status of the BtSync operations.
Every 45 seconds:
- Check the current IP-address
- Check if the Truecrypt volume is actually mounted
- Check if the BtSync daemon is running
Every 3 seconds:
- Checks number of files synced
- Checks number of files to be synced
- Checks the current download speed
- Checks the current upload speed
Pre-Requirements
First you need to wire up the LCD, it differs a bit from model to model but there are ton of descriptions on pinouts if you Google your specific model. Then go ahead and run raspi-config or what ever equivalent your brand of Pi uses. Go under Advanced and enable I2C. Then we download some tools that we need:
[bash]sudo apt-get install i2c-tools python-dev libxml2-dev libxslt1-dev zlib1g-dev python-smbus[/bash]
This will install all the things you need to communicate over the GPIO header to your LCD and also libraries needed for the features in the script. Then you can go ahead and download the script:
[bash] wget -O https://raw.githubusercontent.com/kallsbo/PiBtSyncLCD/master/lcd_info.py%5B/bash%5D
Configuration
There are a few configs you can do in the script, just use nano to edit the script file.
[py]# Configuration – LCD
LCD_BUS = 2 # The bus that the LCD is connected to. (Raspberry Pi usually 1, Banana Pi usually 2 – can be checked with i2cdetect)
LCD_I2C_ADDR = 0x27 # I2C device address of the LCD, use i2cdetect to find your displays address
LCD_WIDTH = 20 # Number of characters that each line can handle
LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x00 # Off
# Enviorment config
NETWORK_NIC = "eth0" # Network card used
TRUECRYPT_MOUNT_PATH = "/mnt/tc_disk" # path where the truecrypt disk is mounted
BTSYNC_SRV_NAME = "btsync" # name of the btsync service
BTSYNC_URL = "https://localhost:8888/gui/" # Web GUI address for btsync
BTSYNC_CRED_FILE = "/mnt/tc_disk/btsync_cred.json" # JSON file with btsync credentials[/py]
Script functions
If we first look at the main method it is simple enough. We run the lcd_init() function to initialize the LCD. All the LCD functions was forked from a script written by Matt Hawkins @ Raspberry Pi Spy. Then we set a simple update counter that keeps track of if the 45 second mark has been hit and if we should check the IP, mount and daemon status. It’s initially set to 16 so it will run the first loop and the counter is reset. Then it pluses one for every 3 second run so whenever it’s larger then 15 the 45 seconds has elapsed.
get_ip_address() – Simple function that takes the adapter name (eth0) as a parameter and then grabs the current IP-address of that adapter.
is_trucrypt_mounted() – Uses the os.path.ismount() function to check if the mount point is actually utilized by the Truecrypt drive.
get_btsync_cred() – Checks for the json file on the encrypted volume containing the UI username and password for BitTorrent Sync. I used this approach to keep the credentials safe. This function is executed every 45 seconds to make sure that the script get’s the credentials when the disk get’s mounted.
get_btsync_token() – Sends the initial request to the BitTorrent Sync UI (api) to get the token needed for all the requests to the API. This will also run every 45 seconds to make sure the token never times out and to counter any recycles of the web service.
Every three seconds the script checks if it has the credentials and token needed for the requests and if so runs the get_btsync_info().
get_btsync_info() – This function takes two parameters LLforSpeed and LLforFiles which stands for LCD Line. This value is used to display the information on the LCD panel row you like. It simply builds an url with the GLOBAL credentials and token and get the same json that the UI uses. Then parses it and get the total file count for downloaded files as well for files that are in the download queue. It also grabs the current upload and download speed and converts it to Mb/s and displays it on the LCD.
Credentials JSON file
This is just a plain JSON file containing the credentials. You can modify the script to hard code the credentials in the script but that will impact the security of the script. Here is an example of the credential files:
[js]{
"BTSYNC_USR": "btuser",
"BTSYNC_PSW": ":wDHz56L.blDgM,3Jm"
}[/js]
Cred and final thoughts
This is a simple setup for keeping track of your BitTorrent Sync daemon. It can be modified to just display the current info about btsync and not care about Truecrypt and the other extras I implemented for the “satellite” build.
I want to give cred to, as mentioned before, Matt Hawkins for the LCD example scripts that my LCD code is based upon. Also want to thank all bloggers and forum users for the posts I have read to be able to do this. This was my first time ever to use the GPIO header on the Pi for anything else then pre-built stuff like touch displays.
Any questions or suggestions? Please comment! And please follow me on a social media of your choice for updates…