Transit bus beacon: Code help please to make it better

Hello wonderful spark community

I am making a visual beacon for transit bus arrival at my stop. Often times in lab, my hands are full of goo and I want to check if my bus is coming. I cannot launch the app or don’t have my phone. I need a way to just glance over and see if my bus is coming and whether I will be able to make it to it or not.

The way I am implementing this is very specific to my application. I am using the OneBusAway REST API for Puget Sound. Spark core checks their API between the hours I usually travel. The method OneBusAway provides give bus arrival information for all the buses coming at a particular stop between X and Y minutes in future. To keep the response length minimum, I check for buses arriving within a minute, for example show me the buses coming between 14 and 15mins from Now. And I do this every minute, so in theory I shouldn’t be missing any buses. I am using the Rest client library to invoke the GET requests.

Then, I parse the response using the wonderful JSMN library by @peekay123. In the parsed response, I look for relevant information and start an animation on neopixels which lasts until the time I could catch the bus. The idea is inspired by the reddit mail notifier and the program structure is motivated from @BDub’s Bitcoin example.

The beacon consists of engraved acrylic sheet edge lit with a neopixel strip. The base is 3D printed. This is how it looks when it’s time to go home: https://www.dropbox.com/s/qfherttbwgnjqyp/VID_20150113_112328.mp4?dl=0 [12 secs]

But I am far from a reliable solution. My core crashes randomly after issuing a few API calls. Almost always it’s a memory manager hard fault (3 red flashes). As you can see the code pasted below, I am rebooting after every ten unyielding calls (bus in not coming) or one successful call to clear the memory but that doesn’t seem to help.

I am an alright coder but can put things together. I am hoping if someone can review my code and provide pointers to make it more reliable. Mostly, I am looking for inputs on optimizing the core memory and better procedures to do what I am doing.

You can find the code at:
http://pastebin.com/u5ehNj6w

A sample XML response is here:
http://pastebin.com/b3xzFnJs

A sample JSON response is here
http://pastebin.com/3zWhg4vk

And this is the program structure:

Any other feedback, feature ideas, questions or comments are always welcome. Thanks!

6 Likes

I did something similar, maybe it gives you some inspiration: https://community.spark.io/t/bus-stop-light-code-review/1176

I am using different libraries though.

Are you possibly running out of RAM? What are the stats at the bottom of Spark Build after you compile? There’s a little (i) info bubble next to the Code Verified! Great Work (i) message. Click that and copy/paste here :smile:

Thanks @Coffee. I will go through your project and come back with questions. Thanks for sharing.

@BDub No, I feel I still have memory left. It crashes after it has issued few API calls. Here’s the compilation output:

Output of arm-none-eabi-size:

text data bss dec hex
100064 1328 12136 113528 1

In a nutshell:
Flash used 101392 / 110592 91.7 %
RAM used 13464 / 20480 65.7 %

i looked at your code... are you still calling the api every 10 seconds...

is that really necessary? are you sure that the API allows you to call that frequently? are you sure that the response isn't timing out?

i'm guessing that once a minute, or even every two, three or even five minutes is enough, unless busses in Seattle are using afterburners. (Boeing reference).

PS. If the last call doesn't get a return, then i'd call again (if I knew i under the bar in rate-limiting).

easy to test with curl

Thanks @BulldogLowell for looking at the code. I am calling the API every minute. My delay loops aren’t elegant but am sure I am not calling the API that frequently. Can you please direct me where you found that I am making frequent calls.

The api calls are limited to 1000/accesstoken/day.

The response shouldn’t timeout if the calls are infrequent, right? How can I build error handling for timeout responses. Start a timer in background and wait for a response until it expires?

The busses is Seattle used to more frequent. Sadly with new budget cuts underway I could get away with sampling every 5 mins even.

@rahilj,

have you tried making the API call using curl and checking to see if you get the responses you expect, in the frequency you wish to transmit?

1 Like

Yes, I have. I get response reliably even if I issue requests every second or less! If you wanna give it a shot, they have a test accesstoken for the api. Try this GET request:
http://api.pugetsound.onebusaway.org/api/where/arrivals-and-departures-for-stop/1_9550.json?key=TEST&minutesBefore=0&minutesAfter=15

I am sure their service doesn’t discriminate between ‘test’ token and ‘real’ token.

I would ordinarily plow through the problem, but looking at the XML return and with the power of Webhooks I’d consider starting over (using them). It will make this all a whole lot easier, IMO.

look at the weather example, I think it is just what you want to do.

Start by getting Spark.variables( )s updated and then layer in the cool led effects.

Webhooks. Yes! That’s a great idea. Thanks you.

1 Like