My Cookie Dash Button Recipe

I’ve conquered the Dash Button. I may not have done it entirely on my own, but The Cookie Button is in business (sort of).

As of right now, if I push my Pop Tarts Dash Button, I instantly get a text message, and a Tweet is sent 20 minutes later.

I also have a thing for donuts.

However, the system isn’t perfect. I’m using a free Twilio account, so the text message includes ‘Sent from your Twilio trial account’, and I haven’t chatted with anyone at 1215 yet, so the Tweet is posted to my personal account. But that’s not the point. The point is, I learned a ton, and made something happen! That excites me.

Things are about to get really nerdy, so if you don’t care about the logistics of this project, skip to the end.

<code talk>

If you’re familiar with GitHub, you can check out the whole project there.

On GitHub, I had to play it cool, you know. I tried to act like I really knew what I was doing. But here, I’ll tell you all about my hardships.

I already complained a lot in my last blog post, but now I have solutions to those issues. Let’s walk through each step of this project. I’m going to do my best to explain things better than developers do, so that all my non-tech friends can understand.

Step 0 — Things to do before getting started

This is something I wish developers would point out online. Generally, you can’t just start writing code and running programs. You have to install things that will allow these programs to work. For this particular project, you’ll need a few things.

  • A good text editor. I suggest Sublime. It includes pretty colors.
  • Node.js. I think if you have a Mac, it’s already installed? Doesn’t hurt to check.
  • This node-dash-button package. It holds all the information you need to make your Dash Button do fun stuff.

Step 1 — Connect Dash Button to WiFi & find MAC address

So here’s how the Dash Button works. Its only function is to interact with the WiFi network. Traditionally, as soon as it pings your network when pushed, your Amazon app kicks in and orders your Pop Tarts, or Toilet Paper, or whatever weird thing you need RIGHT NOW.

In this case, we need to stop Amazon from ordering anything. All we need is for the button to ping the WiFi network. To do this, you set up the Dash Button as usual in your Amazon app, but cancel out of the set up process when it asks you to select a product.

Now, the Dash Button can interact with the network. We have a trigger! But, we can’t do anything with this trigger until we figure out what the Dash Button’s unique MAC address is. We need to be able to tell our function, “When this specific device pings the network, do this.”

Identifying the MAC address proved to be pretty difficult. After trying a few different tutorials, I tried the instructions in this GitHub project and successfully found my Dash Button’s MAC address. The instructions to use Wireshark are very clear on this GitHub page, so I won’t attempt to explain it myself.

At the time, I thought I had surmounted the most difficult task of this project. Ha.

The future, laughing at me.

Step 2 — Send an SMS message when pushed

Now for the fun part. This dash-sms GitHub project is all you need to send an SMS message with your Dash Button. Download all the files to get started.

This was one thing I was clueless about when I first looked at GitHub. I had no idea if I was supposed to download everything, or just look at the files or what. So, to anyone who doesn’t understand GitHub, if it’s a public project, you can download the files and modify them for your own use.

The important files here are index.js and config.json.sampl. The index.js file is the bread and butter (or should I say cookie and milk) of this project. It holds the actual functions that will be performed when your Dash Button is pressed. The config.json.sampl file holds your individual credentials — your button’s MAC address and Twilio information.

Before adjusting any code, you’ll need to create a Twilio account. Twilio will allow us to send an SMS message to yourself or someone else, from a Twilio-owned phone number. Setting up your account is fairly straight forward, and then you’ll need to set up a phone number.

What you need to grab from your Twilio account are your ‘Account SID’ and ‘Auth Token’. These are found in your Twilio Console Dashboard. I made the initial mistake of pulling the SID from my Twilio phone number, so don’t do that. Just take the one on your dashboard.

At this point, you should have all of the information you need to send that SMS message! Create your own config.json file, copy & paste the code from the config.json.sampl file, and add your credentials.

{
"button": {
"id": " " // MAC address of your Dash Button
},
"twilio": {
"sid": " ", // Twilio sid
"token": " " // Twilio token
},
"message": {
"from": "+15555555", // phone number Twilio provides you with
"to": "+15555555", // phone number you want the message sent to
"body": "Cookies have arrived!" // message
}
}

This is what your config.json file should look like, but everything should be filled out. Now let’s run it!

It’s easy to run in the terminal. Navigate into the folder that holds all these fun files, then type $ sudo node index.js, and voila! Press your Dash Button and you should get a text within ~7 seconds. When I got my first text I truly lost it. Ask anyone who was around me at the time. It was a great moment.

I owe this entire step to Ashutosh Sanzgiri, so thank you, whoever you are.

Step 3 — Post a Tweet, 20 minutes later (for selfish reasons)

