Introduction: CTA Train Time Texter

About: Developer & Tinkerer

I've got around a eight-minute walk to my closest train station every morning. Depending on how late I'm running, that time can swing to eight minutes if I really pick up the pace to around twelve if I mosey. It seems like when I try to do the eight-minute run, I always end up getting to the station right as my train is pulling away. To help better predict how much time I have to catch a train, I made a button that can figure out when the next train arrives and then texts me the time.

Step 1: How to Make the Button

Making a button that can talk to the Internet sounds kind of involved, but it's actually pretty easy when you use a microcontroller. I used the Intel Edison to make my button. When the Edison hears a click from a button that's attached to it, it uses JavaScript to make a call to the Chicago Transit Authority's API, and then another call to Twilio to send me a text.

Step 2: Set Up Your Edison

Full disclosure, I'm working on a Mac, so these instructions will skew that way. To get started, you should have a freshly-flashed Edison. After you board is flashed, you can try to find the IP address and enter all the additional commands, or you can just "npm install bloop" on the machine that you're trying to SSH in from. Bloop is a tool from Rex St. John, and it's an absolute lifesaver when you're working with the Edison. Instead of running "screen /dev/cu.usbserial-XXXXX 115200 -L", all you have to do is run "bloop c" in terminal and it will connect to the Edison it finds on your network. Once you're in, run "configure_edison --setup" to get your wi-fi and user creds defined.

While all this is happening, you can start downloading the Edison Yocto Image from this site. You want the link that says, "Edison Yocto complete image." Once downloaded, you'll need to load the files onto a micro SD card - you can read up on Yocto and how to get those files onto the SD card here. After you load the files, power down your Edison, insert the SD card, and the power it back up. To test your install is working, SSH into your Edison and type "node -v". If that returns the version of Node that you have installed you're good to go. If it says "Command not found," you're going to need to try loading Yocto onto the SD card again, because something went wrong.

Step 3: Hook Up Your Edison

At this point in the build, you'll need to hook up whatever kind of button you're using. I was fortunate enough to receive a Grove Starter Kit from Intel, so I'll be talking about my experience with hooking that up. First pro-tip is that the connector shield is located on the back of the red anti-static foam, under the LCD screen. I may or may not have made it all the way to ordering a connector shield on Amazon before errantly knocking the starter kit case over and realizing that there was a shield already in the kit. It was early, I hadn't made coffee yet.

I 3D printed a case for my Edison using this file. The case uses the screws for the struts to secure the lid, and has two slots for the shield pins to go through. I did have to drill an extra hole in the lid for the ISP pins, but other than that everything fits great.

Attach the Grove Connector Shield to the pins on the Arduino breakout board, then grab a connector and attach your button to the pin marked "D2" (digital 2). Once that is securely connected, go ahead and power up your Edison.

It should be noted that while the Grove Starter Kit is a nice to have, you don't really need to worry about not having it. Get a button, solder some wires on, and hook it up straight to the pins on the Arduino - it's all the same thing.

Step 4: Write Your Code

Here's a link to the code on Github. You can certainly just git clone this onto your Edison, but I think it's worthwhile to type the code out yourself so that you're actually looking at what it does. The JS file is commented pretty thoroughly, but here's a quick pseudo-code rundown of what it's doing:

  • Require the libraries that we need for the sketch
  • Define some variables like the message and your Twilio API keys
  • Listen for a button press
  • If button is pressed, ask for the next 2 trains from the CTA's API
  • Make a request to the Twilio API and ask it to send a certain number a text from the CTA API's response

Once you have the repo, run 'npm install' from the root of your project. This looks at package.json and grabs all the correct packages that your node file will need to run.

In the main JS file, traintracker.js, you'll need to insert a few API keys - those sections are called out in the code as [ INSERT WHATEVER HERE ]

To get your Twilio API Keys, sign up for their free trial here. The trial lets you send thousands of texts before you have to decide if you want to pay for your calls, so you should be in the clear for this and many other projects.

The CTA Train Tracker API requires that they approve you for a key, but it's usually a pretty quick process. You can apply for a key here.

Step 5: Run Your Code

In the terminal window that has your Bloop session open, type 'node traintracker.js' in the root of your project folder. This should start the JS running. Check your terminal to see if the Edison gave back the MRAA version - if it did, you're all good to go. Go ahead and press the button, and then wait for your text.

If you don't receive a text, take a look at the output in the terminal. The code will return an error telling you what you're missing. Make sure that you replaced all the CTA and Twilio API keys, as well as the Twilio numbers.

If your button is working, now you can hit it on the way out your door and have you marching orders texted to you right away. Yay, Internet!