Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login
Show HN: Tracking my local bus with a RaspberryPi (purplehoisin.com) similar stories update story
197 points by fersarr | karma 302 | avg karma 1.92 2022-11-04 05:44:28 | hide | past | favorite | 117 comments



view as:

Finally started using my Raspberry Pi! I built a bus clock to know when to leave the house and never miss the bus again. Wrote this little post to share the details and some pictures.

"Error establishing a database connection"

Probably HN killed the server.

BTW, where did you buy the Pi, or did you already have it at home?


It’s sad because I desperately want a raspberry pi or two for some projects I’ve been kicking the can on, but I simply can’t get them for anywhere close to MSRP. The shortage hurts.

Perhaps try an equivalent like odroid? You usually get far more compute per $ anyway.

I haven't closely monitored the situation for years now, but it used to be the case that OS/software support for anything than a Raspi was often spotty at best.

With a Raspi, you not only have Raspian/RPiOS that fully supports all models back to the first one from 2011, but a decent number of really good alternatives like Fedora, Ubuntu and special appliance variants, e.g. for console emulation etc.

In contrast, my last experiments with some other SBCs consisted of downloading some ancient ISO files of questionable provenance from some shady .cn domains. It might have gotten better, but one should carefully investigate the software situation before buying a non-RPi SBC.


It depends on your level of expertise and how comfortable you are straying from an online guide. It's just a small matter of computer engineering, for different difficulty values of "just".

