Things used in this project

Hardware components:
Esp01
Espressif 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

Happy Plant Notifier
Intermediate
  • 314
  • 5

Full instructions

Get a text or call if your plant is getting too dry, along with a nice LCD display giving information regarding your plant's health.

Temperature and humidity meter (iot)
Intermediate
  • 309
  • 3

Measuring temperature and humidity with Arduino and uploading it to the server

Magic VR Hat
Intermediate
  • 776
  • 8

Wear the hat, get transported to different 360 VR experience.

From BT to WiFi: Creating WiFi Controlled Arduino Robot Car
Intermediate
  • 577
  • 5

Protip

NodeMCU ESP8266 in access point mode: the simplest way to make Wi-Fi controlled Robot Car from Bluetooth Arduino Robot Car + Android App.

DIY Complete Philips Hue Home Automation
Intermediate
  • 2,161
  • 6

Full instructions

This project emulates a Philips Hue Bridge that is able to control multiple types of lights.

Traffic Monitor- Monitors traffic on the Go
Intermediate
  • 160
  • 2

Full instructions

The device uses color indication to display traffic on the road you are driving or you have selected.

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog