GPIO to Modbus test code

This commit is contained in:
Yeongdo Park 2020-08-13 17:59:30 -07:00
parent 738337c5f8
commit d5ed630ed3

35
gpio2modbus.py Normal file
View file

@ -0,0 +1,35 @@
import time
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from pubsub import pub
from WebRelay import WebRelay
def button_callback(channel):
print("Button was pushed!")
pub.sendMessage("turn_relay_on", relay_id=0)
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.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
if __name__ == "__main__":
nc_dict = dict()
nc_dict["ipaddr"] = "192.168.1.2"
nc_dict["port"] = 502
nc_dict["uid"] = 240
modbus = WebRelay()
modbus.apply_network_configs(nc_dict)
modbus.connect_slave()
pub.subscribe(modbus.turn_relay_on, "turn_relay_on")
message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up
modbus.disconnect_slave()