-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc_pi_interrupt.py
51 lines (37 loc) · 1.14 KB
/
lc_pi_interrupt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Project : IoT Home Automation
# Author: Navaneeth M
# Target Board : RaspberryPi 3 Model B
import RPi.GPIO as GPIO
import json
import time
import threading
import requests
# Button Configurations
GPIO.setmode(GPIO.BOARD)
s1 = 07
s2 = 11
switch2room = {
s1: '{"topic":"control/n1","pin":2}',
s2: '{"topic":"control/n1","pin":4}',
}
GPIO.setwarnings(False) # because I'm using the pins for other things too!
GPIO.setup([s1,s2], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
################################### Button Configurations End
#Interrupt Handler Functions
i = 0
def handle(pin):
global i
print i
print "pin : " + str(pin)
node_data = json.loads(switch2room[pin])
pub_msg = {"action":GPIO.input(pin),"pin":node_data['pin']}
#print "action : " + str(GPIO.input(pin))
print("json publish ::: topic : " + node_data['topic'] + ", message : " + json.dumps(pub_msg))
#print "action : " + str(GPIO.input(pin))
print " "
i+= 1
GPIO.add_event_detect(s1, GPIO.BOTH, handle, bouncetime=50)
GPIO.add_event_detect(s2, GPIO.BOTH, handle, bouncetime=50)
#######################################Interrupt Handler Functions End
while True:
time.sleep(1e6)