CTA Light Tracker

by achavez116 in Circuits > LEDs

303 Views, 0 Favorites, 0 Comments

CTA Light Tracker

unnamed (7).jpg
Screenshot 2023-01-30 9.21.44 PM.png

I used CTA bus api data to figure out how far the next bus is from my stop in minutes and then display it with lights.

And in the morning you are able to have a visual of how much time you have to meet the bus.

Video in action

Supplies

CTA API

Screenshot 2023-01-29 10.54.15 PM.png
Screenshot 2023-01-29 10.58.09 PM.png
Screenshot 2023-01-29 10.53.22 PM.png

First thing you want is access to the CTA API

Go to the cta bus tracker website and create an account

Then on the main page click on Sign up for API key and follow the on screen instructions

Once you have your specific API key sent to you save it as it will be used to accesses the API data

To get a useful link look at the API documentation

There are many things you can track and it explains how to set up the link

The link I used is https://ctabustracker.com/bustime/api/v2/getpredictions?key=XwijuXRX2qnUuBfptxUk3t5K&rt=94&stpid=4273&top=1&format=json

Base URL for this project:

https://ctabustracker.com/bustime/api/v2/getpredictions?key=(your own key)&rt=(the number of the bus route)&stpid=(the individual number that identifies your stop)&top=1&format=json

To find your route ( rt ) and stop # ( stpid ) go into this URL and you will use the key got earlier in your URL as ( key )

Create Webhook

Screenshot 2023-01-27 1.13.15 PM.png
Screenshot 2023-01-27 1.14.51 PM.png
Screenshot 2023-01-24 12.56.22 PM.png

We need to create a webhook that will call the URL we made and get us the bus data every minute

Go to console - then integrations - then create a new webhook

Click webhook and type in the following in the appropriate boxes as shown in the picture

You can name it anything you want but you want to replace the URL with your own URL that we made in step 1

Code

Screenshot 2023-01-30 12.50.25 PM.png
Screenshot 2023-02-01 1.33.17 PM.png

Copy this code by opening "code" and making a new app

#include <neopixel.h>
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#define PIXEL_COUNT 6
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel light(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int pixelCounter = 0;  

#include <ArduinoJson.h>
#include <iostream>
#include <string>
#include <sstream>

StaticJsonDocument<801> doc;
int s = -1;
unsigned long prevTime = 0;

void setup() {
    Particle.subscribe("hook-response/getBus", myHandler, MY_DEVICES);
    Serial.begin(9600);
    light.begin();
    light.setBrightness(10); 
    Particle.publish("getBus");
    
}
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<PIXEL_COUNT; i++) {
      
    if(i!=s){
        light.setPixelColor(i, c);
    }
    
    else
    {
        light.setPixelColor(i, (100,255,100));
    }
    
    light.show();
    delay(wait);
    
  }
  light.setPixelColor(0, (100,100,255));
}

void loop() {
    
    unsigned long currTime = millis();

if ( currTime - prevTime >= 60000 )
{
prevTime = currTime;



        if(Time.hour() = 11)
        {
            Particle.publish("getBus");
        }
    }
}

void myHandler(const char *event, const char *data)
{
    const char* json = data;
    deserializeJson(doc, json);
    
    String prdtm = doc["bustime-response"]["prd"][0]["prdctdn"];
    
    int in = prdtm.toInt();

    if (in > 0 && in < 3)
    {
        s = 1;
    }
    else if (in > 2 && in < 6)
    {
        s = 2;
    }
    else if (in > 5 && in < 10)
    {
        s = 3;
    }
    else if (in > 9 && in < 25)
    {
        s = 4;
    }
    else if (in > 24 && in < 30)
    {
        s = 5;
    }
    else {
        s = 0;
    }
    
    colorWipe(light.Color(0, 250, 20), 1000);
}

The second picture shows the main customization part where you can change which light changes depending on how many minutes away the bus is

And color wipe is a quick way to change the main light colors

Wiring

unnamed (5).jpg
unnamed (4).jpg
unnamed (6).jpg

Wiring is super easy

You need 3 wires.

In the pictures I have a sort of adapter solder to the wires at the 3 different pads at the opposite end to where the arrows are pointing on the LED strip

then I have 3 jumper wires from the adapter to ground 3v3 volts and Din respectfully as they are labled.

An easier method would be to take 3 individual pieces of wire, making sure they are stripped on both ends

Solder one end of each wire to one copper pad making sure to attach the to the end at the arrow butt (look at the LED picture above)

Each wire / pad is labeled on the led strip (look at same picture above)

Take the wire attached to +5v wire and insert into the breadboard next to the 3v3 pin

Then take the GND wire and insert it into the GND pin

Be very careful with this step as to not insert the wire into the MD pin in between GND and 3v3 or your argon will DIE and its annoying to fix

Finally insert the Din wire into D6 and thats your wiring done

Put It All Together

IMG-9097.jpg
IMG-9098.jpg
IMG-9114.jpg

This part is very up to you as its so simple it can be done in many ways

I took an random piece of cardboard larger than the strip of lights

Measure out 6 marks for holes as far apart as the leds are from each other (around 1.5 inches)

Use a knife/blade to poke the initial cut at the hole location then use a screw driver or other stick to poke through the cardboard

They should align up with the led strip so just hold it down over the holes and tape it down

Then simply taped the led strip with masking tape

The argon can be taped behind the leds and tape the connecting wires down as well