fab-lab.eu
Published

Build your #Dashbutton like devices plus GPIOs

#ESP8266 based #Dashbutton like devices which can send any http based notification just by a push of a button, compatible with #IFTTT Maker

IntermediateFull instructions provided7,473
Build your #Dashbutton like devices plus GPIOs

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
×1
Mini Pushbutton Switch
×1
LiPo 200mAh
×1
10k SMD resistor
needed to adjust the charging current - limited to 100mA, replace R4 with this 10K
×1

Story

Read more

Custom parts and enclosures

Bottom part of housing

Attention: you do need to cut out a small area by hand if you like to plug in the LiPo connector to the THING ... for guidance check the main picture of the project, you see the LiPo connector close to the USB port

Top part of housing

Mount for push button

This plate will keep the push button on distance and position needed

Code

init.lua

Lua
replace both <YOURSSID> <YOURWIFIKEY> with your wifi access data, e.g. wifi.sta.config("HOMESSID","1234567890")
print('init.lua ver 1.2')
wifi.setmode(wifi.STATION)
print('set mode=STATION (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid())
print('heap: ',node.heap())
-- wifi config start
wifi.sta.config("<YOURSSID>","<YOURWIFIKEY>")
-- wifi config end
wifi.sta.connect()
 tmr.alarm(1, 1000, 1, function()
  if wifi.sta.getip()== nil then
  print("IP unavaiable, Waiting...")
 else
  tmr.stop(1)
 print("ESP8266 mode is: " .. wifi.getmode())
 print("The module MAC address is: " .. wifi.ap.getmac())
 print("Config done, IP is "..wifi.sta.getip())
    print("*** You've got 5 sec to stop timer 0 ***")
    -- this wait trick before starting application makes re-flashing easier
    tmr.alarm(0, 5000, 0, function()
      print("Executing lua")
      dofile("ifttt_sw.lua")
    end)
 end
end)

ifttt_sw.lua

Lua
the core function - if GPIO4 connected button was pressed fire an HTTP post ... in this case to IFTTT, replace with your ideas, in case your are using IFTTT app replace <YOUREVENT> <YOURKEY> with your IFTTT credentials, if you don't know how check our other project on IFTTT ...
port = 80
gpio.mode(1, gpio.OUTPUT)
gpio.mode(2, gpio.INPUT)

  gpio.write(1, gpio.HIGH)
  tmr.delay(500000)   -- wait 1,000,000 us = 1 second
  gpio.write(1, gpio.LOW)
  tmr.delay(500000)   -- wait 1,000,000 us = 1 second
  gpio.write(1, gpio.HIGH)
  tmr.delay(500000)   -- wait 1,000,000 us = 1 second
  gpio.write(1, gpio.LOW)
  tmr.delay(500000)   -- wait 1,000,000 us = 1 second

function sendData()

-- dummy value not used in this sample
t1 = 0

-- GPIO4 is the switch connected with pull-up resistor
local pinValue = gpio.read(2)
if pinValue == gpio.LOW then
    print 'GPIO4 is low'
else
    print 'GPIO4 is high'
end

-- conection to IFTTT channel
-- don't change this code section, needed to allow LUA to resolve the hostname
-- without it just ran work with IP addresses
print("Sending data to IFTTT channel")
conn=net.createConnection(net.TCP, 0) 
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(80,'maker.ifttt.com') 

conn:on("connection", function(conn, payload) 
  if pinValue == gpio.HIGH then
	print("Connected, sending event")
          conn:send("GET /trigger/<YOUREVENT>/with/key/<YOURKEY>?value1="..t1.." HTTP/1.1\r\n")
        conn:send("Host: maker.ifttt.com\r\n")
        conn:send("Accept: */*\r\n")
        conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.)\r\n")
        conn:send("\r\n")
        gpio.write(1, gpio.HIGH)
           tmr.alarm(1, 1000, 0, function() -- time index 1
             gpio.write(1, gpio.LOW)
            end)
  end
end)

conn:on("sent",function(conn)
        print("Closing connection")
        conn:close()
    end)

conn:on("disconnection", function(conn)
        print("Got disconnection...")
    end)

end

-- send data every X ms to thing speak
--- checks every 5000 sec for a press of button
tmr.alarm(0, 5000, 1, function() sendData() end )

Credits

fab-lab.eu

fab-lab.eu

15 projects • 158 followers
Maker!
Contact

Comments