Noel Portugal
Published © GPL3+

ESP8266 IFTTT Easy Button

The classic Staples Easy Button with an IFTTT twist.

BeginnerFull instructions provided78,659
ESP8266 IFTTT Easy Button

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
Staples Easy Button
×1

Software apps and online services

Maker service
IFTTT Maker service
NodeMCU firmware
NodeMCU firmware

Hand tools and fabrication machines

FTDI Serial TTL-232 USB Cable
ESP8266 ADAPTER AND BREAKOUT BOARD KIT

Story

Read more

Schematics

IFTTT Easy Button

Code

init.lua

Lua
--init.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("<SSID>","<PSK>")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
	print("Waiting for IP...")
else
	tmr.stop(1)
    print("Your IP is "..wifi.sta.getip())
    print("5 seconds to stop timer 0")
    tmr.alarm(0, 5000, 0, function()
        print("Starting button listener...")
        dofile("button.lua")
    end)
end
end)

button.lua

Lua
--button.lua
buttonPin = 4 -- this is ESP-01 pin GPIO02
gpio.mode(buttonPin,gpio.INT,gpio.PULLUP)

function debounce (func)
    local last = 0
    local delay = 200000

    return function (...)
        local now = tmr.now()
        if now - last < delay then return end

        last = now
        return func(...)
    end
end

function onChange()
    if gpio.read(buttonPin) == 0 then
        print("That was easy! ")
        dofile("ifttt.lua") 
        tmr.delay(500000)
    end
end

gpio.trig(buttonPin,"down", debounce(onChange))

ifttt.lua

Lua
--ifttt.lua
conn = nil
conn=net.createConnection(net.TCP, 0) 

conn:on("receive", function(conn, payload) 
     print(payload) 
     end) 
     
conn:on("connection", function(conn, payload) 
     print('\nConnected') 
     conn:send("GET /trigger/button/with/key/<MAKER-CHANNEL-KEY>"
      .." HTTP/1.1\r\n" 
      .."Host: maker.ifttt.com\r\n"
      .."Accept: */*\r\n" 
      .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 
      .."\r\n")
     end) 
     
conn:on("disconnection", function(conn, payload) 
      print('\nDisconnected') 
      end)
      
print('Posting to ifttt.com')                                    
conn:connect(80,'maker.ifttt.com')

Credits

Noel Portugal

Noel Portugal

2 projects • 60 followers

Comments