Wednesday 2 March 2016

BlissFlixx: wifi config & user information

I was thinking recently about how I might send a configured BlissFlixx SD card or complete system to a friend.

How easy is it for someone to configure and use?

This led to a bit of Python coding, as I attempted to make wifi configuration just a little bit easier.


In an earlier post I described the media centre BlissFlixx, which runs on any model B variant of the Raspberry Pi and is remotely controlled via HTTP from a smart device like a tablet or phone.

Once running, it is super-easy to use, so long as it is connected to your network and you know the IP address that your router has allocated to the Raspberry Pi.

Although the recommendation is to run BlissFlixx via ethernet, I have been using it via wifi. My router is only a few metres away from the Pi so I get a good 54Mb/s data rate.

Wifi configuration

Since the Pi used for BlissFlixx is configured to only boot to the command prompt, I decided to add some code to intially do two things:-

  • Display the current system IP Address
  • Allow the user to configure wifi

A last minute addition was to also display the initial wifi connection rate. This is what the TV screen looks like when the Pi has finished booting:-

The 2 lines "lo & eth0" are just junk I haven't been able to get rid of!

Clearly, if the bit rate is low, you won't get a very good viewing experience.

The code

I'm [clearly] not a Python programmer. So once you have picked yourself up off the floor and dried your eyes, you may be able to help tidy this up.

In outline; I'm using Python to open ifconfig & iwconfig so that I can extract the current IP address and (where applicable) the wifi connection bit rate. For wifi configuration, the program accepts the user entered SSID and password, then runs wpa_passphrase to create a new wpa_supplicant.conf file.

#Simple Python3 prog to display current IP Address and to allow
# users to enter wifi details and update the file:-
#   /etc/wpa_supplicant/wpa_supplicant.conf
#Feb 2016
import os
import time
#extract IP Address from ifconfig
TheIP = "blank"
os.system("ifconfig > /home/pi/blissflixx/ifconfig.txt")
f_ipaddr = open('/home/pi/blissflixx/ifconfig.txt', 'r')
inet_details =
index=inet_details.find("inet addr:")
offset = len("inet addr:")
#I assume there are no more than 3 "inet addr" entries in ifconfig
if index < 0:
    print("\n>>>>>>>>>>>>>>  Hmmm, can't find an IP address!\n")
    testip = inet_details[index+offset:index+25]
    index_sp =  testip.find(" ")
    if testip != "":        #exclude loopback
        TheIP = testip
        index=testip.find("inet addr:")
        if index < 0:
            print("\n>>>>>>>>>>>>  Hmmm, can't find an IP address!\n")
            index_sp = testip.find(" ")
            TheIP =testip[0:index_sp]

if TheIP != "blank":
    print("\n                 Point the browser on your remote tablet/laptop/phone to http://"+TheIP+"\n")

os.system("iwconfig > /home/pi/blissflixx/iwconfig.txt")
f_wireless = open('/home/pi/blissflixx/iwconfig.txt', 'r')
wifi_details =
index = wifi_details.find("Bit Rate")
if index > -1:
    rate = wifi_details[index:index + 17]
    print ("\n Wifi " + rate + "\n")

print ("\n If you need to re-configure BlissFlixx to suit your wifi...\n")
print ("   ... please attached a keyboard to your Pi & enter the wifi/router SSID\n")

#re-configure wifi
varSSID = input("SSID: ")
varPASS = input("Please enter wifi password: ")
print (varSSID,' ',varPASS,'\n')
os.system('wpa_passphrase ' + '"' +varSSID + '"' +" " + '"' + varPASS + '"' + ' > /home/pi/a_pytest')
f_template = open('/home/pi/blissflixx/wpa_template', 'r')
f_wifi = open('/home/pi/a_pytest', 'r')
wpa_details = + '\n\n' +
print (wpa_details)
f_wpasupp = open('/home/pi/a_pytest', 'w')
os.system('sudo mv /home/pi/a_pytest /etc/wpa_supplicant/wpa_supplicant.conf')
print ("...I just need to re-boot the Pi so that changes can take affect...this may take a minute or two to stop all processes...")
os.system('sudo shutdown now -r')

I include a wpa_supplicant template file which looks like this:-

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

The passphrase info is simply added to the template text and a new file created. Python wouldn't let me put this straight into its final resting place, so it is initially saved in Pi home folders and then moved.

Changes to &

I also made a couple of simple mods at the start of the main file:-

from os import path
import sys, os
LIB_PATH = path.join(path.abspath(path.dirname(__file__)), "lib")
import locations, gitutils, cherrypy
import time
print "\n\n..........enforcing a 10 second delay.....\n"

# Do not allow running as root
if os.geteuid() == 0:

The highlighted lines just clear the boot screen and add a 10 second delay. The delay appears to be necessary to ensure that the Pi is connected to the wifi before BlissFlixx starts.

In I've just added a line to start my Python code:-

python /home/pi/blissflixx/ --port 80 --daemon
python3 /home/pi/blissflixx/

All files live in: /home/pi/blissflixx but as already mentioned, a copy of the updated wpa_supplicant.conf is also copied to: /etc/wpa_supplicant.

No comments:

Post a Comment