Last active
March 16, 2017 23:19
-
-
Save nalakawula/4dc99cb1259c8de0787697b6f20bc666 to your computer and use it in GitHub Desktop.
Program python3 jadwal on/off lampu realtime. Menggunakan GPIO raspi, Relay, dan Library RPi.GPIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
''' | |
jadwal_lampu.py | |
rev. 2 17/03/2017 | |
program untuk on/off lampu secara realtime sesuai jam yang ditentukan. | |
menggunakan library RPi.GPIO. | |
PIN nomor 40 digunakan untuk trigger relay (Gunakan driver relay seperti transistor) | |
''' | |
import RPi.GPIO as GPIO | |
import datetime | |
from time import sleep | |
GPIO.setmode(GPIO.BOARD) | |
relay_1 = 40 | |
jam_nyala = 18 | |
jam_mati = 21 | |
status_on = 0 | |
status_off = 0 | |
def setup(): | |
GPIO.setup(relay_1, GPIO.OUT) | |
GPIO.output(relay_1, GPIO.LOW) | |
def loop(): | |
now = datetime.datetime.now() | |
global status_on | |
global status_off | |
if ((now.hour >= jam_nyala) and (now.hour < jam_mati)): | |
if (status_on == 0): | |
GPIO.output(relay_1, GPIO.LOW) | |
print(now.hour,":",now.minute,":",now.second) | |
print("sekarang jam untuk menyala") | |
status_on = 1 | |
status_off = 0 | |
if ((now.hour >= jam_mati) or (now.hour < jam_nyala)): | |
if (status_off == 0): | |
GPIO.output(relay_1, GPIO.HIGH) | |
print(now.hour,":",now.minute,":",now.second) | |
print("sekarang jam untuk mati") | |
status_on = 0 | |
status_off = 1 | |
try: | |
setup() | |
while True: | |
loop() | |
sleep(1) | |
#except KeyboardInterrupt: | |
except: | |
GPIO.cleanup() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setelah dikoreksi oleh teman-teman pegelwrt, maka saya edit scriptnya