This is getting significantly better and fast - largely due to RPis being unavailable for 2 years and growing (and won't get better anytime soon). Odroid's ecosystem is quite a bit better these days. I highly recommend getting an XU-4 from Ameridroid or HardKernel themselves (my last order arrived within 10 days from South Korea to the west coast with zero customers issues) for a fraction of the RPi 4 MSRP cost (much less what it actually costs now) and tinker.

We switched to Odroid XU-4s for this reason.

Ya know, I just realized I missed the Thursday batch of Pi4s and got to thinking about my ODroid C1. I have generic USB cables and chargers laying around but I still don't know where my non-standard C1 charger went.

The last time I made this sort of comment one of the top replies was that Pis are notoriously picky about their power supplies. Be that as it may I've not run into any trouble with my Pis (Zero W + 3B). Hence I'm using my Pis while my C1 sits unused.


https://rpilocator.com is great for finding Pi's closer to MSRP. You just have to waif for them to be in stock.

I have one or two I’ll sell you below MSRP. I don’t use them anymore (barely did did to begin with). Email is in my profile. I just sold a Remarkable2 eink tablet to another HNer this way!

Thanks for sharing! In the first screenshot of CLI output, it seems to show three buses all being 1 minute out? Am I misreading that?

Yea I was surprised with that one too. It wasn't my local station but I think that sometimes happens when it's a busy station at a busy time so they have more than one bus. Or it might be a bug.

Not very surprising to me that they'd start bunching by Bethnal Green tube having all just been slogging their way through Mare Street in Hackney, honestly

Looks like it's been hugged to death. Archive.org link:

https://web.archive.org/web/20221102223146/https://purplehoi...


Wow! I wasn't expecting this at all. Thanks for the cozy hug of death :) Also, thanks for posting the archive link! I'll try to make it more resilient for the future.

The problem I’ve faced is that the official live update arrival times are often wrong.

I’ve fantasized for years about engineering a network of self contained + solar powered microcontroller / camera units that I could place on top of bus stops to track them myself.

Then again, by the time I get around to it the official arrival times will likely have become more reliable.

Any rate, great write up- thanks for sharing!


It’s a fun problem. We hire 4 trolleys for our open studio event in Somerville. One of our volunteers bought some smart phones, and we pulled a together a php and MySQL back end, some google maps and Trolley Tracker. We can see them bunch up over the course of a day.

A lot of transportation services have tracking now.


Why is the live data wrong? Why is it called live data, then? HH

For example, here in Prague, trams always arrives on time schedule or few minutes after that. (you can wait 2minutes lol)

Also all of our public transport has publicly accessible GPS tracking. Buses are more off of the time schedule, but you can check the GPS and compute the live time.

And subway doesn't make sense to track, since it arrives every few minutes at any station.


Ha! I should have specified, I’m in the US- I think it’s a large number of factors that ultimately boil down to “there’s no incentive to be accurate” our buses are often 15-30 minutes late.

I don’t think this is very typical, but I have even had a bus driver stop mid route, say “this is as far as I’m going today”, kick everybody off, and drive away


Ha! me and several engineers from Lockheed built just such a thin in 2007 -- but we couldnt get funding for it (we built the RFID tracking system for the military) but we were too early for the market at the time and people werent educated about tracking so we go rejected all over....(it was for fleet operations - we built the tracking systems for all weapons shipments and containers for the military at the time.

This sounds epic! How did you handle powering the devices at night / rainy days? A battery presumably.. but seems like that would seriously up the maintenance cost.

You would have to replace the batteries fairly regularly and I have a feeling that without funding anything I cowboy rig would be super flaky.

2007 means that the network would probably have been an issue as well. Though, I’m less worried about that these days.


The network problem was an issue in 2007 - and so we were working with cell companies to get sim cards and a deal - but that was really difficult because cell plans were still super expensive and this was a novel startup.

There are two types of RFID [active|passive] active required a battery which was rather small but the pings were so low power that the battery lasts a really long time.

We pivoted and built a tracking system for the cannabis industry - but cannabis growers didnt want to pay for anything they werent legally mandated to do so - we built a system called GreenScale which could track all employees in your growfields, bins etc... but again we are a bunch of engineers (my partner designs and builds the crazy sensors for TDK currently but designed the sensors for Google's RF hand sensors)

However, currently, we have a medical tracking system we have built for elderly care which integrates with Alexa and will tell you where your elderly care person is, if they accessed the restroom, if they fell... etc - but its based on a custom Ras PI basically and it uses RF as opposed to video etc with highly sensitive pressure sensors and other metrics to determine human action in a room....

When I was Director of OPs for Cognitive networks we buil spying bullshit on every single Vizio (and a couple other brands) TVs and could get a screenshot from any single TV connected to the internet...

We were always 5 years too early with our designs... and then all sorts of companies got founded upon things we were already doing 5 years prior... but people didnt undersrtand wtf we were trying to do and we are nerdy engineers and not salesmen so had a shitty time trying to get people to see our visions...

One of my partners now in eng manager at netflix instead.


The Transit App [1] crowdsources position data for vehicles. It's a gamified thing that rewards you with points or whatever for leaving the app running with your GPS on while in motion. This lets them have up-to-the-second knowledge similar to how Google Maps running on phones gives Google live traffic data around the world.

[1]: https://transitapp.com/


They also recently released a developer API [1]. The free access looks okay for personal use, I haven’t tried it yet.

Oh and if your public transit service has GTFS then just install home assistant and get maps and arrival times.

[1]: https://transitapp.com/apis


True, but I think part of the point is that the GTFS data is subject to the technical competency of your local transit agency, and that seems to vary widely— sometimes they lie about where vehicles are, or pretend that schedule-inferred positions are GPS positions, or they're missing vehicles, or the data is delayed, or whatever.

When Transit captures this information from their own users, it's much more trustable— basically all they'd have to watch for is someone maliciously poisoning the data by telling the app they're on a bus when they're really in a car, but that would be a lot of effort to go to just to be a jerk and end up shadowbanned.


Arrival time estimates are really difficult. I much prefer being able to see where the vehicle is on a map and make my own decisions, based upon my own experiences.

However, I played around with one provider (Availtec's) estimations for Birmingham, AL's bus system.

This looked at the GPS position of the vehicle to see when they actually arrived at a stop and back compared it to the running estimation time. As we can see, it isn't great.

https://blog.line72.net/2019/08/06/validating-availtecs-bus-...


In London? They're usually quite accurate - used to be tracked by roadside beacons so had less resolution but now it's all GPS I think.

Of course sudden traffic, letting an elderly passenger get etc will mean that the countdown won't be the same to the second. But that's going to be the same if you used roadside cameras too.

Generally it should be seen as more of an estimate based on how long the last bus took to arrive from the same point rather than an exact prediction but it's usually useably accurate in my experience.


Yea so far they are mostly accurate but there have been one or two occasiones with minor delays. I didn't know they used to have roadside beacons :| How did those work?

In London the TfL API includes access to their live CCTV traffic cameras. I think you now have to register but it should still be there.

really? you can actually see the feed from all the cameras? Oh is it cameras that just show trains?

I love the form factor of this and the ambient data.

TFL do have some quite good APIs so you could use those rather than scraping the HTML.

https://tfl.gov.uk/info-for/open-data-users/api-documentatio...

I think you need to register but they seem happy to have amateurs and enthusiasts using them and they work quite well.


Thanks for the feedback on the form :) And yea, using an API would be much better! I wasn't aware of it, thanks!

Yeah, registration is free for up to 500 req/min if I remember right! Really nice from TfL.

Oh this is cool.

It's a shame that the WMATA bus data here in DC is so bad. The APIs are fine, but the data is all over the place.


Anyone know what that screen is? I don’t see a link in the article

Oh Sorry! I should have added that. Here's a link to it https://thepihut.com/products/hdmi-4-800x480-ips-touch-scree...

If you’re interested in this you might also be interested in a project I’m currently involved with.

There’s a legal obligation in the UK for bus operators to supply live vehicle location positions and static timetable data to the Bus Open Data Service here : https://data.bus-data.dft.gov.uk


Oh that's great to know! There seems to be an experimental API already, will have a look :) thanks!

I know buses rotate and really not practical, but I'd try putting an Apple AirTag somewhere on the bus discretely and track that way.

Wouldn't this cause frequent bus riders to get an alert that they're being followed?

For a minute, before following the link, I was thinking 'why not just look up the schedule? And, did you leave a RasPi on a bus?! Those things are like $200 now! Maybe you can get it back at the bus depot...'

What do you mean 200$

They are a bit hard to find at the moment.

If you want a new raspberry pi 4 8GB today, you basically have to buy from a scalper at a cost of around $200 [1]

Or you can try to monitor https://rpilocator.com more than the scalpers do, and you might be able to get one for $100 instead.

[1] https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2380057.m...


:) We could hide it inside a bus and make a geolocation game!

In some areas, it would be necessary to somehow install trackers on your busses that you use. Depending on the jurisdiction, I've had to deal with busses either being possibly 10 minutes early, or missing their stop time by up to half an hour.

This is a good reason to stop using busses, not add tracking.

That is what I myself ended up doing :)

Awesome! Did something similar using GTFS data. Used an e-ink display though: https://github.com/Scarysize/transit-pi

E-ink is much better choice.

Bus & Mag-Lev?


Bus and ferry in Hamburg

Cool.

I love these types of projects. Being able to automate or make a daily chore easier is so liberating.

As a public transit user, I too have written a dashboard for my transit system on top of the smashing dashboard. It runs on a raspberry pi hooked up to a TV in my front room.

As others below have complained about arrival estimations being unreliable (or these days where we are low on operators and have a bunch of ghosts buses!), I found having a map with real time location gives me a better idea when I should leave:

https://blog.line72.net/2019/08/02/announcing-realtime-bus-t...


I'm amazed at my own programming progress over the past 10 years. I used to read these posts in awe of how people knew how to do such fascinating things that seemed so magical to me. 5 years ago I got myself a raspberry pi and an Arduino starter kit and just started playing around with them. I learned python by practicing on leetcode and other sites and I'm able to recognize a lot of libraries and patterns in his code. It's a bit less magical but I have a much more refined appreciation for posts like this.

Next step is realizing that most code is a liability and you will begin to loathe anything "smart". I'm at this step right now. I'm curious if any graybeard out there can share what the next step is.

I don't even want smart electrical outlets.


It's the natural progression I think.

- Step 1. Know nothing about computer technology, and think it's magic.

- Step 2. Begin to learn about computer technology, and think it's fascinating.

- Step 3. Learn a lot about computer technology, and lose your trust in most developers/engineers that aren't yourself, and avoid said technology.


This is extremely accurate in my experience (besides trusting only myself, I don’t even trust myself). In general, the engineers I work with are much less “techie” than a lot of people I know outside of work.

If only the Chicago CTA api was close to reliable... It's well known for the ghost buses and disappearing trains. However we wouldn't probably need to call Apis, just refreshing their website would work as it's similar to OP's (or Google maps or any other using the apis).

The best solution here would be to secretly implant a RPI zero with gps into every bus


> The best solution here would be to secretly implant a RPI zero with gps into every bus

Ha and then you can also create an API for others with that data :)


Now that would be cool. Although costly, if it were discovered.

Why costly?

> The best solution here would be to secretly implant a RPI zero with gps into every bus

From the title, this was what I was actually expecting the article to be about. :-)


Ha I taped one (with camera) to my window to spot when the FedEx truck was coming. (no vision just periodic images uploaded to local website).

Was this in order receive your personal deliveries?

Yeah my apt complex sucks, doesn't accept deliveries and FedEx doesn't have tracking like Amazon does. Usually I try to change the delivery address to some hold location but sometimes not possible.

It was a lazy update every 5 seconds loop/webpage updates image url with time query. Not great but did it's job.


Looks like I killed my SD card interesting.

Re future work "It bugs me that I’m fetching this information every 30 seconds even though most of the time I won’t use it":

Take a look at Home Assistant, which runs nicely on the same hardware. You get a reasonably capable platform that knows when you're home and can be told a schedule, so you can then limit the checks to times when you'll actually need to know.

https://www.home-assistant.io/


I think that the platform is more than reasonably capable.

Either your usage fits into one of the zillions integrations (especially via HACS), or you can easily build something alongside with pyscript.

The state machine in HA is excellent, I use it for unrelated projects instead of a bus or something like that.

The UI is good and is getting better. I wrote years ago my own dashboard (that uses data from HA, among others) but I am slowly getting used to the idea that I may simply replace it with a full-screen page from HA's UI (especially when I manage to have widgets written in Quasar/Vue running correctly)


> Since I already had an unopened Raspberry Pi 3B that I ordered in 2016 (6 years ago!!)

The key line. RPi aren't any more available as like before, for your new projects.


Yep.. I'd have used an esp8266 for this, and kept the rpi in the box, for when I "really need it".

And it truly is a sad time for rpi-based projects... it really plugs a gap between cheap microcontrollers and power-hungry servers/PCs,...


I wonder if a Pi Pico W would be sufficient to run this, even? waaay cheaper (and available).

I just took a quick glance at the specs, and sure... running an lcd and parsing some text from the web seems totally doable, and in contrast to RPi (3, 4...), you can actually buy picos :)

Now if only this could be done for public school buses that never show up at the same time. Younger kids are required to be dropped off/picked up in the presence of an adult, but one is often left out in the cold/snow/heat/whatever for far too long wondering if the bus will ever arrive, or if they were already passed by.

Now if someone can build something to track ghost trains. I’m looking at you CTA!

I'd definitely recommend you use homeassistant as a front-end for this. You can then update a 'sensor' using an mqtt message.

Small UX idea: what you really care about is not when the next bus arrives, it’s “can you make it?” Of course, you can arrive at the latter from the former, referencing the current time, and roughly how long it takes to get to the bus stop (with reasonable buffer time).

Or you can do this work in code, and put a countdown timer beneath each bus time (such that you walk out the door before time expires and you will make it).


I have a tablet mounted on the wall which displays different kinds of stuff, like weather conditions and forecasts and so on.

Among this is the time of civil dawn, because at this time I don't want to be riding bike anymore since it's getting dark (and colder).

Next to it is a countdown timer with an offset of minus two hours, since I do ride my bike around 1 to 1.5 hours. So when I see that this timer is reaching 0, I know that I'm at my last opportunity to go for a bike ride that day. Once it reaches 0, it shows the countdown to the correct, non-offsetted time of civil dawn, but in another color, so that I know that I still have time to make a shorter ride.

