ESP8266 Wi-Fi Button – DIY Amazon Dash Button Clone

In this project you’re going to build an ESP8266 Wi-Fi Button that can trigger any home automation event. This is like a remote control that you can take in your pocket or place anywhere that when pressed sends out an email. It can also be called a DIY Amazon Dash Button clone.

Watch the Video Version

This guide is available in video format (watch below) and in written format (continue reading this page).

Prerequisites

Before proceeding with this tutorial we recommend taking a look at these resources:

If you like ESP8266 and you want to learn more about it. I recommend downloading my course: Home Automation using ESP8266.

Introduction

The Wi-Fi button is not a new idea and this concept was popularized by Amazon that created the Dash button, which is a small button that instantly orders a product to your home.

Since the ESP boards are so inexpensive, we can make a similar project that works like the Dash button, but with our own twist.

Instead of ordering a product we can turn on a light, toggle a lamp, send a value, trigger an email notification and much more, as you’re going to see by the end of this project.

IFTTT

For this project we’re going to use a free service called IFTTT that stands for If This Than That.

This service is used to automate a wide variety of tasks online. In this case, we will send an email when the ESP pushbutton is pressed.

Type in your browser https://ifttt.com and click the “Get started” button in the middle of the page. Complete the form with your details and create your account.

Creating the Applet

Open the “My Applets” tab, press the create “New Applet” button.

Click the “This” word and search for the “Webhooks” service.

Receive a web request

You need to type in the event name button_pressed. With the Webhooks service you can trigger an action when you make an HTTP request to a specific URL.

Click the “Create trigger” button:

Now press the “That” word and search for the Gmail service.

Send email

If it’s the first time using the Gmail service with IFTTT, a new window opens and you’ll have to grant access, so IFTTT can send out emails through your account.

Choose the “Send email” option and enter the email address in the “To address” field where you want to receive your notification.

You can customize the subject and body of the email, but for demonstration purposes I’ll leave the default values. Finally press the “Create action” button.

You should have your Applet created after clicking “Finish”:

Testing the Applet

Open the “Search” tab, find the Webhooks service and open it.

Go to the “Documentation” tab.

Here you can find your unique API KEY that you must keep private. Type in the event name, button_pressed. Your final URL should appear in the bottom of the web page. Copy that URL.

Open a new tab in your browser and hit enter. You should see this message saying “Congratulations!”.

Open your email client and the new message should be there.

In case the email didn’t arrive after a few seconds, I recommend double-checking the URL and see if you’re using the correct event name, both in your Applet and in your URL.

Note: if it worked save your unique URL in a Notepad, because you’ll need it later in this project.

Uploading Code

Here’s the code that you need to upload to your ESP board. You need to change three variables: SSID, password and resource.

/*
 * ESP8266 Wi-Fi Button
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */
 
#include <ESP8266WiFi.h>

// Replace with your SSID and Password
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Replace with your unique IFTTT URL resource
const char* resource = "REPLACE_WITH_YOUR_IFTTT_URL_RESOURCE";

// How your resource variable should look like, but with your own API KEY (that API KEY below is just an example):
//const char* resource = "/trigger/button_pressed/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy";

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

void setup() {
  Serial.begin(115200); 

  initWifi();
  makeIFTTTRequest();

  // Deep sleep mode until RESET pin is connected to a LOW signal (pushbutton is pressed)
  ESP.deepSleep(0);
}

void loop() {
  // sleeping so wont get here
}

