According to transitchicago.com, on an average weekday there are around 1.6 million rides taken on the CTA. The most efficient way to cut down on commute time is to leave at just the right moment to catch the bus or train. Our project hopes to provide these commuters with consistent and reliable arrival times for CTA buses and trains so they can minimize their commute. The Chicago Data Portal has developer tools available so that people can create their own CTA trackers for their own uses. The API used for this project will consist of these tools.
The will consist of 4 arduino’s all communicating with a raspberry pi. Off of each pair of arduinos will be hardware that will correspond to the output of the CTA data. Using the scrolling function we will be able to say which train is arriving at what stop on one lcd board and the minutes remaining until arrival on the other arduino. This will also apply to the second arduino except with relevant bus information. There will also be LED’s that correspond to when the bus or train is around 5 minutes away.
The user will have to program the raspberry pi to select which train and bus data they would like to see. Due to the size constraints of the LCD boards, we may need more to display all of the relevant information. The data needs to be updated in real time so there needs to be very little downtime between when the information is published to when it is received by the raspberry pi.
The first step in development is to apply for an API key from the Chicago Transit Authority. With the API key, we will be able to make the appropriate calls to the database to obtain real time arrival times for different trains. This API key allows for a larger number of requests to be made as well as increases the frequency of requests.
In order for the raspberry pi and the arduino to communicate we need a serial connection via usb from the Raspberry Pi to the Arduino. When a user selects their train station on the arduino or phone, the selection data will be transferred using this connection from the arduino to the raspberry pi. The Raspberry pi will then take the data that it receives and make the appropriate query to the database and retrieve the trains arrival time. This arrival time will be sent back to the user and displayed on the LCD screens.
A bluetooth adapter will be integrated to allow users to select their trains from their mobile device. This will give users to easily select their line and destination. The data selected on the phone will be sent via bluetooth to the raspberry pi. By not using buttons, users can select their train quickly and won’t accidentally miss a train because of time spent pushing buttons.
The raspberry pi will receive a comma separated string from the first arduino. This string has to be tokenized then sent to a database in order to get the keys to perform the the API lookup. The user string the pi receives will consist of the direction and station name. These two form a super key that we can use to identify STOP_ID which acts as a primary key. The values that need to be obtained include the stopID and route direction. A sample of the table is shown below.
Using the given CTA API, we will send a URL string requesting specific route data based on user input.
A request is sent via a URL, with the following format:
http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=X&stpid=X&rt=X&max=X
Where The corresponding values for the api key, stop id, route direction, and max queries going in the X’s
The data returned from the request is an XML file that must be parsed. The data we’re interested in obtaining is the stop identifier (stpID), the route code (rt), and the max number of results to display(max). The stop identifier is the train station that is determined based on what the user selects from the arduino or from their phone. The route code is what CTA line we are looking at (Blue, Red, etc.). The max queries ensures that we only get the amount of upcoming trains we can display, in this case 2.
An example of how the data is stored is shown below.
From the XML structure, we can see the relevant data such as stpID, rt, and the arrival time. Due to the natural hierarchical structure of the XML, it is natural to store the data as a tree. The type of data structure that we’ll be using is known as an element tree.
For XML parsing of the CTA data we are going to use the xml parsing to build an element tree in which we make iterative command arguments to look for the destination stop (././destNm) and the estimated arrival time (././arrT). Since the arrival time is in hour:minute:second format, it will need to be subtracted, from the current time to determine the amount of time remaining before the next train.
Raspberry Pi: In order to control the raspberry pi, there are a variety of externals that have to be plugged in. This includes, an ethernet cable, monitor, keyboard, and mouse. Unfortunately, by having all these externals there is not enough room for the arduinos to be plugged into it. However, a way to circumvent this issue is to have the pi running in “headless” mode. Headless mode ensures that the raspberry pi can operate without needing those externals. This is done by a client establishing an ssh connection with the raspberry pi and operating from their local machine.
CTA Delays: Due to weather conditions or other extraneous variables, the CTA train may be delayed. If there is a delay then the user should be notified so that they don’t have to wait an extra amount of time. This notification can take place with the buzzer producing more sounds such as 3 beeps instead of the usual 1.
Low Battery: Since one of the arduinos is battery powered, the user will have to check to make sure the battery is has power. A future implementation might check the remaining power and alert the user when it gets low.
Accuracy: The accuracy of the computer’s clock must be considered in order for the app to work properly. One issue that arose during development was the Raspberry pi’s clock being initialized to 6 hours ahead. In the code there is a check to see if the time is more than an hour off, 60 minutes will be added to the time remaining. This resulted in arrival times of 62 and 72 minutes. Once the source of the issue was identified, the raspberry pi’s clock was reset to CST and the app worked normally.
The user first connects to the bluetooth Arduino, then selects the CTA Line, Stop, and Direction. There is an option to set an alarm for a specific amount of time before the next train arrives. The set button then sends that information to the Arduino and Pi in a format for querying the CTA Database. The App Inventor features a block-style of conditional/event-driven programming. It contains the list of CTA lines, stops, and destination stops (direction), as well as code for connecting to bluetooth and sending data across it.