I barely look at the time of dawn, since the countdown timer is so much more informative for me. It let's me know how many hours or minutes I still have to do things.


> civil dawn

Do you mean civil dusk?


Right, sorry and thanks for pointing it out.

My dream UX for that is a single ambient led ranging from green to red, but making it overall usable counting we're two, using two different trains, with different WFH schedule is rather complicated

Check out CityMapper. It handles your use case, as well as multi-transit hops (e.g. bus to subway walking time) as well.

> you can do this work in code, and put a countdown timer beneath each bus time

Some years ago there was a student union at the university where I was studying. We had a space dedicated to us in one of the buildings near the campus area.

Some of the members wrote a program to retrieve subway departures and show them on a screen in our space.

The authors of said program made it simple but friendly. It would show all upcoming departures from the subway station, with the number of minutes until departures, and color coding.

A countdown with green color coding meant that you were likely to make it since there was a lot time left.

Orange color meant that you’d have to hurry up and go to make it.

Red meant that you’d really have to run, and that even then you might not make it.

Very handy system, especially for late nights when you had to pay attention so that you’d get on the last subway for the night that would take you home.

I loved that student union. Easily the best part of studying at the university for me was being a member of that student union. I have so much in my life to thank for the experiences I made from being a member in that student union :D


I made a similar thing in London as well: https://twitter.com/ageitgey/status/1383381051154726920

The TfL API is really pretty good - no need to scape pages.


really cool! and huge!

For anyone who's looking to build something similarly, you want to search for your agency's GTFS Realtime feed: https://gtfs.org/resources/gtfs-realtime/

Transitland offers an API that's easier to use than parsing the agency's XML imho: https://www.transit.land/

None of this will still solve the issue of ghost busses, though. ;)


This is exactly what Web Out Of Browsers (https://woob.tech/) aims to do: get information stored in websites, and offer a programmatic way to use them.

There is an application called woob travel that shows timetables: https://woob.tech/applications/travel. It plugs back ends, each of which is a website. Once parsed, the data is programmatically available for anything.

Bonus point for OP: it is all written in python !


So, a web scrapper?

Yes a web scraper. But it's not (just) an end to end scraper: it gives you easy access to the data so you can do whatever you want with it. Display a table for easy consumption, store it in graphite for historical data, ring a bell when a threshold is reached...

For a moment i though he did a solution for the «ghost buses» . I always used the bus app and it works fine untill, from time to time, you turned 2/3 minutes before to the bus stop, timer goes down, then says due…. And disappears!!! And no bus pass, trust me, i question myself several times. The 254 is a common offender of the “ghost buses” :)

254?

https://tfl.gov.uk/bus/route/254/

I was at a TfL hackathon once, at QMW along this route, but that is irrelevant.

Strangely TfL seemed to be beholden to Ordnance Survey for some copyright issue, it couldn't have been for the Natrional Grid reference could it?

Converting between lat/long to OSgrid was a bit tricky when you have to take the curvature of the Earth into account to get that last bit of accuracy.

https://www.movable-type.co.uk/scripts/latlong-os-gridref.ht...

PS For some reason I almost wrote Couverture of the Earth, just trying to reduce chocolate intake.


Mmm… maybe it was the 271 now that i think of it. But it happen with others too

It's somewhat comforting in a bad way that this isn't an issue isolated to my city. We even have the same name for them.

While this is a nice project, it's basically just simple web scraping. I was hoping for something a bit more interesting, like decoding the radio signals for the bus stop displays[0] and using that data. If it's available from an API it of course makes more sense to use that though.

[0] https://www.windytan.com/2013/11/decoding-radio-controlled-b...


Or setup a RPi camera, with OpenCV and recognize your bus while it's around the corner 2 streets before coming to the stop so you know you'll have a full minute until it gets there. I mean a 30 meters pole on top of your house isn't something that hard to do. And the programming of that is so much more fun that just the web scrapping. Plus is actually more accurate than a web scrapping that can have de-synchronizations between that and the real bus.

I feel lucky that in India we have an app to track the live location of all the buses. I almost use it daily and plan my day accordingly.

