ESP8266 IFTTT BUTTON

Posted by

What You Will Need

To complete this IoT Button project, you’ll need the following components:

Component Description
ESP8266 module WiFi-enabled microcontroller (NodeMCU or Wemos D1 mini recommended)
Pushbutton Momentary pushbutton switch
Breadboard Solderless breadboard for circuit prototyping
Jumper wires Assorted male-to-male jumper wires
Micro USB cable USB cable to power the ESP8266

You’ll also need a computer to program the ESP8266, along with the following software:

  • Arduino IDE
  • ESP8266 Arduino Core
  • IFTTT account

Choosing an ESP8266 Module

The ESP8266 is a low-cost WiFi microcontroller that comes in a variety of shapes and sizes. For this project, we recommend using a development board like the NodeMCU or Wemos D1 mini, which packages the ESP8266 with useful extras like voltage regulation and a USB-to-serial converter.

These boards make it easy to power and program the ESP8266 using just a USB cable connected to your computer. They also breadboard-friendly, so you can quickly prototype a circuit without the need for soldering.

Setting Up the Arduino IDE

To program the ESP8266, we’ll use the Arduino IDE. If you don’t already have it installed, download and install the latest version from the official Arduino website:

https://www.arduino.cc/en/Main/Software

Next, you’ll need to install support for the ESP8266 in the Arduino IDE:

  1. Open the Arduino IDE
  2. Go to File > Preferences
  3. In the “Additional Boards Manager URLs” field, paste the following URL:
    http://arduino.esp8266.com/stable/package_esp8266com_index.json
  4. Click OK to close the Preferences window
  5. Go to Tools > Board > Boards Manager
  6. Search for “esp8266” and install the “esp8266” package by ESP8266 Community

After completing these steps, you should see ESP8266 boards available under Tools > Board.

Wiring the Circuit

Now let’s wire up the IoT button circuit on a breadboard. Follow these steps:

  1. Insert the momentary pushbutton into the breadboard
  2. Use a jumper wire to connect one leg of the button to the ESP8266 GPIO pin (e.g. D1 on NodeMCU, D3 on Wemos D1 mini)
  3. Use another jumper wire to connect the other leg of the button to GND on the ESP8266
  4. Connect the ESP8266 to your computer with a micro USB cable

Here’s a wiring diagram showing the connections:

Programming the IoT Button

With the circuit assembled, it’s time to program the ESP8266 to function as an IoT button. We’ll use the Arduino IDE to write and upload a sketch that connects the ESP8266 to WiFi and triggers an IFTTT action when the button is pressed.

First, open the Arduino IDE and create a new sketch. Include the following libraries at the top:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

Next, define your WiFi credentials and IFTTT Webhook settings:

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* apiKey = "your_IFTTT_API_key";
const char* event = "your_IFTTT_event";

Replace your_SSID and your_PASSWORD with your WiFi network name and password. You’ll also need to create an applet on IFTTT with a Webhook trigger and note the API key and event name to use in the sketch.

In the setup() function, initialize the button pin as an input and connect to WiFi:

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

In the loop() function, check the state of the button pin. If it’s pressed (reads LOW with INPUT_PULLUP), trigger the IFTTT action by making an HTTP request to the Webhook URL:

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    HTTPClient http;
    String url = "http://maker.ifttt.com/trigger/" + String(event) + "/with/key/" + apiKey;
    http.begin(url);
    http.GET();
    http.end();
    delay(5000); 
  }
}

Upload the sketch to your ESP8266 board. If everything is set up correctly, pressing the button should trigger your IFTTT action!

Creating IFTTT Applets

To use your IoT button, you’ll need to create an applet on IFTTT that specifies what action to take when the button is pressed. IFTTT provides a wide range of services and actions that you can combine in creative ways.

Here are a few example applets you could create:

  • Send an email to yourself
  • Post a tweet or Facebook status update
  • Toggle your smart lights or other smart home devices
  • Log a GPS location or timestamp to a Google Spreadsheet
  • Call your phone with a custom message
  • Increment a counter and trigger an action when a threshold is reached

To create an applet:

  1. Sign in to your IFTTT account and click “My Applets”
  2. Click the “New Applet” button
  3. Click “+this” and search for the “Webhooks” service
  4. Choose “Receive a web request” as the trigger
  5. Enter an event name to use in your sketch (e.g. “button_pressed”)
  6. Click “+that” and choose an action service and action
  7. Configure the action settings as desired
  8. Click “Create action” then “Finish”

Your applet is ready to use! Every time the ESP8266 sketch makes a request to the Webhook URL, your action will be triggered.

Going Further

This basic example demonstrates how to create an IoT button with an ESP8266, but there are many ways you can extend and customize the project:

Adding More Buttons

Want to trigger multiple actions? Wire up additional pushbuttons to different GPIO pins on the ESP8266 and modify the sketch to handle each one individually. You could even use buttons of different colors or labels to indicate what each one does.

Connecting a Battery

To make your IoT button portable, you can power it with a 3.7V lithium-ion battery instead of the USB cable. The NodeMCU and Wemos D1 mini boards have built-in voltage regulators that can handle the battery voltage. Just be sure to add a switch to turn off the power when not in use, since the ESP8266 will drain the battery even when it’s not actively transmitting.

Building an Enclosure

For a polished look, house your IoT button circuit inside a custom enclosure. You could 3D print a case or repurpose a small project box. Drill holes for the pushbutton(s) and USB port, then mount the components securely inside.

Conclusion

You now have a functioning ESP8266 IoT button that can be programmed to trigger all sorts of online actions using IFTTT. This is just the beginning of what’s possible with the power of the Internet of Things!

With some creativity and experimentation, you can use the same basic idea to build custom controllers, monitoring systems, and automation triggers. How will you use your IoT button?

FAQ

Can I use an ESP8266 module other than the NodeMCU or Wemos D1 mini?

Yes, any ESP8266 module or development board can work for this project as long as it has a suitable GPIO pin available and can be programmed with the Arduino IDE. However, boards without built-in USB support may require additional wiring and components.

What’s the range of the ESP8266 WiFi connection?

The range depends on several factors, including the type of antenna, obstacles in the environment, and interference from other devices. In general, you can expect a range of around 20-30 meters indoors, or up to 100 meters outdoors with line of sight to the access point.

How do I find my IFTTT Webhook API key?

To find your IFTTT Webhook API key:

  1. Sign in to your IFTTT account
  2. Click on your profile picture and select “My services”
  3. Click on “Webhooks” then “Documentation”

Your API key will be displayed at the top of the page.

What happens if the WiFi connection is lost?

If the ESP8266 loses its WiFi connection, the IoT button will stop working until it’s able to reconnect. The example sketch provided will automatically attempt to reconnect in the background. You can add additional code to indicate the connection status with an LED, or take other actions if a connection can’t be established.

Can I use the IoT button with services other than IFTTT?

Absolutely! The example sketch sends a simple HTTP request, so you can adapt it to work with any web-based API or service that accepts incoming requests. Just modify the URL and other parameters in the sketch to match your desired service.