Warning: Please do the upgrade when you have a couple of hours’ spare time, cuz it is gonna take very LONG.
Python was just upgraded to 3.5.0, if you want to take the first bite of the new version Python on your Raspberry Pi, please follow these simple steps in the Linux console.
$ cd ~ $ wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz $ tar -zxvf Python-3.5.0.tgz $ cd Python-3.5.0 $ ./configure $ make $ sudo make install
wget is used to fetch the installation package from Python ftp server, it will take around a minute to download depending the bandwidth. The whole ‘make’ process is a pain in the butt, it took me about two hours to complete.
After upgrading the Python, you will need to install pip and packages such as requests.
$ cd~ $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py $ sudo pip3 install requests
You can check the installation by trying ‘import requests’ in Python.
Recently I am engaged to develop a CTA bus tracker on Raspberry Pi, the goal is to display the ETA at a specified stop (not really a bus tracker software) on a LCD screen. Now I have the code ready, the next step is to connect the 16*2 LCD to the Pi and output the results.
Stop Number: 6621 --> Clinton & Polk Bus # 60 ETA 3.0 min Vehicle # 1451 Direction Westbound Bus # 60 ETA 14.0 min Vehicle # 1094 Direction Westbound
Here is the entry code
from cls_bustracker import * from checkOS import * import sys import os import time running = True while running: stopName = '' rt = '' prdtm = '' vid = '' rtdir = '' delay = 0 if isWindows(os.name): clearScreen = os.system('cls') else: clearScreen = os.system('clear') myBus = bustracker() stopId = sys.argv[1] stopName, rt, prdtm, vid, rtdir = myBus.getPredTm(stopId) print('Stop Number: {} --> {}'.format(stopId,stopName)) print('Last updated at {}'.format(myBus.getCurrentTime().split(' ')[1])) for route, predTime,vehicle, direct in zip(rt, prdtm, vid, rtdir): print('Bus # {} \t ETA {} \t Vehicle # {} \t Direction {}'.format(route,predTime,vehicle,direct)) print('Press CTRL + C to Exit...') time.sleep(60)
And two classes ‘checkOS’ and ‘bustracker’
Class ‘checkOS.py’
import os def isWindows(arg_os): if os.name == 'nt': return True else: return False
Class ‘cls_bustracker.py’
import requests import xml.etree.ElementTree as ET baseUrl = 'http://www.ctabustracker.com/bustime/api/v1/' key = 'Enter Your Own API' class bustracker(): #=================================================================== def _init_(self): pass #=================================================================== def getCurrentTime(self): getTimeUrl = baseUrl + 'gettime?key=' + key try: r = requests.get(getTimeUrl) if r.status_code == 200: root = ET.fromstring(r.text) for child in root: if child.tag == 'tm': return child.text else: return 'Unable to return current time!' else: return 'Service Unavailabe!' except: return 'Service Unavailabe!' #=================================================================== def getPredTm(self, arg_stpid): getPredUrl = baseUrl + 'getpredictions?key=' + key + '&stpid=' + arg_stpid rt = [] prdtm = [] vid = [] rtdir = [] rtdst = [] stopName = '' try: r = requests.get(getPredUrl) if r.status_code == 200: root = ET.fromstring(r.text) for child in root: currTime = '' for grandchild in child: if grandchild.tag == 'rt': rt.append(grandchild.text) elif grandchild.tag == 'prdtm': currTime = self.getCurrentTime() predTime = grandchild.text ETA = self.procTime(currTime, predTime) prdtm.append(ETA) elif grandchild.tag == 'stpnm': stopName = grandchild.text elif grandchild.tag == 'vid': vid.append(grandchild.text) elif grandchild.tag == 'rtdir': rtdir.append(grandchild.text) elif grandchild.tag == 'rtdst': rtdst.append(grandchild.text) return stopName, rt, prdtm, vid, rtdir except: return 'Service Unavailabe!' #=================================================================== def procTime(self, arg_currentTime, arg_predTime): if len(arg_predTime) == 14 and len(arg_currentTime) == 17: predSec = int(arg_predTime[9:11])*3600 + int(arg_predTime[12:14])*60 currSec = int(arg_currentTime[9:11])*3600 + int(arg_currentTime[12:14])*60 ETA = predSec - currSec if ETA > 120: return str(ETA / 60) + ' min' elif ETA <= 120 and ETA > 60: return '<2 min' elif ETA >=60 and ETA > 20: return '<1 min' else: return 'Approaching' else: return 'No Time Information!'