I'd been exploring a similar project a few years ago, and ended up making a map that showed the location of buses along the routes I tended to take. I was pretty happy with it...except for the fact that it was built for the town (Duluth, MN) I went to school in, and I didn't get it finished until finals week of my last semester, and I left town immediately after!

https://chandlerswift.com/projects/bus-tracker


That board deserves to be on display at City Hall or something. Inspiring work and I lament with you that you weren’t able to use it more than a few days.

Thanks! :) I definitely thought about seeing if I made a polished version, if I'd be able to sell a copy to the local transit authority, or something. But it made for a great conversation piece, and was tons of fun to build, so it's definitely a success as far as I'm concerned.

Small world. I did this on behalf of BlirpIt for UVM back in 2009 or so. We had trackers on each bus, and they would report location on a certain interval, or whenever the speed changed by a certain threshold from the speed at last report. We had server-side geofences on each bus stop, and defined a "route" that was just a chain of stops, and then we stored a moving average of time between each pair of stops and that was about it. One of the more fun projects I was doing back at that time!

Small world indeed.

> then we stored a moving average of time between each pair of stops and that was about it.

Modest. It got a little more sophisticated once we brought code inhouse.


I built something similar recently, linking it here in case people want similar projects for inspiration: https://www.kerrickstaley.com/2022/02/25/transit-panel

I think the biggest difference is that I wrote my version as a web app without any backend (it gets data from a REST API + a hardcoded schedule as a fallback), so that I could host it on GitHub pages and display it on a cheap Android tablet. I still use it every day and am glad that I made it :)


I did a similar thing as a web page an eInk tablet. What doubled the value for me though was adding a weather widget to the corner. It shows me:

* the current temperature

* the high or low over the next 12 hours

* graphical forecast of the 12 hours (including chance of precipitation)

which is all the info I need to know to dress for wherever I'm taking the bus to. You can get all this info (including graphics!) from weather.gov DWML, e.g. https://forecast.weather.gov/MapClick.php?lat=40.7143&lon=-7...


I recently moved to Belgrade, Serbia and found out that Google Maps are giving sometimes inaccurate data about public transit. All locals are saying that they are using Moovit app. And that app on Android has a widget showing next bus/tram/trolleybus: https://imgur.com/a/OsDyLIR

In February 2020 I did almost same: used beautiful soup to parse njtransit's arrival time website to send a push notification to my phone about 10 minutes before bus arrives to my stop in the morning, so I had enough time to get there. Unfortunately, something happened that year, which forced me not to use bus any more.

> It bugs me that I’m fetching this information every 30 seconds even though most of the time I won’t use it.

You don't have to. If the program knows that the bus is 20 minutes away, it could wait 10 minutes or so before doing a refresh (assuming the bus doesn't magically gain a lot of speed in the meantime).


These are my favorite kinds of projects. Not some major disruptive application, just a tiny project that solves a real problem for a real person. Great job OP!

I had this exact idea more than 5 years ago, but the only thing my city provided were horrible PDF timetables. No GTFS, not API, not even an easy to scrape HTML table. Ever since then, my constant side project has been to create a hub for transit and other mobility data from all providers in the country (Slovenia) and just this year, all the work has started coming together.

That stupid bus clock will become not only possible, but trivial to build in just a few more weeks. It took a many meetings with company executives and government officials, freedom-of-information requests, hours of reverse engineering apps and subsequently arguing with lawyers... I accidentally stumbled onto two entirely separate multi-million-euro cases of clear corruption in public tenders, several CVSS >= 7 vulnerabilities, got both job offers and legal threats (once from the same company) and throughout the process learned more about multiple different CS fields than I did about even a single one in 3 years of university.

So if anyone else wants to build such a clock, but is disappointed that their local bus company doesn't have an API, don't be discouraged. There is always a way. It might cost you a year's worth of free time and most of your sanity, but you will end up with some shiny LEDs on that wall that still won't prevent you from missing the bus and a bunch of extremely niche knowledge. 10/10 would recommend


Legal | privacy