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.

Background


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
#SteveDee
#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 = f_ipaddr.read()
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")
else:
    testip = inet_details[index+offset:index+25]
    index_sp =  testip.find(" ")
    testip=testip[0:index_sp]
    if testip != "127.0.0.1":        #exclude loopback
        TheIP = testip
    else:
        testip=inet_details[index+offset:len(inet_details)]
        index=testip.find("inet addr:")
        if index < 0:
            print("\n>>>>>>>>>>>>  Hmmm, can't find an IP address!\n")
        else:
            testip=testip[index+offset:index+25]
            index_sp = testip.find(" ")
            TheIP =testip[0:index_sp]

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

os.system("iwconfig > /home/pi/blissflixx/iwconfig.txt")
time.sleep(5)
f_wireless = open('/home/pi/blissflixx/iwconfig.txt', 'r')
wifi_details = f_wireless.read()
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 = f_template.read() + '\n\n' + f_wifi.read()
print (wpa_details)
f_wpasupp = open('/home/pi/a_pytest', 'w')
f_wpasupp.write(wpa_details)
f_wpasupp.close()
f_wifi.close()
f_template.close()
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...")
time.sleep(5)
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
update_config=1



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 blissflixx.py & start.sh


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

#!/usr/bin/python
from os import path
import sys, os
LIB_PATH = path.join(path.abspath(path.dirname(__file__)), "lib")
sys.path.append(LIB_PATH)
import locations, gitutils, cherrypy
import time
os.system('clear')
print "\n\n..........enforcing a 10 second delay.....\n"
time.sleep(10)


# 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 start.sh I've just added a line to start my Python code:-

#!/bin/bash
python /home/pi/blissflixx/blissflixx.py --port 80 --daemon
python3 /home/pi/blissflixx/wificonf.py



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