Nerding Out With the Amazon IoT Button

My Amazon IoT button, affixed to the whiteboard next to my desk, with artwork by the project’s intern.

A little while ago Amazon announced a programmable button, based on the Dash button, and made for developers. It’s basically a wireless button that sends a signal to Amazon’s cloud services when you press it. That’s it. A nerd can program that service to react by performing any function. I’d been tinkering with Amazon’s Lambda service for some projects. Lambda can be used to react to these kinds of events so I figured what the hell, let me order this button.

Prior Art

Some fellow nerds have used Dash buttons before to do things like summon an Uber, or pizza, or beer. You press the button and a chain of events takes place that brings something to your door. A dad used a Dash button to track his baby’s poop activity. In all of these use-cases, it’s known exactly what action will be taken and what the result will be. I wanted to try pulling in more data about the moment when the button is pressed. Maybe this would add a little more personalization and even serendipity.

The button and intern after unboxing.

Hangry Nerds

As anyone who works from home knows, moments arise when one may forget to eat or forget to go outside, among other things. You might as well be in a windowless casino where time has no meaning.

What if pushing this button hits an Amazon server, checks the time, looks for restaurants that are open right now, and sends me a text as I’m headed out the door?

The chosen restaurant should fit the moment: breakfast in the morning, lunch in the afternoon, or dinner in the evening.

That’s getting pretty nerdy already but can we take it up a bit. What if I’m heading out the door into a torrential downpour, blizzard, or ice storm? All things I should probably see coming, but I want my almighty Button to take the weather into account when it picks a place to eat.

OpenWeatherMap has a free API that returns current weather conditions and forecasts from around the world. So if the weather sucks, it can just pick a place that delivers food. Domino’s apparently has an API but for this quick project, I’ll settle for getting a phone number to call.

Now, if it’s nice out I should get off my ass. So let’s find a place within 200 meters of my location. And let’s get the address along with the time it will take to walk there. The Google Maps Directions API is perfect for this.

Pseudocode for Pseudoproblems

Now things are getting complicated enough that we should write down the basic algorithm. The full code is below but here is the pseudocode:

Press the button
If the weather is clear
Search restaurants within 200m
Filter for 'open now', sort for good ones
Select randomly
Get address and ETA
Else
Search restaurants that deliver within 10km
Filter for 'open now', sort for good ones
Select randomly
Get phone number to place an order
Eat

This is already more intelligent that most VC-funded bots.

Here’s an example of what I get:

And when it started to rain:

Praise me Button

Initially I thought of using the Yelp API for all this, but it doesn’t let you filter by how expensive a restaurant is. Google’s Places API does. Now I could make sure the Button doesn’t recommend something crazy expensive.

… unless I can afford to splurge. How do I let the Button decide? I can connect it to my Stripe account! If your regular bank has an API, by all means try that too. But I figured tying restaurant recommendations to Stripe is a great way to motivate your other projects that pay you via Stripe.

Press the button
If the weather is clear
Search restaurants within 200m
Filter for $ restaurants 'open now', sort for good ones
Select randomly
Get address and ETA
Else
If Available + Pending Stripe balance > $1000
Search restaurants within 10km
Filter for $$$ restaurants now, sort by prominence
Select randomly
Send address and drive time via SMS
Summon a Lyft?
Else
Search restaurants that deliver within 10km
Filter for $ restaurants 'open now', sort for good ones
Select randomly
Get phone number to place an order
End
End
Eat

So the new algorithm adds this condition: if there’s a bunch of money coming into Stripe, let’s widen the distance range and look for some swankier places. Since it’ll probably mean having to drive, it should text the drive time with the address.

Here’s what I get:

Now, here I could have summoned a Lyft automatically since I know the pickup and dropoff locations. But I’ll leave that as an exercise for the reader. It involves dealing with OAuth and I wanted to wrap this thing up in a few hours.

You can check out the actual code for my Lambda function on Github.

There’s a ton of stuff I could add. One thing you’ll notice in the code already is a way to just give me the swanky option by double-clicking the Button. Amazon will send three types of signals: single-click, double-click, or hold. We could also book table (single-click for solo, double-click for a +1), we could connect Fitbit to see if we deserve to indulge, we could have Alexa tell us what to do…

But for now back to real work.


Follow or reach me on Twitter.

Have you played with the IoT button? Let me know what you come up with.

Warning: recommending this article by clicking the❤️ below will only encourage more gratuitous nerding.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.