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.



You'll need:



Here's the file:
# 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():

	if thereIsAServiceProblem():


# 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]
		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
		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('')
	txt =
	return txt

# go!!

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, 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