forked from brendanchambers/cta-transit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcta_lcd.py
125 lines (93 loc) · 3.33 KB
/
cta_lcd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''
CTA TRACKER
cta_lcd.py
3/2019
'''
##################################################
import adafruit_character_lcd.character_lcd as LCD
import board
import digitalio
import time
import json
import requests
import pandas as pd
##################################################
# LCD INITIALIZATION
# Raspberry Pi pin configuration:
# Based on: https://pimylifeup.com/raspberry-pi-lcd-
lcd_rs = digitalio.DigitalInOut(board.D25) # 25
lcd_en = digitalio.DigitalInOut(board.D24) # 24
lcd_d7 = digitalio.DigitalInOut(board.D22) # 22
lcd_d6 = digitalio.DigitalInOut(board.D18) # 18
lcd_d5 = digitalio.DigitalInOut(board.D17) # 17
lcd_d4 = digitalio.DigitalInOut(board.D23) # 23
lcd_backlight = digitalio.DigitalInOut(board.D4) # 4
# Define columns and rows of a 20x4 LCD.
lcd_columns = 20
lcd_rows = 4
# Initialise the lcd class
lcd = LCD.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
lcd.backlight = True
##################################################
# SEND URL REQUEST FOR CHICAGO TRANSIT AUTHORITY DATA VIA BUS TIME
# Add credentials and parameters
config_path = "config.json"
with open(config_path,'r') as configfile:
config = json.load(configfile)
api_key = config['auth']['api-key']
# 10566 - 55th & HP Blvd Westbound
# 1654 - 55 & HP Blvd Southbound
# 1518 - 55 & HP Blvd Northbound
stop_ids = [10566,1654,1518]
route_ids = [6,55,171]
##################################################
# Construct query for the API Prediction call
# prefix for predictions request
prediction_url = 'http://www.ctabustracker.com/bustime/api/v2/getpredictions'
# add apikey
prediction_url += '?key=' + api_key
# add arguments containing stop id(s)
prediction_url += '&stpid='
for idx,stpid in enumerate(stop_ids):
if idx > 0:
prediction_url += ','
prediction_url += str(stpid)
# and route arguments
prediction_url += '&rt='
for idx,rtid in enumerate(route_ids):
if idx > 0:
prediction_url += ','
prediction_url += str(rtid)
prediction_url += '&format=json'
##################################################
while True:
# Make a request, convert data to json
response = requests.get(url=prediction_url) # , params=params)
try:
data = response.json() # Check the JSON Response Content documentation below
except:
print("Warning, JSON Decode Error")
print(response)
data = pd.DataFrame(data['bustime-response']['prd'])
##################################################
# Print out data
routes_running = data['rt'].unique()
for iRoutes in routes_running:
#initialize and clear LCD message
lcd.clear()
text = "Route {}\n".format(iRoutes)
# get data subset
rt_data = data.loc[data['rt']==iRoutes]
#get unique directions
directions = rt_data['rtdir'].unique()
for iDirection in directions:
dir_data = rt_data.loc[rt_data['rtdir']==iDirection]
# set time string
times = ""
for iTime in dir_data['prdctdn']:
times += " {},".format(iTime)
text += "{}: ".format(iDirection[:-5]) + times + '\n'
lcd.message = text
time.sleep(5.0)
time.sleep(60000) #update every 10 minutes