SimpleIOThings
Published © GPL3+

$10 DIY Wifi IFTTT Smart Button (No Coding/Soldering Req'd)

A $10 no soldering needed, microusb powered version of Noel Portugal's now famous IFTTT Button.

BeginnerFull instructions provided23,462
$10 DIY Wifi IFTTT Smart Button (No Coding/Soldering Req'd)

Things used in this project

Hardware components

DIY Project Box Enclosure
×1
Female-to-Female Jumper Wires
×1
MicroUSB Cable (10 ft)
×1
USB Wall Charger
×1
ESP8266 Dev Board
×1
Tactile Push Button
×1

Software apps and online services

Maker service
IFTTT Maker service

Hand tools and fabrication machines

Flat Nose Pliers
Hack Saw
Super Glue

Story

Read more

Schematics

Button.fzz

How to wire your ESP8266 Development Board to any Momentary Tactile Push Button.

Code

init.lua

Lua
This file initializes all other files during startup.
--init.lua

gpio.mode(2, gpio.OUTPUT)
-- Define Mode for General Purpose In Out Pin; Mode 2 == Pin# GPIO4
-- http://esp8266.co.uk/tutorials/introduction-to-the-gpio-api/

gpio.write(2, gpio.LOW)
-- Set esp8266 Pin GPIO2 to write mode, and write "High" to signify high noise level.

cnt = 0
-- Set count variable


print("Starting SimpleIOThings Wifi to Cloud Alert System")
-- Displays startup status

tmr.alarm(1, 1000, 1, function()
-- Create timer to execute function after 1 second (1000 ms)

if wifi.sta.getip() == nil then
    cnt = cnt + 1
	print("(" .. cnt .. ") Waiting for a Wifi Connection...")
-- function that counts each failed attempt to get an IP address from your router.


    if cnt == 10 then
        tmr.stop(1)
        dofile("setwifi.lua")
    end
-- if statement that executes code in "setwifi.lua", which needs to be loaded onto your device along with this file.

else
	tmr.stop(1)
	print("You are connected to Wifi. IP Address:"
	..wifi.sta.getip())
    print("Starting Sensor...")
    tmr.alarm(0, 3000, 0, function()
    print("Listening for Sensor Inputs...")
    dofile("sensor.lua")
    end)
-- else statement that stops the previously setup timer and executes "ifttt.lua" when your device is connected to the internet.

end
end)

ifttt.lua

Lua
A lot of the variables you see here require companion files. The companion files are generated by SensorSetup.bat from the simpleiothings.com download page, which prompts you for essential info to make your Sensor operate the way you want.
--ifttt.lua

file.open( "iftttKey.lua", "r" )
iftttKey = file.read()
iftttKey = string.gsub(iftttKey, "%s+", "")
file.close()
--print("IFTTT Maker Key: "..iftttKey)

file.open( "eventName.lua", "r" )
eventName = file.read()
eventName = string.gsub(eventName, "%s+", "")
file.close()
print("IFTTT Event Name: "..eventName)

file.open( "sensorType.lua", "r" )
sensorType = file.read()
sensorType = string.gsub(sensorType, "%s+", "")
file.close()
print("Sensor Type: "..sensorType)

file.open( "sensorLocOne.lua", "r" )
sensorLocOne = file.read()
sensorLocOne = string.gsub(sensorLocOne, "%s+", "")
file.close()
print("Sensor Location: "..sensorLocOne)

file.open( "sensorLocTwo.lua", "r" )
sensorLocTwo = file.read()
sensorLocTwo = string.gsub(sensorLocTwo, "%s+", "")
file.close()
print("Sensor Location: "..sensorLocTwo)

conn = nil
conn=net.createConnection(net.TCP, 0) 
--net.createConnection(type, secure); i.e. Type = TCP,

conn:on("receive", function(conn, payload) 
    --Shutdown!
    gpio.write(4, gpio.LOW)
    tmr.alarm(0, 3000, 1, function()
        delay()
        end)
    end)

conn:on("connection", function(conn, payload) 

print("Sending to IFTTT")
conn:send("POST /trigger/" .. eventName .. "/with/key/" .. iftttKey .. "/?value1=" .. sensorType .. "&value2=" .. sensorLocOne .. "&value3=" ..sensorLocTwo
--/?value1=SoundSensor&value2=Kitchen&value3=10:30" 

	  .." 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")
	  print("Alert sent to IFTTT.")

end) 

conn:dns("maker.ifttt.com",function(conn,ip) 
    if (ip) then
        print("We can connect to " .. ip)
        conn:connect(80,ip)
    else
		delay()
    end
end)

function delay()
  --wifi.sta.disconnect()
  --wifi.sta.config("","")
  dofile("delay.lua")
end

sensor.lua

Lua
--sensor.lua
file.open( "sensorThreshold.lua", "r" )
sensorThreshold = file.read()
sensorThreshold = string.gsub(sensorThreshold, "%s+", "")
sensorThreshold = tonumber(sensorThreshold)
file.close()


--Set how many times you'd like the sensor to receive an input before sending an alert.

file.open( "timerThreshold.lua", "r" )
timerThreshold = file.read()
timerThreshold = string.gsub(timerThreshold, "%s+", "")
timerThreshold = tonumber(timerThreshold)*1000
file.close()
--print("Timer Reset: "..timerThreshold)

--The interaction of these two variables above will determine your tolerance for false alarms.
--For example if this device is a panic button and you want to send a message after 3 button presses within 10 seconds, set countThreshold = 3 and TimerResetThreshold = 10000.
--If this device is a Smoke Alarm Sound Sensor, and you want to wait until the alarm has been sounding for ~30 seconds before sending an alert, set countThreshold = 60, TimerResetThreshold = 60000 (60 loud sounds within 60 seconds).
--If this device is a Baby Noise SMS Alert, set countThreshold = 5, TimerResetThreshold = 60000 (5 loud sounds/cries within 60 seconds). Keep in mind you will have to calibrate your microphone sensor to a lower threshold than a Smoke Alarm Sensor.

buttonPin = 2 -- this is ESP8266 pin GPIO4
gpio.mode(buttonPin,gpio.INT,gpio.PULLUP)
count = 0

function resetCounter()
	count = 0
	end

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

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

function onChange()
	if gpio.read(buttonPin) == 0
	then count = count + 1
	
		tmr.alarm(6,timerThreshold,0,function() 
		resetCounter()
		print("Time limit reached. Restarting Counter...")
		end)
		
			if count == sensorThreshold
			then 
				print("(" .. count .. ") sensor inputs counted! Sending Alert!")
				dofile("ifttt.lua")
			else print("(" .. count .. ") sensor inputs counted...")
			end		
    end
end

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

delay.lua

Lua
This file is used to ensure that you don't get a continuous flow of notifications from your device which can be both expensive and annoying. For example, for a wifi button, I generally configure it so it can't send a message more than once per minute. The tmr.delay function monopolizes the dev board, preventing it from executing any other actions. The board then resets after the delay is over.
--delay.lua
file.open( "alertDelay.lua", "r" )
alertDelay = file.read()
alertDelay = string.gsub(alertDelay, "%s+", "")
alertDelay = tonumber(alertDelay)*60*1000
file.close()
print("alertDelay: "..(alertDelay/(60*1000)).." minutes.")

print("Waiting "..(alertDelay/(60*1000)).." minutes before sending another message.")

tmr.delay(alertDelay)

function restart ()
node.restart()
end

tmr.alarm(0,alertDelay,0,restart)

Credits

SimpleIOThings

Posted by SimpleIOThings

Comments