LIRR service status
Here's a quick project for a Raspberry Pi. The MTA posts the status of trains and buses on their website. This project grabs that file, looks for one LIRR branch, and turns on a LED if there's a problem.
- A Raspberry Pi with access to the Internet
- A LED with a current-limiting resistor
- Connect the LED to the Raspberry Pi with the resistor like you've seen in 100 other RPi tutorials. The software below assumes pin 11, but it can go on any GPIO pin if you want.
- Use this command to create a file called lirr.py which will contain the program
nano lirr.pyHere's the file:
# # lirr.py # # firstname.lastname@example.org # # 2017-08-27 # # Import the GPIO library import RPi.GPIO as GPIO # This sets the pin number to use when turning the LED on and off: LIRR_STATUS_LED_PIN = 11 # Pin 11 using board numbering is GPIO 17 using BCM numbering # main() is the, um, main function def main(): GPIO.setmode(GPIO.BOARD) GPIO.setup(LIRR_STATUS_LED_PIN,GPIO.OUT) if thereIsAServiceProblem(): GPIO.output(LIRR_STATUS_LED_PIN,1) else: GPIO.output(LIRR_STATUS_LED_PIN,0) GPIO.cleanup() return # This function checks the MTA's site for service status, parses the file # to find the branch we care about, and then returns True if there's a problem # or False if there isn't a problem. If there's an error it returns False, which # probably isn't what you want. def thereIsAServiceProblem(): # first, define some strings that get reused agency = 'LIRR' lineName = 'Babylon' # change this to whatever you want. Options are: # 'Babylon' # 'City Terminal Zone' # 'Far Rockaway' # 'Hempstead' # 'Long Beach' # 'Montauk' # 'Oyster Bay' # 'Port Jefferson' # 'Port Washington' # 'Ronkonkoma' # 'West Hempstead' agencyStart = '<' + agency + '>' agencyEnd = '</' + agency + '>' partLIRR = '' # call the function that downloads the serviceStatus.txt file: serviceStatus = downloadServiceStatus() # Look for the LIRR section: if agencyStart in serviceStatus and agencyEnd in serviceStatus: partsStart = serviceStatus.split(agencyStart) partsEnd = partsStart.split(agencyEnd) partLIRR = partsEnd else: print ('ERROR: agency not found') return False # look for the branch we care about: lineStart = '<name>' + lineName + '</name>' lineEnd = '</line>' if lineStart in partLIRR and lineEnd in partLIRR: partsStart = partLIRR.split(lineStart) partsEnd = partsStart.split(lineEnd) partLine = partsEnd # optional: print the status for the branch to the screen: print (partLine) # Any of these is considered a problem: if 'SERVICE CHANGE' in partLine: return True if 'DELAYS' in partLine: return True if 'NO SERVICE' in partLine: return True else: print('ERROR: branch not found') # if no problems, return False (i.e. No problems) return False # get the file from the MTA's site, and return it as a string def downloadServiceStatus(): import urllib2 response = urllib2.urlopen('http://web.mta.info/status/serviceStatus.txt') txt = response.read() return txt # go!! main()
- Press control-X to save it.
- Start it up using this command:
sudo python lirr.py
Why build this? Because glancing at an LED on your way out the door is faster than finding your phone, entering your password, finding your email, and looking for an email from the MTA. The command above runs it once, but we need it to continually check.
- Do this to edit root's crontab:
sudo crontab -e
- Add this line at the end:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * python /home/pi/lirr.py
Other branchesInside lirr.py, in the thereIsAServiceProblem function, there's a variable called lineName. Change that to one of the listed branches to check another branch. You can probably adjust it to check the status of a Metro-North branch, or maybe a subway or bus line. I never tried it, but it should work.
Something not working?Drop me a line.
More Raspberry Pi projects