// Establish a Wi-Fi connection with your router
void initWifi() {
  Serial.print("Connecting to: "); 
  Serial.print(ssid);
  WiFi.begin(ssid, password);  

  int timeout = 10 * 4; // 10 seconds
  while(WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if(WiFi.status() != WL_CONNECTED) {
     Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: "); 
  Serial.print(millis());
  Serial.print(", IP address: "); 
  Serial.println(WiFi.localIP());
}

// Make an HTTP request to the IFTTT web service
void makeIFTTTRequest() {
  Serial.print("Connecting to "); 
  Serial.print(server);
  
  WiFiClient client;
  int retries = 5;
  while(!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if(!!!client.connected()) {
     Serial.println("Failed to connect, going back to sleep");
  }
  
  Serial.print("Request resource: "); 
  Serial.println(resource);
  client.print(String("GET ") + resource + 
                  " HTTP/1.1\r\n" +
                  "Host: " + server + "\r\n" + 
                  "Connection: close\r\n\r\n");
                  
  int timeout = 5 * 10; // 5 seconds             
  while(!!!client.available() && (timeout-- > 0)){
    delay(100);
  }
  if(!!!client.available()) {
     Serial.println("No response, going back to sleep");
  }
  while(client.available()){
    Serial.write(client.read());
  }
  
  Serial.println("\nclosing connection");
  client.stop();
}

View raw code

Here’s how the code works:

  1. It starts the serial communication at a baud rate of 115200;
  2. Runs the initWifi function that establishes a Wi-Fi connection between your ESP and your router;
  3. Then, it executes the makeIFTTTRequest function that will make a request to the IFTTT service and ultimately IFTTT will send out an email.

About the Deep Sleep Function

Finally, we’re using the Deep Sleep function, so that the ESP is always off and consumes very little power. The Deep Sleep function with ESP8266 was covered in greater detail in a previous guide that you can find here.

In summary, when you press the pushbutton the ESP wakes up, performs an action and it goes back to Deep Sleep mode to save battery power. It’s pretty simple how it works.

After adding your SSID, password and URL, upload the code to the ESP. You can click here to learn how to upload code to an ESP-01 using an FTDI programmer.

About the circuit

We want this device to be portable and easy to make, so we’re going to power the ESP with a Lithium battery.

Voltage Regulator

To power the ESP8266 safely with a Lithium battery you need to make a voltage regulator circuit. We’ve also covered that subject in a previous guide that you can read here.

Parts Required

After having the code running on the ESP-01, these are the components that you need for your circuit:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Here’s the schematics that you need to follow:

Here’s the breadboard version:

I recommend assembling the circuit first on a breadboard to test if it’s working properly.

Then, you can make a permanent circuit with a small strip board, a few wires and plastic box enclosure to store everything.

After assembling everything, here’s how the final product looks like:

It kinda looks like a remote control. Now you can take it or place it anywhere.

Let’s test it. When I press the pushbutton, I receive an email within a few seconds:

Note: even though the ESP is powered with a battery it can last weeks or even months, because it’s always in Deep Sleep mode.

Wrapping Up

I think it’s important that you keep in mind that the applications for this project are endless. For example, the event “button_pressed”, depending on where you place your ESP can have a different meaning.

If you place it as a doorbell button, you can use it to know if someone is at your home.

Also note that instead of using a 3rd party service like IFTTT, you could:

  • Turn on relay that is connected to another ESP8266;
  • Send a request to another device in your network;
  • Make an HTTP request to Node-RED to trigger an action;
  • Publish an MQTT message;
  • Connect to any other home automation software.

Taking It Further

You can also replace the pushbutton with other sensors or actuators. For instance if you replace the pushbutton with a PIR motion sensor, you can be notified when someone enters a room in your house. Or you can use it to detect smoke in a room:

Replacing the pushbutton with a magnetic reed switch allows you to detect if someone opened a door or window. You can even attach it to a mailbox to see when you receive actual letters in the mail or other packages.

That’s it for now. I hope this project was interesting and you can apply these concepts to your own home automation projects.

This is an excerpt from my Home Automation using ESP8266 eBook. If you like ESP8266 and you want to learn more about it. I recommend downloading my course: Home Automation using ESP8266.

I hope this guide was useful. Thanks for reading!



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

46 thoughts on “ESP8266 Wi-Fi Button – DIY Amazon Dash Button Clone”

  1. It’s perfectly possible to do this with an Amazon Dash Button – at, I suspect, significantly lower cost, plus being smaller and neater.

    The only downside to them is that the battery is not rechargable and it’s not easily replacable.

    If you quit out of the button setup process, immediately before tying it to a product purchase, then it has all the information it needs to connect to the network.

    I have several working with Node-Red and the actions are all contained within the flows.

    For those who want to use the ESP-01, this might be an interesting addition to this tutorial:

    youtube.com/watch?v=lmVK5cGVrpqQ

    Reply
  2. Just another thought – if this project used a LiFePO4 battery, then you could do away with the regulator and it’s attendant losses…

    Reply
  3. I was just going to build one, now it will be easy, thanks. I will try to add 3 leds. Yellow, Green, Red. That indiciets connecting, send ok, send fail. I hope not too diffucult ?

    Reply
    • Hi.
      We’re glad you’ve found this project useful.
      That is a great addition to the project, and it should be simple to add.

      Reply
  4. Can I ask why there is “Serial.println” when there is no Serial monitor output? The code would be smaller and also faster.

    Reply
    • Hi Pavel.
      The Serial.println are used to help readers to debugg their projects and make it easier for them to see if something went wrong.
      We encourage everyone to remove those lines after they’re sure the project is working properly.
      Regards,
      Sara 🙂

      Reply
  5. Awesome idea,
    perhaps you can made more similar process with DASH Button that after being pressed it has a feedback led that change the color after order successfully placed, in this case maybe using webhook IFTTT applet that after this email had replied then automatically send a response to ESP8266 web server?

    Reply
  6. Having just needed to create an ESP8266 MQTT button (there was mains power available, so an Amazon Dash Button was pointless), something hit me…

    MQTT’s QOS (Quality Of Service) is only valid if both ends of the ‘conversation’ are using the same setting (as I understand it). So how would you set the QOS for publish and subscribe in the pubsubclient library?

    The readme file for the library was very unclear (to me).

    Reply
  7. Just what I was looking for! Will be able to improve security. Had a rough idea of what I wanted to do and the setting up of IFTTT described was comparable to having a good road Atlas in new territory

    Reply
  8. Hi,
    You did not show connection of gpio 16 to restset, Do you have to connect gpio 16 to rst for deep sleep to work with your code?
    Thanks

    Reply
  9. Works well, but I’ve modified it to turn on a relay to keep power applied until all tasks have been performed, then it depowers the relay there by cutting the power completely. This way it will virtually never run out of power, the batteries only get used for the few seconds after the button is pressed.

    Reply
  10. Hi
    thanks for an amazing example.
    I want to send request to to my server not to IFTTT, how i can modify this code?

    please suggest.

    thanks

    Reply
    • Hi.
      You need to replace the server variable with your server URL.
      Then, you need to customize the request with the information you want to send in the following lines.

      client.print(String(“GET “) + resource +
      ” HTTP/1.1\r\n” +
      “Host: ” + server + “\r\n” +
      “Connection: close\r\n\r\n”);

      I hope this helps.
      Regards,
      Sara

      Reply
  11. I really like the button push idea for a project I am doing. Do you see how this button push could trigger a n RGB LED ring to turn on Red that is controlled by an esp32 board? I tried mqtt but did not want to spend money on holding up a aws config. Any better way to light up a RGB LED via an esp32 with that wireless button?

    Reply
  12. Hi, the given example is working well, but I try to replace the button with a reed switch whitout success. I want to build a windows contact and need to know, if the switch is open or closed. Is that possible? How?

    Reply
    • Hi Dirk.
      You need to see if the reed switch sends a HIGH or LOW signal when the door is opened. The ESP32 will only wake up when it receives a LOW signal on the RST pin.
      So, you need to wire the reed switch accordingly.
      Regards,
      Sara

      Reply
  13. Hi Rui and Sara, congratulations for the interesting article and as always very clearly exposed. My question is: if it is possible to connect this to another ESP configured as AP to turn on a led? I think the part of the connection will be able to modify it, but then is there a way to eliminate the code that communicates with IFTTT without thereby tilting the rest of the code? Thank you.

    Reply
  14. Hi Rui and Sara, thankyou for the project, i try to follow the IFTTT but when come to press the “That” word and search for the Gmail service. at the IFTTT “Choose action service” page, there is no Gmail service available.

    I am stuck there and can not complete the project, please help and advice how to proceed from there.

    Thanks in advanced.
    Alex Tan

    Reply
  15. Hello! I made this button and was very happy with it. But, I was thinking if there is any way to change the ssid and password without hard coding? I saw many codes for changing the ssid and password without hard coding but I was unable to get them to work with this code. If you can provide the code for this button in which we can change ssid and password without having to remote the esp8266 from the little box and then uploading it, it would be very helpful. Please help in any way that you possibly can. I’m too much interested in this button and I’m thinking of making 2 or 3 of these. Thank you.

    Reply
      • Hi, I would like to say thanks for replying first of all.
        I have tried that WiFiManager library and it’s doing its job. But the problem is that after it gets connected to the wifi network it doesn’t send the IFTTT request. I have tried to modify the code in so many different ways. I have also tried to paste the ifttt credentials before the setup and then the void makeIFTTTRequest part after the void loop(which is empty of course) but it isn’t working. I’m actually very new to Arduino programming. I bought my first arduino just a month and a half ago and this esp8266 module just a few weeks ago. I saw this ifttt button video on YouTube and was very excited to make this. I gave this a try and was very happy with it but the problem is that I can’t take it anywhere as I’ve coded my home wifi credentials. I know it might be too much to ask for but if possible and if you have some spare time someday can you please provide the code for this button in which I can change the wifi credentials through the browser method.
        I’ll be waiting.

        Once again thank you.

        Reply
        • Hi.
          Unfortunately, due to the amount of work, we don’t build custom projects for our readers.
          You can try adding some Serial.print() statements along the code to find out where the code gets blocked or if there is any condition preventing the code from proceeding to the IFTTT request.
          Additionally, you can also try to find help in the ESP8266 or Arduino forums.
          Good luck with your project.
          Regards,
          Sara

          Reply
          • Thanks for the support but I said two days ago that I figured it out and you even replied to that comment saying “Great!”. Then why did you still reply to this comment. I’m not complaining or anything so don’t take it the wrong way.
            Also I made the project completely and now it’s working flawlessly. Thanks for the article and the support in the comments.
            Regards,
            Srijan.

  16. To use this as a door status monitor (open and close) we need active low pulses for both door open and door close events. RST is pulled high by the resistor most of the time. I think we are missing an event when the switch is restored back.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.