From d5ed630ed3b3226c348ebb94510bcf08289efd4d Mon Sep 17 00:00:00 2001 From: Yeongdo Park Date: Thu, 13 Aug 2020 17:59:30 -0700 Subject: [PATCH] GPIO to Modbus test code --- gpio2modbus.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 gpio2modbus.py diff --git a/gpio2modbus.py b/gpio2modbus.py new file mode 100644 index 0000000..e4cadb7 --- /dev/null +++ b/gpio2modbus.py @@ -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()