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!

I made one

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

Give feedback

Comments

Similar projects you might like

Wireless Attendance System Using NodeMCU with MySQL
Intermediate
  • 483
  • 9

Protip

Here we are going to interface NodeMCU with RFID-RC522 and getting data to phpMyAdmin database.

Attendance System Using MYSQL with Raspberry and RFID-RC522
Intermediate
  • 1,009
  • 8

Protip

Here we are taking data from RFID reader which is connected to Node MCU V3 and saving it to MYSQL database running on Raspberry Pi.

IoT Pet Feeder
Intermediate
  • 5,531
  • 31

Full instructions

Use circuito.io to build a smart food dispenser for your pet controlled directly from your phone.

Build a Tank Volume Reader in Under $30 using ESP32
Intermediate
  • 687
  • 6

Full instructions

Build a DIY Level Sensor - tank monitor for volume, temperature, and humidity. Prototype for under $30.

Nodemcu annunciator for Domoticz
Intermediate
  • 29
  • 0

Full instructions

Status annunciator for Domoticz using the Raspberry Pi and Nodemcu.

MATCHBOX LEDS WITH ESP8266 IoT
Intermediate
  • 18
  • 0

Protip

One IoT device controlled by wifi inside a Matchbox -Battery included!!!

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login