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.

Prerequisites

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

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
 * WE\elcome to Gnd_to_Vcc!!
 */
 
#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();
}

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:

  • ESP-01 – read Best ESP8266 Wi-Fi Development Boards
  • Pushbutton
  • 10k ohm resistor
  • Li-ion/LiPo battery
  • Voltage regulator:
    • MCP1700-3302E
    • Ceramic capacitor – 100nF
    • Electrolytic capacitor – 1000uF
  • Plastic box enclosure

Schematics

Here’s the schematics that you need to follow:

https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

Here’s the breadboard version:

I recommend assembling the circuit first on a breadboard to test if it’s working properly.https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

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

https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

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:

https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

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.

Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: