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.

Hardware

Parts

You'll need:

Assembly

Software

lirr.py

nano lirr.py
Here's the file:
#
# lirr.py
#
# douglaspkelly@gmail.com
#
# 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[1].split(agencyEnd)
		partLIRR = partsEnd[0]
	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[1].split(lineEnd)
		partLine = partsEnd[0]

		# 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()

Run it!

If there's a problem on the branch--by default it shows the Babylon Branch--then the LED will light up. If not, the LED still stay off.
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.

Checking regularly

That will run the script every 5 minutes, of every hour of every day. Check out a tutorial on cron to run it less often.

Other branches

Inside 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