I’m pretty proud to admit that I figured MOST of this step out on my own.

Here was my logic. I knew that, in my index.js file, there’s a bunch of code that recognizes when the Dash Button pings the network, and then triggers the code to send an SMS message. I just needed to add another function in there, something that would post a Tweet.

// This is the code that detects the Dash Button
console.log(‘waiting for dash button to be pressed…’);
dash.on(‘detected’, () => {
console.log(‘Dash button detected!’);

I came across this Twitter for Node.js page that holds pretty much everything I needed. Time to download another package. Before moving forward with this step, in your terminal, type $ npm install twitter.

Following along with the instructions on this page, I created a Twitter developer account — I felt very cool doing so, if we’re being honest. Once you’ve created a Twitter developer account, create an app. Under the ‘Keys and Access Tokens’ tab, you’ll find all the credentials you need.

Below is all the code you need to add to your index.js file, to post a Tweet.

var Twitter = require('twitter');

var client = new Twitter({
consumer_key: '', // Super secret stuff
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
var params = {screen_name: 'ray_fenz'}; // Your Twitter username

// The code below will be placed after the console.log code, this is what is triggered when the Dash Button is pushed.
client.post('statuses/update', {status: 'Cookies have arrived!'},  function(error, tweet, response) {
if(error) throw error;
console.log(tweet);
console.log(response);
});

Remember, if you want to see my entire index.js file, check out my GitHub page. It might make a little more sense to see this all put together.

At this point, when my Dash Button is pushed, the SMS message and the Tweet are both sent right away. That’s not my plan. I want the Tweet to be posted 20 minutes later. This little hiccup took me about 48 hours to figure out.

What I need to do now is add the setTimeout function. This W3Schools page gave me the simplest explanation of how to use this function, but it wasn’t quite enough. I needed the help of some developers to nail down this step, but I’m still going to try and explain it to you guys.

Here’s the basic anatomy: setTimeout(function, milliseconds)

  • function: This is what I want delayed. In my case, it’s the function that posts the Tweet.
  • milliseconds: A delay is defined in milliseconds. I guess most people want things to happen quicker, but in my case I want to delay 20 minutes, which is 1,200,000 milliseconds. Fun!

The issue I ran into is that my Tweeting function hadn’t been defined. So, I adjusted my code to look like this:

function postTweet() {
clienttwitter.post('statuses/update', {status: 'The cookies have been delivered!'}, function(error, tweet, response) {
if(error) throw error;
console.log(tweet);
console.log(response);
});
}

Now, I can just refer to postTweet, and this file knows that it’s the function posting a Tweet.

I was also told that it’s best to define the delay elsewhere, instead of within the function itself. So, I created a variable, tweetDelay, set to 1,200,000 milliseconds.

var tweetDelay = 1200000;

My setTimeout function is now short, sweet, and to the point.

setTimeout(postTweet, tweetDelay);

It’s saying this: We’re going to set a timeout for something. That something is the postTweet function, and we’re going to delay it by the time defined by the tweetDelay variable, which is 1,200,000 milliseconds, or 20 minutes.

I’m going to assume that no one followed that explanation. But, writing it out actually just made it easier for me to understand. Selfish, yet again.

And that’s it! I saved all my files, ran $ sudo node index.js in my terminal, pushed my Dash Button, received a text, and saw the Tweet 20 minutes later. It really, truly worked.

Thank you, thank you.

</code talk>

So, guys. After this extremely long and nerdy post, it saddens (and excites me) to say that I’m not done. I’ve learned that the Dash Button, or the Dash Button alone, isn’t enough to make my Cookie Button function in the real world.

WHAT? Why not? Well, this function needs to be running on the same WiFi network as the Dash Button. I, however, do not have the ability to just sit my laptop at this coffee shop to run the function all day. To get around this, I could get a Raspberry Pi (a tiny computer) to run the function constantly and sit at 1215, connected to their WiFi network. I don’t really want to do that, though.

Here’s what I am going to do. There’s this really cool company in Cincinnati called Losant. They specialize in this kind of stuff, building ‘The Internet of Things’. I have a Losant kit, and with this kit, and I will create a workflow that is very similar to what I’ve just done here, but much simpler. The kit has a button, so when that button is pushed, it will trigger the SMS message and Tweet.

I know, I know — I just wasted two weeks of my life building out something that I won’t even use. But hey, I learned a ton, and that was the point of this project anyway.

I’m going to do my best to knock out this Losant project pretty quickly, but it may be another two weeks before that’s functioning — I’m about to start a new job and will have significantly less free time, so bear with me.

Big thanks to all of you smart people who helped me out. I owe you a drink (or a cookie).


Show your support

Clapping shows how much you appreciated Rachel Fenner’s story.