Things used in this project

Hardware components:
Esp01
Everything ESP ESP8266 ESP-01
×1
1N4001 Diode
×1
Mfr 25frf52 1k sml
Resistor 1k ohm
×1
Push Button
×1
Software apps and online services:
Q8wtlimqnp04fzhtr9v5
IFTTT Maker service

Schematics

IftttEasyButtonV2.fzz
IftttEasyButtonV2.fzz

Code

init.luaLua
--init.lua

gpio.mode(4, gpio.OUTPUT)
gpio.write(4, gpio.HIGH)
cnt = 0

print("Starting SmartButton")

tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
    cnt = cnt + 1
	print("(" .. cnt .. ") Waiting for IP...")
    if cnt == 10 then
        tmr.stop(1)
        dofile("setwifi.lua")
    end
else
	tmr.stop(1)
    dofile("ifttt.lua")
end
end)
setwifi.luaLua
--setwifi.lua

print("Entering wifi Setup..")

wifi.setmode(wifi.STATIONAP)
cfg={}
    cfg.ssid="SmartButton"
    --cfg.password="12345678" --comment to leave open
wifi.ap.config(cfg)

ipcfg={}
    ipcfg.ip="192.168.1.1"
    ipcfg.netmask="255.255.255.0"
    ipcfg.gateway="192.168.1.1"
wifi.ap.setip(ipcfg)

ap_list = ""

function listap(t)
  for bssid,v in pairs(t) do
   local ssid, rssi, authmode, channel = string.match(v, "([^,]+),([^,]+),([^,]+),([^,]+)")
    ap_list = ap_list.."<option value='"..ssid.."'>"..ssid.."</option>"
  end
end
wifi.sta.getap(1, listap)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end

        if path == "/favicon.ico" then
            conn:send("HTTP/1.1 404 file not found")
            return
        end   

        if (path == "/" and  vars == nil) then
            buf = buf.."<html><body style='width:90%;margin-left:auto;margin-right:auto;background-color:LightGray;'>";
            buf = buf.."<h1>SmartButton Wifi Configuration</h1>"
            buf = buf.."<form action='' method='get'>"
            buf = buf.."<h4>SSID:</h4>"
            buf = buf.."<select name='dssid'>"..ap_list.."</select>"
            buf = buf.." or <input type='text' name='ssid' value='' maxlength='100' width='100px' placeholder='ssid' />"
            buf = buf.."<br><br>"
            buf = buf.."<h4>Password:</h4>"
            buf = buf.."<input type='text' name='password' value='' maxlength='100' width='100px' placeholder='empty if AP is open' />"
            buf = buf.."<p><input type='submit' value='Submit' style='height: 25px; width: 100px;'/></p>"
            buf = buf.."</body></html>"
    
        elseif (vars ~= nil) then
            restarting = "<html><body style='width:90%;margin-left:auto;margin-right:auto;background-color:LightGray;'><h1>Restarting...You may close this window.</h1></body></html>"
            client:send(restarting);
            client:close();
            if(_GET.dssid)then
                ssid = _GET.dssid
                password = ""
                if (_GET.ssid) then
                    ssid = _GET.ssid
                end
                if (_GET.password) then
                    password = _GET.password
                end
                print("Setting to: "..ssid..":"..password)
                tmr.alarm(0, 5000, 1, function()
                    wifi.setmode(wifi.STATION);
                    wifi.sta.config(ssid,password);
                    node.restart()
                end)
            end
        end

        client:send(buf);
        client:close();
        collectgarbage();
    end)
    
end)
ifttt.luaLua
--ifttt.lua

print("Sending to IFTTT")

conn = nil
conn=net.createConnection(net.TCP, 0) 

conn:on("receive", function(conn, payload) 
    --Shutdown!
    gpio.write(4, gpio.LOW)

    --If esp is enabled that means the button is still pushed!
    tmr.alarm(0, 2000, 1, function()
        reset()
    end)
     
end) 
     
conn:on("connection", function(conn, payload) 
     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")
      print("IFTTT request sent. Goodbye") 
end) 
                                       
conn:dns("maker.ifttt.com",function(conn,ip) 
    if (ip) then
        print("We can connect to " .. ip)
        conn:connect(80,ip)
    else
        reset()
    end
end)

function reset()
    print("Reseting Wifi..")
    wifi.sta.disconnect()
    wifi.sta.config("","")
    dofile("setwifi.lua")
end

Credits

Replications

Did you replicate this project? Share it!

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

ESP8266 MikroE Buggy
Intermediate
  • 555
  • 11

Full instructions

ESP8266 web server - driver for MikroE Buggy. Web server provides simple HTML5 canvas as driving joystick.

Automatic Gardening System with NodeMCU and Blynk
Intermediate
  • 685
  • 8

Full instructions

Controlling water and heat of a plantation, using real data as temperature, relative air humidity, and soil moisture.

A Valentine's day wreath that responds to you !
Intermediate
  • 81
  • 2

Full instructions

An IOT connected, interactive Valentine's day wreath which responds when people come close and keeps track of how many people came close !!

Voice Control Chip-E With Google Home
Intermediate
  • 290
  • 3

Full instructions

Want to control a robot with your voice? Here's a great place to start with a Google Home, IFTTT, a WiFi enabled microcontroller and Arduino

EchoGarage
Intermediate
  • 365
  • 4

Work in progress

Universal cloud-based garage door opener with ultrasonic monitoring

Automated Cat Feeder with Particle Photon
Intermediate
  • 1,485
  • 19

Work in progress

The goal was to make it possible to feed my cats when I'm not at home.

ProjectsCommunitiesContestsLiveJobsBetaFree StoreBlogAdd projectSign up / Login
Respect project