Things used in this project

Hardware components:
Esp01
Everything ESP ESP8266 ESP-01
×1
5v Relay Module
×1
00114 05
SparkFun Breadboard Power Supply 5V/3.3V
×1
Magnetic Door Switch
×1
TTL Serial Converter
×1
Software apps and online services:
OpenHAB
Nodemcu style5 150px.png 150x150
NodeMCU firmware

Schematics

Wiring Diagram
8589391425867423356

Code

sitemapSpace
OpehHab sitemap file
      Text item=Switch1
      Switch item=Relay1 mappings=[ON="Go!"]
esp8266-garage-door.luaLua
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)  
itemsSpace
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.mapSpace
map file for OpenHAB
goes in the transform/switch.map directory
0=open
1=closed
rulesSpace
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

Pete2014
Pete Hoffswell

Network Engineer for All the People. Internet advocate. Microcontroller Fan. Maker.

Contact

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

Wildlife Detector
Advanced
  • 543
  • 11

Device detects motion, then takes a picture of triggering wildlife. Uses a neural net to identify the wildlife species and does something

MEGA BREAD - Hank the Hexabot
Advanced
  • 203
  • 4

Work in progress

A 6 legged, 18 servo, self guiding time wasting project. MEGA BREAD series #7, Delivery and retrieval from the air project.

Solar Power Module V2
Advanced
  • 1,615
  • 24

Full instructions

Single board to control solar charge and battery monitoring for your outdoor projects. Your project will see a perpetually charged battery.

Living Space web APP
Advanced
  • 32
  • 1

Work in progress

Moving Somewhere New? Want to know the climate of a location with amenities nearby ? Use LIVING SPACE APP to find that perfect location.

Kalam32 ESP32 Board as a Wireless WiFi/TCP ECG/Resp Monitor
Advanced
  • 507
  • 12

Protip

We interface one of our most popular Arduino shields, the ADS1292R ECG/Respiration shield, to ProtoCentral's new ESP32 platform.

 Theft Detector
Advanced
  • 37
  • 1

Full instructions

This project will show you how to make a simple valuable things or gadget protector.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback