gpio2modbus added 4 buttons
This commit is contained in:
parent
d5ed630ed3
commit
32c8b19dbd
1 changed files with 20 additions and 5 deletions
|
|
@ -7,15 +7,29 @@ from pubsub import pub
|
|||
from WebRelay import WebRelay
|
||||
|
||||
def button_callback(channel):
|
||||
print("Button was pushed!")
|
||||
pub.sendMessage("turn_relay_on", relay_id=0)
|
||||
print("Button ", channel, " was pushed!")
|
||||
if channel == 11:
|
||||
rid = 0
|
||||
elif channel == 13:
|
||||
rid = 1
|
||||
elif channel == 16:
|
||||
rid = 2
|
||||
elif channel == 18:
|
||||
rid = 3
|
||||
pub.sendMessage("turn_relay_off", relay_id=rid)
|
||||
|
||||
|
||||
GPIO.setwarnings(False) # Ignore warning for now
|
||||
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
|
||||
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
|
||||
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 11 to be an input pin and set initial value to be pulled low (off)
|
||||
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 13 to be an input pin and set initial value to be pulled low (off)
|
||||
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 16 to be an input pin and set initial value to be pulled low (off)
|
||||
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 18 to be an input pin and set initial value to be pulled low (off)
|
||||
|
||||
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
|
||||
GPIO.add_event_detect(11, GPIO.RISING, callback=button_callback) # Setup event on pin 11 rising edge
|
||||
GPIO.add_event_detect(13, GPIO.RISING, callback=button_callback) # Setup event on pin 13 rising edge
|
||||
GPIO.add_event_detect(16, GPIO.RISING, callback=button_callback) # Setup event on pin 16 rising edge
|
||||
GPIO.add_event_detect(18, GPIO.RISING, callback=button_callback) # Setup event on pin 18 rising edge
|
||||
|
||||
if __name__ == "__main__":
|
||||
nc_dict = dict()
|
||||
|
|
@ -27,7 +41,8 @@ if __name__ == "__main__":
|
|||
modbus.apply_network_configs(nc_dict)
|
||||
modbus.connect_slave()
|
||||
|
||||
pub.subscribe(modbus.turn_relay_on, "turn_relay_on")
|
||||
# pub.subscribe(modbus.turn_relay_on, "turn_relay_on")
|
||||
pub.subscribe(modbus.turn_relay_off, "turn_relay_off")
|
||||
|
||||
message = input("Press enter to quit\n\n") # Run until someone presses enter
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue