fab-lab.eu
Published

#IoT made easy #ESP8266 and IFTTT (If This Then That) app

Trigger IFTTT Maker channel on a Smartphone using the IF application while feeding data events from ESP8266 ... super easy! #SENSableTHING

IntermediateWork in progress19,626
#IoT made easy #ESP8266 and IFTTT (If This Then That) app

Things used in this project

Software apps and online services

Maker service
IFTTT Maker service

Story

Read more

Code

init.lua

Lua
init script will be started by autostart of nodemcu, scripted in LUA language, change the SSID and passkey according to your Wifi AP, uncomment dofile ("ifttt.lua") if you want to autostart thing.lua after next boot
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("SSID","passkey")
-- 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())
 -- dofile ("ifttt.lua")
 end
end)

ifttt.lua

Lua
sending event to trigger IFTTT event processing in their Maker channel, replace YOURKEY with your IFTTT Maker channel key in "GET /trigger/test/with/key/YOURKEY HTTP/1.1\r\n" as well "test" is your Event name, this test does a time based loop ... attention you might receive a lot events ;-) added ?value1=t1 ...
port = 80
gpio.mode(1, gpio.OUTPUT)

function sendData()
  
t1=adc.read(0) -- reads value from Analog Input (max 1V)

-- conection to IFTTT channel
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) 
	print("Connected, sending event")
        conn:send("GET /trigger/test/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")
        
        -- flash LED GPIO5 0.5 sec to indicate send
        gpio.write(1, gpio.HIGH)
             tmr.alarm(1, 500, 0, function()
                gpio.write(1, gpio.LOW)
             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
tmr.alarm(0, 50000, 1, function() sendData() end )

Credits

fab-lab.eu

fab-lab.eu

22 projects • 276 followers
Maker!

Comments