Constantin Craciun
Published © GPL3+

Do button from IFTTT - home automation example

Using IFTTT's new Maker channel and Do button to switch devices around the house has never been more fun. Especially for prototyping.

EasyFull instructions provided21,282
Do button from IFTTT - home automation example

Things used in this project

Hardware components

ESP8266-EVB
×1

Software apps and online services

Maker service
IFTTT Maker service

Story

Read more

Code

init.lua

Lua
We usually use this file that runs at startup that has included a delay in it so it allows to safely upload new code on the Esp8266 without it starting to make the connection to wifi and run other code yet.
function startup()
    print('in startup')
    dofile('project.lua')
    end

tmr.alarm(0,5000,0,startup)

project.lua

Lua
The main code, be sure to replace the parts starting with paste_ with the correct information from your account on DeviceHub.net .
projectID        = "paste_project_id_here"
ACTUATOR_NAME    = "paste_actuator_name_here"
apiKey           = "paste_api_key_here"
deviceUUID       = "paste_device_id_here"

serverIP   = "104.155.7.31" -- mqtt.devicehub.net IP
mqttport   = 1883           -- MQTT port (default 1883)
userID     = ""             -- username for authentication if required
userPWD    = ""             -- user password if needed for security
clientID   = "ESP1"         -- Device ID
mqtt_state = 0              -- State control

wifiName = "paste_wifi_ssid_here"
wifiPass = "paste_wifi_password_here"

wifi.setmode(wifi.STATION)
wifi.sta.config(wifiName, wifiPass)
wifi.sta.connect()

relay_pin = 1

gpio.mode(relay_pin, gpio.OUTPUT)
gpio.write(relay_pin, gpio.LOW) 

function setRelay(state)
  if state == 1 then
    gpio.write(relay_pin, gpio.HIGH)  
  else
    gpio.write(relay_pin, gpio.LOW)  
  end
end

function mqtt_do()     
     if mqtt_state < 5 then
          mqtt_state = wifi.sta.status() --State: Waiting for wifi

     elseif mqtt_state == 5 then
          m = mqtt.Client(clientID, 120, userID, userPWD)
          mqtt_state = 10 -- State: initialised but not connected
          m:on("message",
          function(conn, topic, data)
               if data ~= nil then
                  -- print("get: "..data)
                  local pack = cjson.decode(data)
                  if pack.state then
                  
                    print(pack.state)
                    if (pack.state == 0 or pack.state == "0")  then
                      setRelay(0)    
                    elseif (pack.state == 1 or pack.state == "1") then
                      setRelay(1)
                    end

                    
                  end
               end
          end)
     elseif mqtt_state == 10 then
          m:connect( serverIP , mqttport, 0, 
          function(conn)
               print("Connected to MQTT:" .. serverIP .. ":" .. mqttport .." as " .. clientID )
               m:subscribe("/a/"..apiKey.."/p/"..projectID.."/d/"..deviceUUID.."/actuator/"..ACTUATOR_NAME.."/state",0, 
               function(conn)
                    print("subscribed!")
               end)
          end)
          mqtt_state = 20
      end
end

tmr.alarm(0, 600, 1, function() mqtt_do() end)

Credits

Constantin Craciun

Constantin Craciun

5 projects • 34 followers
Passionate about hardware development and IoT.

Comments