Skip to content

Instantly share code, notes, and snippets.

@tsur
Last active November 24, 2016 12:56
Show Gist options
  • Save tsur/9a77d698f05dbd11fb27c59ee172a48d to your computer and use it in GitHub Desktop.
Save tsur/9a77d698f05dbd11fb27c59ee172a48d to your computer and use it in GitHub Desktop.
raspberry pi - garage door opener

Note: Set all wires up before keeping reading. Negative jumper must go into 3rd pin right column and positive jumper into 4th pin left column (right and left looking ethernet port in pi in front of your eyes)

Upload this content into your pi:

if do not know how to access your pi, connect it to your local network and to your pc or power source and discover hosts with ssh enabled

$> nmap -p 22 -sV 192.168.1.0/24

Then try with default login/passwd:

# Enter with default settings
ssh pi@<IP> # i.e. ssh [email protected]
# Use raspberry as passwd

Once got access into your pi, download this gist to your pi

git clone https://gist.github.com/9a77d698f05dbd11fb27c59ee172a48d.git garage_doors && cd garage_doors

Give permissions to setup if neede and run the script to install dependencies:

chmod +x setup.sh
./setup.sh

After that open a screen session:

screen -S garage_doors
# use <C-a-d> to come back to main shell and then from main shell use screen -r garage_doors
# to kill, type exit within screen session

Finally run the process:

sudo python doors.py

Note: for connectivity issues, make sure file /etc/network/interfaces is working (i.e ping google.com, or if firewall does not accept pinging, then use curl --silent --head https://google.com)

import signal
import sys
import time
import datetime
#import urlparse
import paho.mqtt.client as mqtt
try:
import RPi.GPIO as GPIO
GPIO_ACTIVED = True
except:
GPIO_ACTIVED = False
MQTT_HOST = "iot.eclipse.org"
MQTT_PORT = 1883
MQTT_TOPIC = "com/itrsgroup/mlg2016"
MQTT_LOG = True
GPIO_JOIN = 17
GPIO_LEAVE = 22
def setup_gpio():
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(GPIO_JOIN, GPIO.OUT) ## Setup GPIO Pin 17(11) to OUT
GPIO.setup(GPIO_LEAVE, GPIO.OUT) ## Setup GPIO Pin 22(15) to OUT
def signal_handler(signal, frame):
log("quitting ...")
GPIO.cleanup()
sys.exit(0)
#def reset(publish):
# publish(MQTT_TOPIC, "garage?0")
def log(string, *f):
if MQTT_LOG:
print(string.format(*f))
def open_doors(mqttc, pin):
if not GPIO_ACTIVED:
return
log("Open door '{}' on {} ", "join" if pin == GPIO_JOIN else "leave", datetime.datetime.now())
# start current
GPIO.output(pin, True)
time.sleep(1)
# stop current
GPIO.output(pin, False)
def on_connect(mqttc, obj, flags, rc):
#reset(mqttc.publish)
log("connected")
def on_message(mqttc, obj, msg):
try:
(command, value) = str(msg.payload).split("?")
#options = urlparse.parse_qs(value)
log("Got message {} with value {}", command, value)
if not mqttc.sem and command == "garage":
mqttc.sem = True
open_doors(mqttc, GPIO_JOIN if value == "join" else GPIO_LEAVE)
# reset(mqttc.publish)
# publish ack to the emitter to let it know the command was executed
mqttc.sem = False
except Exception, e:
print(e)
def on_publish(mqttc, obj, mid):
log("onpublish: mid {}", str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
log("onsubscribed: mid {}, graned_qos {}", str(mid), str(granted_qos))
def on_log(mqttc, obj, level, string):
log(string)
def on_disconnect(mqttc, userdata, rc):
log("disconnected {}", str(rc))
connect()
def setup_mqtt_client():
mqttc = mqtt.Client()
mqttc.sem = False
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.on_disconnect = on_disconnect
if MQTT_LOG:
mqttc.on_log = on_log
return mqttc
def connect():
try:
mqttc = setup_mqtt_client()
mqttc.connect(MQTT_HOST, MQTT_PORT)
mqttc.subscribe(MQTT_TOPIC)
mqttc.loop_forever()
except Exception, e:
print str(e)
log("quitting ... ")
if GPIO_ACTIVED:
GPIO.cleanup()
if __name__ == "__main__":
if GPIO_ACTIVED:
setup_gpio()
connect()
#!/bin/bash
sudo apt-get update
sudo apt-get -y install screen
sudo apt-get -y install python-pip
sudo apt-get -y install python-rpi.gpio
# if running python3
# sudo apt-get -y install python3-rpi.gpio
sudo pip install --upgrade pip
sudo pip install -r requirements.txt
import sys
import time
import datetime
import RPi.GPIO as GPIO
def setup_gpio():
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(17, GPIO.OUT) ## Setup GPIO Pin 17(11) to OUT
GPIO.setup(22, GPIO.OUT) ## Setup GPIO Pin 22(15) to OUT
def log(string, *f):
print(string.format(*f))
def test(pin):
log("Testing pin '{}' on {} ", pin, datetime.datetime.now())
# start current
GPIO.output(pin, True)
time.sleep(1)
# stop current
GPIO.output(pin, False)
if __name__ == "__main__":
setup_gpio()
test(17)
test(22)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment