Pete Hoffswell
Published © GPL3+

ESP8266 Wifi Smart Garage Door

You can use an ESP-01 micro controller and an OpenHAB server to control your Garage Door.

AdvancedFull instructions provided10 hours12,784
ESP8266 Wifi Smart Garage Door

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
5v Relay Module
×1
SparkFun Breadboard Power Supply 5V/3.3V
SparkFun Breadboard Power Supply 5V/3.3V
×1
Magnetic Door Switch
×1
TTL Serial Converter
×1

Software apps and online services

OpenHAB
NodeMCU firmware
NodeMCU firmware

Story

Read more

Schematics

Wiring Diagram

Code

sitemap

Space
OpehHab sitemap file
      Text item=Switch1
      Switch item=Relay1 mappings=[ON="Go!"]

esp8266-garage-door.lua

Lua
esp8266 code to drive garage door
-- Garage Door controller version 2/15/15 pete@hoffswell.com
-- GPIO0 is connected to switch with internal pulldown enabled  
gpio.write(3,gpio.LOW)
gpio.mode(3,gpio.INPUT,gpio.PULLDOWN)  
--GPIO2 is connected to Relay
gpio.mode(4,gpio.OUTPUT)  
gpio.write(4,gpio.HIGH)  

print("Program Start")

-- Start up mqtt
m = mqtt.Client("ESP1", 120, "user", "password")
m:lwt("/lwt", "offline", 0, 0)
m:connect("192.168.15.22", 1883, 0, function(conn) print("mqtt connected")
   m:subscribe("openhab/garage/relay1",0, function(conn) print("subscribed relay1") 
   end)
end)

-- Reconnect to mqtt server if needed
m:on("offline", function(con) print ("reconnecting...")
   tmr.alarm(1, 10000, 0, function()
      m:connect("192.168.15.22", 1883, 0, function(conn) print("mqtt connected")
         m:subscribe("openhab/garage/relay1",0, function(conn) print("subscribed relay1") 
         end)
      end)
   end)
end)

 -- Switch Trigger
gpio.trig(3, "both",function (level)
   state = gpio.read(3)
   m:publish("openhab/garage/switch1",state,0,0)
   print("Sent openhab/garage/switch1 " .. state )    
end)

-- MQTT Message Processor
m:on("message", function(conn, topic, msg)   
   print("Recieved:" .. topic .. ":" .. msg)   
   if (msg=="GO") then  -- Activate Door Button
      --print("Activating Door")   
      gpio.write(4,gpio.LOW)  
      tmr.delay(1000000) -- wait 1 second
      gpio.write(4,gpio.HIGH)  
   else  
      print("Invalid - Ignoring")   
   end   
end)  

items

Space
OpenHAB items file
Number Switch1         "Door Status [MAP(switch.map):%d]"    <garagedoor> (Sensors) {mqtt="<[mqttbroker:openhab/garage/switch1:state:default]", autoupdate="false"}
Switch Relay1 "Garage Door"           <garagedoor>    (All) {mqtt=">[mqttbroker:openhab/garage/relay1:command:ON:GO]", autoupdate="false"}

switch.map

Space
map file for OpenHAB
goes in the transform/switch.map directory
0=open
1=closed

rules

Space
OpenHAB rules file
var Timer timer = null

rule "Tweet Switch Status"
when
     Item Switch1 changed
then
     var SimpleDateFormat df = new SimpleDateFormat( "YYYY-MM-dd HH:mm:ss" )
     var String Timestamp = df.format( new Date() )
     if(Switch1.state == 0) {
        sendDirectMessage('yourtwittername', 'Switch Open ' + Timestamp)
        timer = createTimer(now.plusSeconds(300)) [|   // start timer and watch for 5 minute timeout
        sendDirectMessage('yourtwittername', 'Door open too long!')
        ]
     } else if(Switch1.state == 1) {
        sendDirectMessage('yourtwittername', 'Switch Closed ' + Timestamp)
        if(timer!=null) {  // cancel timer, door is closed
            timer.cancel
            timer = null
        }
     }
end

Credits

Pete Hoffswell

Pete Hoffswell

10 projects • 63 followers
Network Engineer for All the People. Internet advocate. Microcontroller Fan. Maker.

Comments